–滚动ResultSet
–更新ResultSet
–RowSet
–为了获得可滚动ResultSet,我们必须用如下的方式来创建Statement或者PreparedSatement
–rs.previous()
–rs.relatice(n)
–rs.absolute(n)
–rs.getRow()
–rs.first()
–rs.last()
–rs.beforeFirst()
–rs.afterLast()
–rs.isFirst()
–rs.isLast()
–rs.isBeforeFirst()
–rs.isAfterLast()
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
public class ConnectionUtil {
//第一种方法
public Connection getConnection(){
Connection conn = null;
try {
//Class.forName加载驱动
Class.forName("com.mysql.jdbc.Driver");
//DriverManager获得连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_db","root","mysqladmin");
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//第二种方法
public Connection getConnection(String driver,String url,String user,String password){
Connection conn = null;
try {
//Class.forName加载驱动
Class.forName(driver);
//DriverManager获得连接
conn = DriverManager.getConnection(url,user,password);
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//第三种方法
public Connection openConnection(){
String driver = "";
String url = "";
String user = "";
String password = "";
Properties prop = new Properties();
Connection conn = null;
try {
//加载属性文件
prop.load(this.getClass().getClassLoader().getResourceAsStream("DBConfig.properties"));
driver = prop.getProperty("driver");
url = prop.getProperty("url");
user = prop.getProperty("user");
password = prop.getProperty("password");
//Class.forName加载驱动
Class.forName(driver);
//DriverManager获得连接
conn = DriverManager.getConnection(url,user,password);
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestResultSet {
public static void test(){
Connection conn = new ConnectionUtil().openConnection();
try {
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
//执行查询获得结果集
ResultSet rs = stmt.executeQuery("select * from UserTbl");
//定义行号
rs.next();
//相对定位
rs.relative(3);
//绝对定位
rs.absolute(2);
//移动游标到第一行
boolean b = rs.first();
boolean b2 = rs.last();
int rowNumber = rs.getRow();
//输出行号,即游标位置
System.out.println(rowNumber);
System.out.println(b);
System.out.println(b2);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
import com.michael.jdbc.TestResultSet;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
TestResultSet.test();
}
}
–更新一行
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestResultSet {
public static void test(){
Connection conn = new ConnectionUtil().openConnection();
try {
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
//执行查询获得结果集
ResultSet rs = stmt.executeQuery("select * from UserTbl");
//绝对定位到第四条记录
rs.absolute(4);
rs.updateString(2, "alibaba");
rs.updateString(3, "alimama");
//更新一行
rs.updateRow();
/*
//定义行号
rs.next();
//相对定位
rs.relative(3);
//绝对定位
rs.absolute(2);
//移动游标到第一行
boolean b = rs.first();
boolean b2 = rs.last();
int rowNumber = rs.getRow();
//输出行号,即游标位置
System.out.println(rowNumber);
System.out.println(b);
System.out.println(b2);
*/
} catch (SQLException e) {
e.printStackTrace();
}
}
}
import com.michael.jdbc.TestResultSet;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
TestResultSet.test();
}
}
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestResultSet {
public static void test(){
Connection conn = new ConnectionUtil().openConnection();
try {
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
//执行查询获得结果集
ResultSet rs = stmt.executeQuery("select * from UserTbl");
//插入一行
rs.moveToInsertRow();
rs.updateString(2, "Jake");
rs.updateString(3, "123456");
rs.updateInt(4, 26);
rs.insertRow();
/* 更新一行
//绝对定位到第四条记录
rs.absolute(4);
rs.updateString(2, "alibaba");
rs.updateString(3, "alimama");
//更新一行
rs.updateRow();
*/
/*
//定义行号
rs.next();
//相对定位
rs.relative(3);
//绝对定位
rs.absolute(2);
//移动游标到第一行
boolean b = rs.first();
boolean b2 = rs.last();
int rowNumber = rs.getRow();
//输出行号,即游标位置
System.out.println(rowNumber);
System.out.println(b);
System.out.println(b2);
*/
} catch (SQLException e) {
e.printStackTrace();
}
}
}
–可更新的ResultSet是强大的,但是在操作期间必须保持数据处于连接状态
–RowSet接口继承了ResultSet,提供了如下的实现类:
• CachedRowSet: 允许断开连接操作
• WebRowSet: 能保存XML文件
• FilteredRowSet/JoinRowSet: 在rowset上SELECT/JOIN操作
• JdbcRowSet: 可以将一行封装为一个Java Bean