标签:batch ast http mys main 建立 local 结果 ace
ResultSet的Type属性TYPE_FORWARD_ONLY, TYPE_SCROLL_INSENSITIVE,or TYPE_SCROLL_SENSITIVE解释:
1.TYPE_FORWORD_ONLY,只可向前滚动;
2.TYPE_SCROLL_INSENSITIVE,双向滚动,但不及时更新,就是如果数据库里的数据修改过,并不在ResultSet中反应出来。
向前rs.previous() 向后rs.next()
最上面rs.first() 最后面rs.last()
3.TYPE_SCROLL_SENSITIVE,双向滚动,并及时跟踪数据库的更新,以便更改ResultSet中的数据
代码实例
{ 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
原文地址:https://www.cnblogs.com/cckong/p/14255257.html