package oracle.otnsamples.jdbc.progupdate;
import java.io.IOException;
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;
import java.util.Enumeration; import java.util.Properties; import java.util.ResourceBundle;
import oracle.jdbc.pool.OracleDataSource;
public class ProgUpdateSample { private ProgUpdateFrame gui;
private Connection connection = null;
private PreparedStatement stmt = null;
private ResultSet updatableRst = null;
public ProgUpdateSample( ) { gui = new ProgUpdateFrame( this ); gui.setVisible( true ); }
public static void main( String[] args ) { ProgUpdateSample progUpdate = new ProgUpdateSample( );
progUpdate.dbConnection( );
if ( progUpdate.connection != null ) { progUpdate.selectRecords( "%", "%", "%", "%", "%", "%", "%" ); } }
public void dispatchEvent( String eventName ) { String name = gui.nameTextField.getText( ); String address = gui.addressTextField.getText( ); String id = gui.idTextField.getText( ); String phone = gui.phoneTextField.getText( ); String fax = gui.faxTextField.getText( ); String hotelUrl = gui.hotelUrlTextField.getText( ); String airport = gui.airportTextField.getText( );
if ( eventName.equals( "SELECT" ) ) { this.selectRecords( name, address, id, phone, fax, hotelUrl, airport ); }
else if ( eventName.equals( "UPDATE" ) ) { if ( id.equals( "" ) || name.equals( "" ) || address.equals( "" ) || phone.equals( "" ) ) { gui.putStatus( " id, name, address, phone cannot be null" ); } else
{ this.updateRecord( name, address, id, phone, fax, hotelUrl, airport ); } }
else if ( eventName.equals( "INSERT" ) ) { if ( id.equals( "" ) || name.equals( "" ) || address.equals( "" ) || phone.equals( "" ) ) { gui.putStatus( " id, name, address, phone cannot be null" ); } else
{ this.insertRecord( name, address, id, phone, fax, hotelUrl, airport ); } }
else if ( eventName.equals( "DELETE" ) ) { this.deleteRecord( ); }
else if ( eventName.equals( "EXIT" ) ) { this.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 selectRecords( String name, String address, String id, String phone, String fax, String hotelUrl, String airport ) { String query = "SELECT id,name,address,phone,fax," + "hotel_url,hotel_airport FROM otn_hotels h " + "WHERE UPPER(name) like ? AND UPPER(address) " + "LIKE ? AND id LIKE ? " + "AND phone LIKE ? AND
UPPER(NVL(fax, '%')) LIKE ? " + "AND UPPER(NVL(hotel_url, '%')) LIKE ? " + "AND UPPER(NVL(hotel_airport, '%')) LIKE ?";
try { stmt = connection.prepareStatement( query, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE );
if ( name.equals( "" ) ) { name = "%"; } else { name = "%" + name.toUpperCase( ) + "%"; }
if ( address.equals( "" ) ) { address = "%"; } else { address = "%" + address.toUpperCase( ) + "%"; }
if ( id.equals( "" ) ) { id = "%"; } else { id = "%" + id + "%"; }
if ( phone.equals( "" ) ) { phone = "%"; } else { phone = "%" + phone + "%"; }
if ( fax.equals( "" ) ) { fax = "%"; } else { fax = "%" + fax.toUpperCase( ) + "%"; }
if ( hotelUrl.equals( "" ) ) { hotelUrl = "%"; } else { hotelUrl = "%" + hotelUrl.toUpperCase( ) + "%"; }
if ( airport.equals( "" ) ) { airport = "%"; } else { airport = "%" + airport.toUpperCase( ) + "%"; }
stmt.setString( 1, name ); stmt.setString( 2, address ); stmt.setString( 3, id ); stmt.setString( 4, phone ); stmt.setString( 5, fax ); stmt.setString( 6, hotelUrl ); stmt.setString( 7, airport );
updatableRst = stmt.executeQuery( );
gui.tmodel.clearTable( );
while ( updatableRst.next( ) ) {
String localname = updatableRst.getString( "name" ); String localaddress = updatableRst.getString( "address" ); String localid = String.valueOf( updatableRst.getInt( "id" ) ); String localphone = updatableRst.getString( "phone" ); String localfax = updatableRst.getString( "fax" ); String localhotelUrl = updatableRst.getString( "hotel_url" ); String localairport = updatableRst.getString( "hotel_airport" );
gui.addToJTable( localname, localaddress, localid, localphone, localfax, localhotelUrl, localairport ); }
gui.putStatus( gui.tmodel.getRowCount( ) + " Records selected from database." ); } catch ( SQLException ex ) { gui.putStatus( "Error in selecting from the database " + ex.toString( ) ); } }
private void insertRecord( String name, String address, String id, String phone, String fax, String hotelUrl, String airport ) { try { updatableRst.moveToInsertRow( );
try { updatableRst.updateInt( "id", Integer.parseInt( id ) ); updatableRst.updateString( "name", name ); updatableRst.updateString( "address", address ); updatableRst.updateString( "phone", phone ); updatableRst.updateString( "fax", fax ); updatableRst.updateString( "hotel_url", hotelUrl ); updatableRst.updateString( "hotel_airport", airport );
updatableRst.insertRow( );
gui.clearfields( ); dispatchEvent( "SELECT" );
int rowIndex = gui.hotelTable.getSelectedRow( ); gui.putStatus( "The Record is inserted into the database" ); } catch ( NumberFormatException nfEx ) { gui.putStatus( "id has to be a number" ); } } catch ( Exception ex ) { gui.putStatus( "Error while inserting record in the database. "+ "Error message is: \n" + ex.toString( ) ); } }
private void updateRecord( String name, String address, String id, String phone, String fax, String hotelUrl, String airport ) { try { int rowIndex = gui.hotelTable.getSelectedRow( );
updatableRst.absolute( rowIndex + 1 );
updatableRst.updateInt( "id", Integer.parseInt( id ) ); updatableRst.updateString( "name", name ); updatableRst.updateString( "address", address ); updatableRst.updateString( "phone", phone ); updatableRst.updateString(
"fax", fax ); updatableRst.updateString( "hotel_url", hotelUrl ); updatableRst.updateString( "hotel_airport", airport );
updatableRst.updateRow( );
gui.updateJTable( name, address, id, phone, fax, hotelUrl, airport );
gui.putStatus( "Record is successfully updated " ); } catch ( Exception ex ) { gui.putStatus( "Error while inserting in the database : " + ex.toString( ) ); } }
private void deleteRecord( ) { try { int rowIndex = gui.hotelTable.getSelectedRow( );
updatableRst.absolute( rowIndex + 1 );
updatableRst.deleteRow( );
gui.deleteFromJTable( );
gui.putStatus( "Record is successfully deleted" ); } catch ( Exception ex ) { gui.putStatus( "Error in deleting the row " + '\n' + 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 ); } }
|