当前位置:Gxlcms >
数据库问题 >
【数据库】JDBC课设(3)TYPE_SCROLL_INSENSITIVE使结果集可以前后滚动
【数据库】JDBC课设(3)TYPE_SCROLL_INSENSITIVE使结果集可以前后滚动
时间:2021-07-01 10:21:17
帮助过:34人阅读
public static void main(String[] args) {
ResultSet rs =
null;
Statement stmt =
null;
Connection conn =
null;
PreparedStatement pstm=
null;
try {
//1.注册驱动
Class.forName(
"com.mysql.cj.jdbc.Driver");
//2.建立连接
conn = (Connection) DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8&useSSL=FALSE" ,
"root",
"");
//3.处理结果集
// stmt = conn.createStatement();
// rs = stmt.executeQuery("select * from tablename1");
//
//
// while (rs.next()) {
// int age = rs.getInt("age");
//
//// 输出查到的记录的各个字段的值
// System.out.println( " " + age);
// }
//预编译语句
// String sql="select * from tablename1 where age = ?";
// pstm=conn.prepareStatement(sql);
// pstm.setInt(1,21);
// rs=pstm.executeQuery();
// while (rs.next()) {
// int age = rs.getInt("age");
//
//// 输出查到的记录的各个字段的值
// System.out.println( " " + age);
// }
//addbatch方法 执行一组sql语句
// String sql1="insert into tablename1(age) values(?)";
// stmt = conn.createStatement();
// stmt.addBatch(sql1);
// stmt.addBatch(sql2);
// stmt.addBatch(sql3);
//psmt的addbatch方法
// PreparedStatement psmt1=conn.prepareStatement(sql1);
// psmt1.setInt(1,40);
// psmt1.addBatch();
// psmt1.setInt(1,45);
// psmt1.addBatch();
// int[] upRowS=psmt1.executeBatch();
//执行命令所影响数据库中行数的更新计数
// int[] upRowS=stmt.executeBatch();
// for(int tmp:upRowS)
// System.out.println(tmp);
//psmt查询到的结果集 进行previous last等取值
String sql=(
"select * from tablename1");
PreparedStatement psmt=
conn.prepareStatement(sql,rs.TYPE_SCROLL_INSENSITIVE,rs.CONCUR_READ_ONLY);
rs=
psmt.executeQuery();
System.out.println(
"先按顺序输出所有内容");
while(rs.next())
{
int tmp=rs.getInt(
1);
System.out.print(tmp+
" ");
}
System.out.println(
"\n输出第一个值:");
rs.first();
int tmp=rs.getInt(
1);
System.out.println(tmp);
System.out.println(
"输出最后一个值:");
rs.last();
tmp=rs.getInt(
1);
System.out.println(tmp);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
//4.关闭资源
if (rs !=
null) {
rs.close();
rs =
null;
}
if (stmt !=
null) {
stmt.close();
stmt =
null;
}
if (conn !=
null) {
conn.close();
conn =
null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
【数据库】JDBC课设(3)TYPE_SCROLL_INSENSITIVE使结果集可以前后滚动
标签:batch ast http mys main 建立 local 结果 ace