📄 movingcursorexample.java
字号:
import java.sql.*;public class MovingCursorExample{ // Private reference to an instance of the Connection Connection connection = null; // Default Constructor public MovingCursorExample( 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 performExample() { Statement stmt = null; ResultSet rs = null; try { // These two variables are used throughout this example String name = null; int cursorPosition = 0; // 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 ); // Let's count the rows int rowSize = 0; while( rs.next() ) { rowSize++; } System.out.println( "Number of Rows in ResultSet is: " + rowSize ); if ( rowSize == 0 ) { System.out.println( "Since there are no rows, exiting..." ); System.exit( 0 ); } // Let's go to the half way point in the ResultSet cursorPosition = Math.round( rowSize / 2 ); System.out.println( "Moving to position: " + cursorPosition ); // Get the name at the half way position rs.absolute( cursorPosition ); System.out.println( "Name: " + rs.getString( 2 ) ); // Try to go back one row relative from the current position rs.relative( -1 ); cursorPosition = rs.getRow(); System.out.println( "Moving to position: " + cursorPosition ); System.out.println( "Name: " + rs.getString( 2 ) ); System.out.println( "Moving to the first row" ); // Move to the first row backwards, one row at a time while( !rs.isFirst() ) { rs.previous(); } System.out.println( "Name: " + rs.getString( 2 ) ); rs.close(); } catch( SQLException ex ) { ex.printStackTrace(); } } public static void main(String[] args) { // Use the previous DatabaseManager Connection conn = DatabaseManager.getConnection(); MovingCursorExample example = new MovingCursorExample( conn ); example.performExample(); // 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 + -