package oracle.otnsamples.jdbc.columntype;
import java.io.IOException;
import java.sql.SQLException; import java.sql.PreparedStatement; import java.sql.Connection; import java.sql.Types; import java.sql.ResultSet; import oracle.jdbc.driver.OraclePreparedStatement; import oracle.jdbc.pool.OracleDataSource;
import java.util.Properties; import java.util.ResourceBundle; import java.util.Enumeration;
public class ColumnTypeSample { Connection connection = null;
ColumnTypeFrame gui;
int numFetchedRows ;
public ColumnTypeSample() { gui = new ColumnTypeFrame(this); gui.setVisible(true); }
public static void main( String args[] ) { ColumnTypeSample colType = new ColumnTypeSample(); colType.dbConnection(); if( colType.connection != null ) { colType.gui.buttonStateChange(true); } }
public void dispatchEvent(String eventName) { if( eventName.equals("START SELECT") ) selectRecords(gui.flag); 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(); connection.setAutoCommit(false); 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( boolean specifyColumnType ) { long timetaken; numFetchedRows = 0;
PreparedStatement prepare = null;
String query = "SELECT language_id FROM products WHERE ROWNUM < 11"; try { gui.putStatus("Started selecting rows, Please wait..");
prepare = connection.prepareStatement(query);
if ( specifyColumnType ) ((OraclePreparedStatement)prepare).defineColumnType(1,Types.VARCHAR);
long initialTime = System.currentTimeMillis();
ResultSet rst = prepare.executeQuery();
String temp = null;
while ( rst.next() ) { numFetchedRows++; temp = rst.getString(1); }
long finalTime = System.currentTimeMillis();
timetaken = finalTime - initialTime;
gui.displayResult( specifyColumnType, timetaken); gui.putStatus( numFetchedRows + " Rows Selected" ); } catch( SQLException ex ) { gui.putStatus(" Error in Selecting rows "+'\n'+ ex.toString()); } finally { try { prepare.close(); } catch(SQLException ex) { } } }
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); } }
|