📄 scrollableresultsetexample.java
字号:
import java.sql.*;public class ScrollableResultSetExample{ // Private reference to an instance of the Connection Connection connection = null; // Default Constructor public ScrollableResultSetExample( Connection conn ) { super(); connection = conn; } // Private accessor for the connection private Connection getConnection() { return connection; } // Print out the Employee Records backwards using a scrollable // ResultSet public void printEmployeeRecordsBackwards() { Statement stmt = null; ResultSet rs = null; try { // Get a scrollable ResultSet Connection conn = getConnection(); stmt = conn.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY ); // Get some fields from the employee table String sqlQuery = "SELECT EMPNO, EName, Job, MGR, HIREDATE FROM EMP"; rs = stmt.executeQuery( sqlQuery ); // Move the cursor directly to the last position in the ResultSet // You can't do this like this before JDBC 2.0 rs.last(); // Move the cursor backwards through the ResultSet while( rs.previous() ) { String nbr = rs.getString( 1 ); String name = rs.getString( 2 ); String job = rs.getString( 3 ); String mgr = rs.getString( 4 ); Timestamp hireDate = rs.getTimestamp( 5 ); // Create a new Employee using the data Employee emp = new Employee( nbr, name, job, mgr, hireDate ); // Call the Employee's default toString() method System.out.println( emp.toString() ); } rs.close(); } catch( SQLException ex ) { ex.printStackTrace(); } } public static void main(String[] args) { // Use the previous DatabaseManager Connection conn = DatabaseManager.getConnection(); ScrollableResultSetExample example = new ScrollableResultSetExample( conn ); example.printEmployeeRecordsBackwards(); // Always make sure to close the connection when you are finished try { conn.close(); } catch( SQLException ex ) { ex.printStackTrace(); } catch( Exception ex ) { ex.printStackTrace(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -