JDBC中Scrollable ResultSet(滚动结果集)(四)
2017-08-23 10:37
351 查看
1.Scrollable ResultSet
2.Syntax :
3.Scroll type constant
There are 3 scroll type constants can be used with ResultSets.
ResultSet.TYPE_FORWARD_ONLY
Default type.. only allows forward only fetching
ResultSet.TYPE_SCROLL_INSENSITIVE
Allows both forward and backward movement. Not sensitive to ResultSet updates.
ResultSet.TYPE_SCROLL_SENSITIVE
Allows both forward and backward movement. Not sensitive to ResultSet updates.
4.Concurrency constant
We can use following Concurrency constants for the ResultSets.
ResultSet.CONCUR_READ_ONLY
Default value .. ResultSet can not be updated.
ResultSet.CONCUR_UPDATABLE
Signifies an updatable ResultSet.
5.Demo Code
Normal ResultSet allows fetching elements in forward only direction. However, Scrollable ResultSet allows us to easily move in forward/backward direction. To create scrollable ResultSet, we must use a Statement/PreparedStatement object and provide scroll type to createStatement/prepareStatement method.
2.Syntax :
1.PreparedStatement pstmt = conn.prepareStatement(sql,Scroll type constant,Concurrency constant);
Statement stmt = conn.createStatement(Scroll type constant,Concurrency constant);
3.Scroll type constant
There are 3 scroll type constants can be used with ResultSets.
ResultSet.TYPE_FORWARD_ONLY
Default type.. only allows forward only fetching
ResultSet.TYPE_SCROLL_INSENSITIVE
Allows both forward and backward movement. Not sensitive to ResultSet updates.
ResultSet.TYPE_SCROLL_SENSITIVE
Allows both forward and backward movement. Not sensitive to ResultSet updates.
4.Concurrency constant
We can use following Concurrency constants for the ResultSets.
ResultSet.CONCUR_READ_ONLY
Default value .. ResultSet can not be updated.
ResultSet.CONCUR_UPDATABLE
Signifies an updatable ResultSet.
1.Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
2.PreparedStatement pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
5.Demo Code
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStat 4000 ement; import java.sql.ResultSet; import java.sql.SQLException; public class ScrollableResultsetDemo { public static void main(String[] args) throws SQLException { String url ="jdbc:mysql://localhost:3306/TestDB"; String user = "userid"; String password = "password"; Connection conn = DriverManager.getConnection(url, user, password); System.out.println("Successfully connected"); getEmployeeData(conn); } private static void getEmployeeData(Connection conn) throws SQLException{ String sql = "select id,name,age from employee"; try(PreparedStatement pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);){ ResultSet rs = pstmt.executeQuery(); //First Record rs.first(); System.out.println("Emp Id : " + rs.getInt("id") + ", Name : " + rs.getString("name") + ", Age : " + rs.getInt("age")); //Last Record rs.last(); System.out.println("Emp Id : " + rs.getInt("id") + ", Name : " + rs.getString("name") + ", Age : " + rs.getInt("age")); //Previous Record rs.previous(); System.out.println("Emp Id : " + rs.getInt("id") + ", Name : " + rs.getString("name") + ", Age : " + rs.getInt("age")); //Next Record rs.next(); System.out.println("Emp Id : " + rs.getInt("id") + ", Name : " + rs.getString("name") + ", Age : " + rs.getInt("age")); } } }
相关文章推荐
- jdbc 可滚动的结果集,可更新例子学习记录
- java_jdbc_可滚动结果集与分页
- JDBC之处理可滚动的结果集和可更新的结果集
- JavaEE JDBC 可滚动和可更新的结果集ResultSet
- JDBC-ResultSet之滚动结果集-光标与元数据
- JDBC-结果集的特性(是否可滚动、是否敏感、是否可更新)
- JDBC滚动结果集、SQL注入、处理大数据、批处理、DAO模式介绍
- 可滚动的结果集操作(Jdbc,oracle数据库)
- 初学JDBC__第六节(可滚动的结果集)
- 将jdbc结果集ResultSet转换成对象列表
- jdbc连接数据库异常对只转发结果集的无效操作:last
- JDBC 入门 - 结果集
- JDBC之通过ResultSet对象对结果集进行处理
- com.microsoft.sqlserver.jdbc.SQLServerException: 结果集已关闭
- [疯狂Java]JDBC:可滚动/可更新的ResultSet
- jdbc调用存储过程获取多个结果集
- 将jdbc结果集ResultSet转换成对象列表
- JDBC(三)查询与 ResultSet 结果集
- jdbc调用oracle function返回结果集
- JDBC可滚动可更新感知更新结果集