Приведенный ниже фрагмент работает с тонким драйвером. С OCI не могу проверить нет под руками. К сожалению действительно - тонкий драйвер в такой ситуации создает курсоп, скроллируемы только в одном направлении. Если надо все-же скролировать набор туда и обратно попробуйте закинуть данные из ResultSet в OracleRowSet. С год назад пробовал - его вовсе можно отсоединить от базы и в файл записать.
| Код | import java.sql.*; import java.io.*; import java.util.*; import java.text.SimpleDateFormat; import oracle.jdbc.*;
class ExApp_4 { private Connection conn; private static Properties connInfo; private static final String driverName = "oracle.jdbc.driver.OracleDriver"; private String dbUrl = "jdbc:oracle:thin:@localhost:1521/XE"; private Locale defLoc = Locale.getDefault(); //------------------- public void selDept() { if (conn == null) { System.out.println("NULL Connection in callEmp() !" ); System.exit( 0 ); } SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy"); CallableStatement cstmt = null; ResultSet rset = null; String block = "begin open ? for select ename, hiredate from emp; end;"; try { cstmt = conn.prepareCall( block ); cstmt.registerOutParameter( 1 ); cstmt.execute(); rset = ((OracleCallableStatement)cstmt).getCursor(1); if (rset != null) checkWarnings( rset.getWarnings() ); System.out.println("============== first time ==============="); while( rset.next() ) { java.sql.Date dt = rset.getDate(2); System.out.println( rset.getString(1) + " " + df.format(dt) ); } System.out.println("========================================"); } catch ( SQLException eSQL ) { ptintSQLExceptions( eSQL ); } catch ( Exception e ) { System.out.println("Error: " + e); } finally { try { if (rset != null) rset.close(); if (cstmt != null) cstmt.close(); if (conn != null) conn.close(); } catch (Exception e) {} } } //-------------------- public void getConn() { try { Locale.setDefault( Locale.US ); conn = DriverManager.getConnection( dbUrl, connInfo ); if (conn != null) checkWarnings( conn.getWarnings() ); // if( defLoc != null ) Locale.setDefault( defLoc ); } catch ( SQLException eSQL ) { ptintSQLExceptions( eSQL ); } catch ( Exception e ) { System.out.println("Error: " + e); } } //------------------------------------------------- public void ptintSQLExceptions( SQLException p_ex ) { while( p_ex != null ) { System.out.println( "Error code :" + p_ex.getErrorCode()); System.out.println( "Msg : " + p_ex.getMessage()); System.out.println( "SQL State : " + p_ex.getSQLState()); p_ex = p_ex.getNextException(); } } //-------------------------------------- public void checkWarnings( SQLWarning p_warn ) { if( p_warn != null ) { while( p_warn != null ) { System.out.println("Warning code :" + p_warn.getErrorCode()); System.out.println("Msg : " + p_warn.getMessage()); System.out.println( "SQL State : " + p_warn.getSQLState()); p_warn = p_warn.getNextWarning(); } } } //====================================== public static void main (String args []) { connInfo = new Properties(); connInfo.put( "user", "scott" ); connInfo.put( "password", "tiger" ); try { Class.forName( driverName ).newInstance(); } catch (Exception e) { System.err.println ( "ERROR: Driver <" + driverName + "> not found"); System.exit(0); } ExApp_4 a = new ExApp_4(); a.getConn(); a.selDept(); } }
|
|