package oracle.otnsamples.jdbc.batchupdate;
import java.io.IOException;
import java.sql.Date;
import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.BatchUpdateException; import java.sql.Connection;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Enumeration; import java.util.Properties; import java.util.ResourceBundle; import java.util.Vector;
import oracle.jdbc.pool.OracleDataSource;
public class BatchUpdateSample { private BatchUpdateFrame gui;
private Connection connection = null;
public BatchUpdateSample( ) { gui = new BatchUpdateFrame( this ); gui.setVisible( true ); }
public static void main( String[] args ) { BatchUpdateSample batch = new BatchUpdateSample( ); batch.dbConnection( );
if ( batch.connection != null ) { batch.populateHotels( ); } }
public void dispatchEvent( String eventName ) { if ( eventName.equals( "SUBMIT CART" ) ) { this.submitCart( );
} else if ( eventName.equals( "EXIT" ) ) { this.exitApplication( ); }
else if ( eventName.equals( "SHOW ROOM BOOKINGS" ) ) { this.showRoomBookings( gui.getSelectedHotelID( ) ); } }
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" ) + " \n Choose a Hotel to Reserve or Cancel." ); } 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 populateHotels( ) { try { Statement stmt = connection.createStatement( );
ResultSet resultSet = stmt.executeQuery( "SELECT id,name,address,city FROM otn_hotels ORDER BY id" );
while ( resultSet.next( ) ) { Vector vec = new Vector( ); vec.addElement( resultSet.getString( 1 ) ); vec.addElement( resultSet.getString( 2 ) ); vec.addElement( resultSet.getString( 3 ) ); vec.addElement( resultSet.getString( 4 ) );
gui.insertHotelRow( vec ); }
stmt.close( ); } catch ( SQLException ex ) { gui.putStatus( "Error in Populating the otn_hotels Table. " + ex.toString( ) ); } }
public Vector retrieveRoomTypes( int hotID ) { Vector roomTypes = new Vector( );
try { PreparedStatement stmt = connection.prepareStatement( "SELECT room_type FROM otn_available_room_types WHERE hot_id = ?" );
stmt.setInt( 1, hotID );
ResultSet resultSet = stmt.executeQuery( );
while ( resultSet.next( ) ) roomTypes.addElement( resultSet.getString( 1 ) );
stmt.close( ); } catch ( SQLException ex ) { gui.putStatus( "Error while Retrieving Room Type. " + ex.toString( ) ); }
return roomTypes; }
private void showRoomBookings( int hotId ) { try { PreparedStatement stmt = connection.prepareStatement( "SELECT booking_id, to_char(arrival_date, 'DD-Mon-YYYY'), room_type," + " no_of_rooms, no_of_nights, reserved_by FROM otn_hotel_bookings " + " WHERE hot_id = ?" );
stmt.setInt( 1, hotId );
ResultSet resultSet = stmt.executeQuery( );
String name = new String( );
if ( resultSet.next( ) ) { do {
Vector newRow = new Vector( );
newRow.addElement( resultSet.getString( 1 ) ); newRow.addElement( resultSet.getString( 6 ) ); newRow.addElement( resultSet.getString( 2 ) ); newRow.addElement( resultSet.getString( 3 ) ); newRow.addElement( resultSet.getString( 4 ) );
newRow.addElement( resultSet.getString( 5 ) );
gui.cancelDialog.tablemodel.insertRow( newRow ); } while ( resultSet.next( ) );
gui.displayRoomBookings( ); } else { gui.putStatus( "No Reservations in the selected hotel to cancel" );
return; } } catch ( SQLException ex ) { gui.putStatus( "Error while getting the Reservation Details: " + ex.toString( ) ); } }
public int getBookingID( ) { try { int bookingID = 0;
Statement stmt = connection.createStatement( );
ResultSet resultSet = stmt.executeQuery( "SELECT otn_hotel_booking_id.nextval FROM dual" );
if ( resultSet.next( ) ) { bookingID = resultSet.getInt( 1 ); }
stmt.close( );
return bookingID; } catch ( SQLException ex ) { gui.putStatus( "Error in Getting Booking ID " + ex.toString( ) );
return -1; } }
private void submitCart( ) { PreparedStatement cancelStmt = null;
try { int noOfRows = gui.hotelReservationModel.getRowCount( );
if ( noOfRows <= 0 ) { gui.putStatus( "No entry in the Reservation Cart for Submission" ); }
PreparedStatement reStmt = connection.prepareStatement( "insert into otn_hotel_bookings (booking_id, hot_id, arrival_date," + "room_type, no_of_rooms, no_of_nights, reserved_by) " + " VALUES(?,?,?,?,?,?,?)" );
cancelStmt = connection.prepareStatement( "delete from otn_hotel_bookings where BOOKING_ID=?" );
for ( int i = 0; i < noOfRows; i++ ) { Vector newVect = gui.hotelReservationModel.getRow( i );
String resOrCan = newVect.elementAt( 7 ).toString( );
int bookID = Integer.parseInt( newVect.elementAt( 1 ).toString( ) );
if ( resOrCan.equals( "R" ) ) {
int hotID = Integer.parseInt( newVect.elementAt( 0 ).toString( ) ); String name = newVect.elementAt( 2 ).toString( ); String arrDate = newVect.elementAt( 3 ).toString( ); String roomType = newVect.elementAt( 4 ).toString( );
int noRooms = Integer.parseInt( newVect.elementAt( 5 ).toString( ) );
int noNights = Integer.parseInt( newVect.elementAt( 6 ).toString( ) );
Date tempDate = null;
try { SimpleDateFormat formatter = new SimpleDateFormat( "dd-MMM-yyyy" );
tempDate = new Date( formatter.parse( arrDate ).getTime( ) ); } catch ( ParseException pex ) { }
reStmt.setInt( 1, bookID ); reStmt.setInt( 2, hotID ); reStmt.setDate( 3, tempDate ); reStmt.setString( 4, roomType ); reStmt.setInt( 5, noRooms ); reStmt.setInt( 6, noNights ); reStmt.setString( 7, name );
reStmt.addBatch( ); } else if ( resOrCan.equals( "C" ) ) { cancelStmt.setInt( 1, bookID );
cancelStmt.addBatch( ); } }
int[] upDateCounts = reStmt.executeBatch( );
reStmt.close( ); } catch ( BatchUpdateException b ) {
if ( b.toString( ).indexOf( "ORA-00001" ) == -1 ) { gui.putStatus( " Error in Updating the Reservations: " + b.toString( ) ); } else { gui.putStatus( " ERROR! There are date clashes for reservations. " + " The hotel rooms have been reserved already." ); }
try { connection.rollback( );
return; } catch ( SQLException e ) { gui.appendStatus( "Error in rolling back :" + e.toString( ) ); } } catch ( SQLException ex ) { gui.putStatus( "Error in Updating :" + ex.toString( ) );
try { connection.rollback( );
return; } catch ( SQLException e ) { gui.appendStatus( "Error in rolling back :" + e.toString( ) ); } }
try { int[] upDateCounts2 = cancelStmt.executeBatch( );
cancelStmt.close( ); connection.commit( );
gui.hotelReservationModel.clearTable( ); gui.submitCart.setEnabled( false ); } catch ( BatchUpdateException b ) { gui.putStatus( "Error in Updating the Cancellations: " );
try { connection.rollback( );
return; } catch ( SQLException e ) { gui.appendStatus( "Error in rolling back :" + e.toString( ) ); } } catch ( SQLException ex ) { gui.putStatus( "Error in Updating " + ex.toString( ) );
try { connection.rollback( );
return; } catch ( SQLException e ) { gui.appendStatus( "Error in rolling back :" + e.toString( ) ); } }
gui.putStatus( " Updated successfully!" ); }
private void exitApplication( ) { try { gui.putStatus( "Closing the connection....please wait....." );
if ( connection != null ) { connection.close( ); } } catch ( SQLException ex ) { gui.putStatus( "Error in Closing the connection :" + ex.toString( ) ); }
System.exit( 0 ); } }
|