package oracle.otnsamples.jdbc.scrollrset;
import java.io.IOException;
import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration; import java.util.Properties; import java.util.ResourceBundle;
import oracle.jdbc.pool.OracleDataSource;
public class ScrollableResSetSample { private ScrollableResSetFrame gui;
private Connection connection = null;
private ResultSet resultSet = null;
private Statement stmt = null; private int currentPosition = -1; private int resSetsize;
public ScrollableResSetSample( ) { gui = new ScrollableResSetFrame( this ); gui.setVisible( true ); }
public static void main( String[] argv ) { ScrollableResSetSample srollSet = new ScrollableResSetSample( );
srollSet.dbConnection( );
if ( srollSet.connection != null ) { srollSet.fetchHotelInfo( );
srollSet.populateHotels( ); } }
public void dispatchEvent( String eventName ) { if ( eventName.equals( "SELECT" ) ) { this.setToSelectedRow( ); } else if ( eventName.equals( "NEXT" ) ) {
if ( currentPosition == -1 ) { gui.putStatus("Please select any row before using 'Show Next' button");
return; }
if ( resSetsize == currentPosition ) { gui.putStatus( "Currently you are at the end of the ResultSet." + " Press 'Show Previous' button " );
return; }
this.moveToNextRow( ); } else if ( eventName.equals( "PREVIOUS" ) ) {
if ( currentPosition == -1 ) { gui.putStatus( "Please select any row before using 'Show Previous'"+ " button." );
return; } else if ( currentPosition == 1 ) { gui.putStatus( "Currently you are at the beginning of the result Set."
+" Press 'Show Next' button " );
return; }
this.moveToPreviousRow( ); } else if ( eventName.equals( "EXIT" ) ) { exitApplication( ); } }
private static Properties loadParams( String file ) throws IOException { Properties prop = new Properties( ); ResourceBundle bundle = ResourceBundle.getBundle( file ); Enumeration enum = bundle.getKeys( ); String key = null;
while ( enum.hasMoreElements( ) ) { key = (String) enum.nextElement( ); prop.put( key, bundle.getObject( key ) ); }
return prop; }
private void dbConnection( ) { try { gui.putStatus( "Trying to connect to the Database" );
Properties prop = this.loadParams( "Connection" );
OracleDataSource ods = new OracleDataSource( );
ods.setDriverType( "thin" );
ods.setServerName( (String) prop.get( "HostName" ) );
ods.setDatabaseName( (String) prop.get( "SID" ) );
ods.setPortNumber( new Integer( (String) prop.get( "Port" ) ). intValue( ) );
ods.setUser( (String) prop.get( "UserName" ) );
ods.setPassword( (String) prop.get( "Password" ) );
connection = ods.getConnection( );
gui.putStatus( " Connected to " + prop.get( "SID" ) + " Database as " + prop.get( "UserName" ) ); } catch ( SQLException ex ) { gui.putStatus( "Error in Connecting to the Database " + '\n' + ex.toString( ) ); } catch ( IOException ex ) {
gui.putStatus( "Error in reading the properties file " + '\n' + ex.toString( ) ); } }
private void fetchHotelInfo( ) { try { gui.putStatus( "Populating ResultSet." );
Statement stm1 = connection.createStatement( );
ResultSet res = stm1.executeQuery( "SELECT COUNT(*) FROM otn_hotels" );
if ( res.next( ) ) { resSetsize = res.getInt( 1 ); }
stm1.close( );
stmt = connection.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY );
String query = "SELECT id,name,address,phone,fax,hotel_url,"+ "hotel_airport FROM otn_hotels";
resultSet = stmt.executeQuery( query ); } catch ( SQLException ex ) { gui.putStatus( "Error " + ex.toString( ) ); } }
private void populateHotels( ) { if ( resultSet != null ) { gui.putStatus( "Populating Hotels Please wait ..." );
try { while ( resultSet.next( ) ) { String hotelId = resultSet.getString( 1 ); String hotelName = resultSet.getString( 2 ); String hotelAddress = resultSet.getString( 3 );
gui.insertHotelRow( hotelId, hotelName, hotelAddress ); } } catch ( SQLException ex ) { gui.putStatus( "Error " + ex.toString( ) ); }
gui.putStatus( "Please select a hotel from the above table." ); } else { gui.putStatus( "ResultSet is null. Error in fetching records from "+ "database" ); } }
private void setToSelectedRow( ) { try { int selectedRow = gui.selectedRow;
if ( currentPosition == -1 ) { currentPosition = selectedRow + 1;
resultSet.absolute( currentPosition ); } else { int relativeIndex = ( selectedRow + 1 ) - currentPosition; currentPosition = selectedRow + 1;
resultSet.relative( relativeIndex ); }
String phoneNumber = resultSet.getString( 4 ); String faxNumber = resultSet.getString( 5 ); String url = resultSet.getString( 6 ); String hotelAirport = resultSet.getString( 7 );
gui.insertHotelInfoRow( phoneNumber, faxNumber, url, hotelAirport );
gui.setCurrentPosition( currentPosition );
gui.putStatus( "Corresponding Hotel information is displayed" ); } catch ( SQLException ex ) { gui.putStatus( "Error " + ex.toString( ) ); } }
private void moveToNextRow( ) { try { currentPosition++; resultSet.next( );
String phoneNumber = resultSet.getString( 4 ); String faxNumber = resultSet.getString( 5 ); String url = resultSet.getString( 6 ); String hotelAirport = resultSet.getString( 7 );
gui.insertHotelInfoRow( phoneNumber, faxNumber, url, hotelAirport );
gui.setCurrentPosition( currentPosition );
gui.selectRow( currentPosition - 1 );
gui.putStatus( "Corresponding Hotel information is displayed" ); } catch ( SQLException ex ) { gui.putStatus( "Error " + ex.toString( ) ); } }
private void moveToPreviousRow( ) { try { currentPosition--; resultSet.previous( );
String phoneNumber = resultSet.getString( 4 ); String faxNumber = resultSet.getString( 5 ); String url = resultSet.getString( 6 ); String hotelAirport = resultSet.getString( 7 );
gui.insertHotelInfoRow( phoneNumber, faxNumber, url, hotelAirport );
gui.setCurrentPosition( currentPosition );
gui.selectRow( currentPosition - 1 );
gui.putStatus( "Corresponding Hotel information is displayed" ); } catch ( SQLException ex ) { gui.putStatus( "Error " + ex.toString( ) ); } }
private void exitApplication( ) { try { gui.putStatus( "Closing the connection....please wait....." );
if ( stmt != null ) { stmt.close( ); }
if ( connection != null ) { connection.close( ); } } catch ( SQLException ex ) { gui.putStatus( "Error " + ex.toString( ) ); }
System.exit( 0 ); } }
|