Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之08.JDBC可更新的ResultSet及RowSet

�C设置可滚动ResultSet
�C滚动ResultSet
�C更新ResultSet
�CRowSet
####################Michael分割线#######################
• 设置可滚动ResultSet
�C为了获得可滚动ResultSet,我们必须用如下的方式来创建Statement或者PreparedSatement
image
• 滚动ResultSet
�Crs.previous()
�Crs.relatice(n)
�Crs.absolute(n)
�Crs.getRow()
�Crs.first()
�Crs.last()
�Crs.beforeFirst()
�Crs.afterLast()
�Crs.isFirst()
�Crs.isLast()
�Crs.isBeforeFirst()
�Crs.isAfterLast()
ConnectionUtil.java
package com.michael.jdbc;    

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;    
        }    

}
TestResultSet.java
package com.michael.jdbc;    

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();    
                }    
        }    
}
Main.java
package com.michael.main;    

import com.michael.jdbc.TestResultSet;    

public class Main {    

         /**    
         * @param args    
         */    
         public static void main(String[] args) {    
                TestResultSet.test();    
        }    
}
image
• 更新ResultSet
�C更新一行
image
更新第四条记录
image
TestResultSet.java
package com.michael.jdbc;    

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();    
                }    
        }    
}
Main.java
package com.michael.main;    

import com.michael.jdbc.TestResultSet;    

public class Main {    

         /**    
         * @param args    
         */    
         public static void main(String[] args) {    
                TestResultSet.test();    
        }    
}
image
image
�C插入一行
image
TestResultSet.java
package com.michael.jdbc;    

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();    
                }    
        }    
}
image
image
• RowSet
�C可更新的ResultSet是强大的,但是在操作期间必须保持数据处于连接状态
�CRowSet接口继承了ResultSet,提供了如下的实现类:
• CachedRowSet: 允许断开连接操作
• WebRowSet: 能保存XML文件
• FilteredRowSet/JoinRowSet: 在rowset上SELECT/JOIN操作
• JdbcRowSet: 可以将一行封装为一个Java Bean
####################Michael分割线#######################

本文出自 “王乾De技术博客” 博客,谢绝转载!

你可能感兴趣的:(servlet,jdbc,ResultSet,RowSet,WEB工程师培训)