package oracle.otnsamples.jdbc.stcollection;
import java.util.Properties; import java.util.ResourceBundle; import java.util.Enumeration; import java.io.IOException; import java.sql.Statement; import java.sql.ResultSet; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Connection;
import oracle.jdbc.driver.OracleResultSet; import oracle.jdbc.pool.OracleDataSource;
public class STCollectionSample { private Connection connection = null; private STCollectionFrame gui; public STCollectionSample() { gui = new STCollectionFrame( this ); gui.setVisible( true ); }
public static void main( String[] args ) { STCollectionSample rootFrame = new STCollectionSample(); rootFrame.dbConnection(); rootFrame.populateCountries(); }
public void dispatchEvent( String eventName ) { if( eventName.equals( "SELECT CITIES" ) ) { int selectedRow = gui.table.getSelectedRow(); String name = (String)gui.tableModel.getValueAt( selectedRow, 0 ); displayCitiesData( name ); } 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 populateCountries() { gui.putStatus( "Populating Countries ..." ); gui.tableModel.clearTable(); try { Statement stmt = connection.createStatement(); ResultSet rset = stmt.executeQuery( "SELECT Name, Geography, Currency " + "FROM Obj_Table_Countries ORDER BY name" ); while( rset.next() ) { String name = rset.getString( 1 ); String geography = rset.getString( 2 ); String currency = rset.getString( 3 ); gui.addToCountryJTable( name, geography, currency ); } gui.putStatus( "Connected to database and populated all countries" ); gui.appendStatus( "Please choose a country" ); } catch( SQLException ex ) { gui.putStatus( " Error in Querying countries table: " ); gui.appendStatus( " " + ex.toString() ); } } private void displayCitiesData( String country ) { try { gui.putStatus( "Retrieving cities for the selected country ..." ); PreparedStatement pstmt = connection.prepareStatement( "SELECT CITY_LIST FROM OBJ_TABLE_COUNTRIES WHERE NAME = ? " ); pstmt.setString( 1, country );
OracleResultSet rset = (OracleResultSet)pstmt.executeQuery(); java.util.Hashtable mymap = new java.util.Hashtable(); Class obj = Class.forName("oracle.otnsamples.jdbc.stcollection.CityRefs"); mymap.put( "OBJ_TYPE_CITY", obj ); while( rset.next() ) { gui.tableModel1.clearTable(); CityRefs cityList = (CityRefs)rset.getORAData(1, CityRefs.getORADataFactory() ); ObjTypeCityRef[] cityRefs = cityList.getArray(); for( int i = 0;i < cityRefs.length;i = i + 1 ) { ObjTypeCity cityObject = (ObjTypeCity)cityRefs[ i ].getValue(); String name = cityObject.getName().toString(); String state = cityObject.getProvince().toString(); String cityCode = cityObject.getCityCode().toString(); gui.addToCityJTable( name, state, cityCode ); } } pstmt.close(); gui.putStatus( "Retrieving cities for the selected country ...Done" ); } catch( SQLException ex ) { gui.putStatus( " Error in Querying: " ); gui.appendStatus( " " + ex.toString() ); } catch( Exception ex ) { gui.putStatus( " Unknown error: " ); gui.appendStatus( " " + ex.toString() ); } } private void exitApplication() { try { gui.putStatus( "Closing the connection....please wait....." ); if( connection != null ) { connection.close(); } } catch( SQLException ex ) { gui.putStatus( ex.toString() ); } System.exit( 0 ); } }
|