package com.jdbctest.ScrollResult;
import java.sql.*;
import com.jdbctest.Util.JDBCUtil;
public class ScrollResult {
/**可滚动结果集
* @param args
*/
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
conn = JDBCUtil.getConnection();
try {//设置成为可滚动可更新
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
String sql = "SELECT id,sno,sname,score FROM score";
rs = stmt.executeQuery(sql);
/***********结果集的滚动***********************/
//定位到第二条记录
rs.absolute(1);
print(rs);
//向后滚动一条
//rs.previous();
print(rs);
//定位到最后一条
rs.last();
//定位到第一条
print(rs);
rs.first();
rs.beforeFirst();
while(rs.next()){
print(rs);
}
/************用结果集更新数据*******************/
System.out.println("更新数据 -----------------");
rs.absolute(1);
rs.updateString("score", "86");
rs.updateRow();
rs.beforeFirst();
while(rs.next()){
print(rs);
}
System.out.println("插入数据 -----------------");
rs.moveToInsertRow();
rs.updateString("sname", "吴小雨");
rs.updateString("sno", "200608016");
rs.updateDouble("score", 87);
//rs.updateString("birthday", "1996-1-26");
rs.insertRow();
rs.beforeFirst();
while(rs.next()){
print(rs);
}
System.out.println("删除数据 -----------------------------");
rs.absolute(3);
rs.deleteRow();
//移到最前面
rs.beforeFirst();
while(rs.next()){
print(rs);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void print(ResultSet rs) throws SQLException {
System.out.println(rs.getString("sno"));
System.out.println(rs.getString("sname"));
System.out.println(rs.getDouble("score"));
System.out.println("******************************");
}
}
可滚动结果集,可更新结果集
最新推荐文章于 2021-01-21 15:15:22 发布