JDBC之(4)高级特性2行集

来源:互联网 发布:明星的灵异事件知乎 编辑:程序博客网 时间:2024/05/16 12:17
一,事务

一,事务
ublic static void main(String[] args) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtils.getConnection();
            conn.setAutoCommit(false);
            String sql = "update student set age=age+? where id=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, 10);
            pstmt.setInt(2, 4);
            pstmt.executeUpdate();

            pstmt = conn.prepareStatement("update student set name=? where id=?");
            pstmt.setString(1, "liusanjie");
            pstmt.setInt(2, 8);
            pstmt.executeUpdate();
            
            conn.commit();//事务提交,下一个事务的开始
            
            sql="select *  from student";
            rs=pstmt.executeQuery(sql);
            conn.commit();
            
            System.out.println("事务执行后:");
            JdbcUtils.printResult(rs);
        } catch (SQLException e) {
            e.printStackTrace();
            if (null!=conn) {
                try {
                    conn.rollback();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        }finally{
            JdbcUtils.free(rs, pstmt, conn);
        }


   2,public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            
            conn = JdbcUtils.getConnection();
            conn.setAutoCommit(false);              //1、设置事务非自动提交
            
            pstmt = conn.prepareStatement("update student set age=age+? where id=?");
            pstmt.setInt(1,10);
            pstmt.setInt(2,4);
            pstmt.executeUpdate();
            
            pstmt = conn.prepareStatement("update student set name=? where id=?");
            pstmt.setString(1,"liusanjie");
            pstmt.setInt(2,8);
            pstmt.executeUpdate();
            
            conn.commit();  
            //2、提交事务:前一个事务的结束,后一个事务的开始
            
            rs = pstmt.executeQuery("select * from student");
            conn.commit();
            
            System.out.println("事务执行后:");
            System.out.println("id\tname\tage");
            while(rs.next()){
                System.out.print(rs.getInt(1)+"\t");
                System.out.print(rs.getString(2)+"\t");
                System.out.print(rs.getInt(3)+"\n");
            }
            //我们这里可以不写事务
            
        } catch(SQLException ex) {
            ex.printStackTrace();
            //有任何一个事务执行不成功则回滚,将数据恢复到初试状态
            try {
                conn.rollback();              //回滚
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        finally {
            JdbcUtils.free(rs, pstmt, conn);
        }
    }

2,事务隔离;
* 并发控制
 * 事务隔离级别
 */
public class TestJDBC2 {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = JdbcUtils.getConnection();
            // 设置事务隔离级别:
            // Connection.TRANSACTION_READ_COMMITTED
            // 要求某一事务只能等别的事务全部更改完才能读。
            // 可以防止发生脏读,但不可重复读和幻读有可能发生
            conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
            pstmt = conn
                    .prepareStatement("insert into student(name,age) values(?,?)");
            pstmt.setString(1, "Oracle");
            pstmt.setInt(2, 23);
            int count = pstmt.executeUpdate();
            System.out.println("受影响行数:"+count);
            // 查询数据
            rs = pstmt.executeQuery("select * from student");
            JdbcUtils.printResult(rs);

        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.free(rs, pstmt, conn);
        }
    }

二,行集
 1,行集的填充1:
 * 使用结果集填充
 */
public class TestJDBC3 {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtils.getConnection();
            pstmt = conn.prepareStatement("select * from student");
            rs = pstmt.executeQuery();
            
            //得到行集并填充数据
            CachedRowSetImpl rowset = new CachedRowSetImpl();
            rowset.populate(rs);
            
            //关闭结果集,preparedstatement,连接对象
            rs.close();
            pstmt.close();
            conn.close();
            
            System.out.println("结果集关闭,从行集中输出数据:");
            System.out.println("id\tname\tage");
            while(rowset.next()){
                System.out.print(rowset.getInt(1)+"\t");
                System.out.print(rowset.getString(2)+"\t");
                System.out.print(rowset.getInt(3)+"\n");
            }
            rowset.close();         //关闭行集
        } catch(Exception ex) {
            ex.printStackTrace();
        }




三,行集的增删改查

1,修改
public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            
            conn =JdbcUtils.getConnection();
            conn.setAutoCommit(false);              //使用行集修改数据必须设置为非自动提交
            pstmt = conn.prepareStatement("select * from student where id=?");
            pstmt.setInt(1,10);
            rs = pstmt.executeQuery();
            
            //得到行集并填充数据
            CachedRowSetImpl rowset = new CachedRowSetImpl();
            rowset.populate(rs);
            
            //关闭结果集和preparedstatement可以关闭,
            //连接对象不能关闭,行集修改数据库时可能会用到连接对象
            rs.close();
            pstmt.close();
            
            rowset.next();          //移动游标指向第一行数据
           
            
            //修改数据
            //修改id为10的学员信息
            rowset.updateString("name","wuyuanping");
            rowset.updateInt(3,27);
            rowset.updateRow();                      //修改行集
            //conn.close();//如果connection已经关闭,会抛出异常
            rowset.acceptChanges(conn);              //使用conn连接对象,修改数据库
            
            rowset.close();         //关闭行集
        } catch(Exception ex) {
            ex.printStackTrace();
        }

2,添加
public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            
            conn = JdbcUtils.getConnection();
            conn.setAutoCommit(false);              //使用行集修改数据必须设置为非自动提交
            pstmt = conn.prepareStatement("select * from student");
            rs = pstmt.executeQuery();
            
            //得到行集并填充数据
            CachedRowSetImpl rowset = new CachedRowSetImpl();
            rowset.populate(rs);
            
            //关闭结果集和preparedstatement可以关闭,
            //连接对象不能关闭,行集修改数据库时可能会用到连接对象
//            rs.close();
//            pstmt.close();
            
            rowset.last();
            
            rowset.setTableName("student");   //使用行集添加数据时必须指定表名,删除和修改不需要
            rowset.moveToInsertRow();       
            //使用行集添加数据时必须指定主键值(不能使用自动增长),
            //删除和修改可以使用自动增长
            //此示例代码执行前必须将表的主键改为非自动增长
            rowset.updateInt("id",33);      
            rowset.updateString("name","oracle");
            rowset.updateInt("age",25);
            rowset.insertRow();                      //在行集中添加数据
            rowset.moveToCurrentRow();
            rowset.acceptChanges(conn);              //添加数据到数据库
                        
            rowset.close();         //关闭行集
        } catch(Exception ex) {
            ex.printStackTrace();
        }
3,删除
public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            
            conn = JdbcUtils.getConnection();
            conn.setAutoCommit(false);              //使用行集修改数据必须设置为非自动提交
            pstmt = conn.prepareStatement("select * from student");
            rs = pstmt.executeQuery();
            
            //得到行集并填充数据
            CachedRowSetImpl rowset = new CachedRowSetImpl();
            rowset.populate(rs);
            
            //关闭结果集和preparedstatement可以关闭,
            //连接对象不能关闭,行集修改数据库时可能会用到连接对象
            rs.close();
            pstmt.close();
            
            //移动游标指向最后一行数据
            if(rowset.last()){
                //删除行集数据
                rowset.deleteRow();
                rowset.acceptChanges(conn);              //修改数据库
            }
            
            rowset.close();         //关闭行集
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }
4,读取
public class TestJDBC4 {
    public static void main(String[] args) {
        try {
            
            //得到行集并填充数据
            CachedRowSetImpl rowset = new CachedRowSetImpl();
            rowset.setUrl("jdbc:mysql://localhost:3306/test");
            rowset.setUsername("root");
            rowset.setPassword("root");
            rowset.setCommand("select * from student");
            rowset.execute();
            
            System.out.println("使用行集连接数据库并填充数据,从行集中输出数据:");
            System.out.println("id\tname\tage");
            while(rowset.next()){
                System.out.print(rowset.getInt(1)+"\t");
                System.out.print(rowset.getString(2)+"\t");
                System.out.print(rowset.getInt(3)+"\n");
            }
            
            rowset.close();          //关闭行集
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }

四,行集的分页
public class TestJDBC8 {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            
            conn = JdbcUtils.getConnection();
            pstmt = conn.prepareStatement("select * from student");
            rs = pstmt.executeQuery();
            
            //得到行集并填充数据
            CachedRowSetImpl rowset = new CachedRowSetImpl();
            rowset.setPageSize(4);       //设置每页行数
            rowset.populate(rs,5);       //从结果集的第5行开始取数据  
            
            //结果集,preparedstatement,连接对象不能关闭,
            //否则行集无法获得后续页数据
//            rs.close();
//            pstmt.close();
//            conn.close();
            
            System.out.println("从行集中输出数据,第一页:");
            System.out.println("id\tname\tage");
            while(rowset.next()){
                System.out.print(rowset.getInt(1)+"\t");
                System.out.print(rowset.getString(2)+"\t");
                System.out.print(rowset.getInt(3)+"\n");
            }
            
            if( rowset.nextPage() ){     //获得后续页数据并输出
                System.out.println("第二页:");
                System.out.println("id\tname\tage");
                while(rowset.next()){
                    System.out.print(rowset.getInt(1)+"\t");
                    System.out.print(rowset.getString(2)+"\t");
                    System.out.print(rowset.getInt(3)+"\n");
                }
            }
            
            rowset.close();         //关闭行集
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}

五,
 使用行集分页
 * 行集连接数据库并填充数据
 */
public static void main(String[] args) {
        try {
            
            //得到行集并填充数据
            CachedRowSetImpl rowset = new CachedRowSetImpl();
            rowset.setUrl("jdbc:mysql://localhost:3306/test");
            rowset.setUsername("root");
            rowset.setPassword("root");
            rowset.setCommand("select * from student");
            rowset.setPageSize(4);     //每页行数,一定要在execute()方法执行前设置,否则无效
            rowset.execute();
            
            System.out.println("从行集中输出数据,第一页:");
            System.out.println("id\tname\tage");
            while(rowset.next()){
                System.out.print(rowset.getInt(1)+"\t");
                System.out.print(rowset.getString(2)+"\t");
                System.out.print(rowset.getInt(3)+"\n");
            }
            
            if( rowset.nextPage() ){     //获得后续页数据并输出
                System.out.println("第二页:");
                System.out.println("id\tname\tage");
                while(rowset.next()){
                    System.out.print(rowset.getInt(1)+"\t");
                    System.out.print(rowset.getString(2)+"\t");
                    System.out.print(rowset.getInt(3)+"\n");
                }
            }
            
            rowset.close();          //关闭行集
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }
原创粉丝点击