–设置可滚动ResultSet
–滚动ResultSet
–更新ResultSet
–RowSet
####################Michael分割线#######################
• 设置可滚动ResultSet
–为了获得可滚动ResultSet,我们必须用如下的方式来创建Statement或者PreparedSatement
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之08.JDBC可更新的ResultSet及RowSet_WEB工程师培训
• 滚动ResultSet
–rs.previous()
–rs.relatice(n)
–rs.absolute(n)
–rs.getRow()
–rs.first()
–rs.last()
–rs.beforeFirst()
–rs.afterLast()
–rs.isFirst()
–rs.isLast()
–rs.isBeforeFirst()
–rs.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();    
        }    
}
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之08.JDBC可更新的ResultSet及RowSet_ResultSet_02
• 更新ResultSet
–更新一行
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之08.JDBC可更新的ResultSet及RowSet_JDBC_03
更新第四条记录
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之08.JDBC可更新的ResultSet及RowSet_JDBC_04
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();    
        }    
}
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之08.JDBC可更新的ResultSet及RowSet_RowSet_05
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之08.JDBC可更新的ResultSet及RowSet_WEB工程师培训_06
–插入一行
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之08.JDBC可更新的ResultSet及RowSet_Servlet_07
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();    
                }    
        }    
}
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之08.JDBC可更新的ResultSet及RowSet_ResultSet_08
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之08.JDBC可更新的ResultSet及RowSet_ResultSet_09
• RowSet
–可更新的ResultSet是强大的,但是在操作期间必须保持数据处于连接状态
–RowSet接口继承了ResultSet,提供了如下的实现类:
• CachedRowSet: 允许断开连接操作
• WebRowSet: 能保存XML文件
• FilteredRowSet/JoinRowSet: 在rowset上SELECT/JOIN操作
• JdbcRowSet: 可以将一行封装为一个Java Bean
####################Michael分割线#######################