package oracle.otnsamples.jdbc.dynamicsql;
import java.sql.SQLException; import java.sql.Statement; import java.sql.ResultSet; import java.sql.Connection; import oracle.jdbc.pool.OracleDataSource;
import java.util.Properties; import java.util.ResourceBundle; import java.util.Enumeration;
import java.io.IOException;
public class DynamicSqlSample {
Connection connection = null;
DynamicSqlFrame gui;
public DynamicSqlSample() { gui = new DynamicSqlFrame(this); gui.setVisible(true); }
public static void main(String[] args) { DynamicSqlSample root = new DynamicSqlSample(); root.dbConnection(); if( root.connection != null ) root.displayTableList(); }
public void dispatchEvent( String eventName ) { if( eventName.equals( "DISPLAY_COLUMNS") ) displayColumns();
if( eventName.equals("SELECT") ) selectRecords();
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 displayTableList() { ResultSet resultSet = null; Statement stmt = null; try { stmt = connection.createStatement(); resultSet = stmt.executeQuery(" SELECT table_name FROM user_tables");
gui.tableNameModel.clearTable();
while( resultSet.next() ) {
String tableName = resultSet.getString(1);
gui.addToJTable(tableName); } } catch( SQLException ex ) { gui.statusField.setText( "Error in querying the database " + '\n' + ex.toString()); gui.statusField.setScrollOffset(0); } finally { try { stmt.close(); } catch(SQLException ex) { } } }
private void selectRecords() {
String columnNames = gui.makeStringOfAllColumns();
String whereClause = gui.restrictionTextfield.getText();
StringBuffer query = new StringBuffer( "SELECT " ); query.append( columnNames + " FROM "); query.append( gui.tableName );
if( !whereClause.equals("") ) query.append(" WHERE " + whereClause);
Statement stmt = null; try { gui.putStatus("Selecting Records ...");
stmt = connection.createStatement();
ResultSet resultSet = stmt.executeQuery( new String(query) );
String[] columnValues = new String[gui.selectedColumns.length];
while( resultSet.next() ) { for( int i = 1; i < gui.selectedColumns.length + 1; i++ ) { columnValues[i -1] = ( resultSet.getString(i) == null )? "" :(String )resultSet.getString(i); } gui.addToResultsTable(columnValues); } gui.putStatus("Selection Complete"); gui.restrictionTextfield.setText("");
} catch (SQLException ex) { gui.statusField.setText( "Error in querying the database " + '\n' + ex.toString()); gui.statusField.setScrollOffset(0); } finally { try { stmt.close(); } catch(SQLException ex) { } } }
private void displayColumns() { Statement stmt = null; try { stmt = connection.createStatement(); ResultSet resultSet = stmt.executeQuery( "SELECT column_name FROM user_col_comments "+ "WHERE table_name = '" + gui.tableName + "'");
while( resultSet.next() ) {
String columnName = resultSet.getString(1);
gui.addToJTable1( columnName ); } } catch( SQLException ex ) { gui.statusField.setText( "Error in querying the database " + '\n' + ex.toString()); gui.statusField.setScrollOffset(0); } finally { try { stmt.close(); } catch(SQLException ex) { } } }
private void exitApplication(){ if( connection != null ) { try { connection.close(); } catch( Exception ex ) { gui.putStatus(" Error in closing the connection: "); gui.appendStatus(" " + ex.toString()); } } System.exit(0); } }
|