import java.awt.*; import java.awt.event.*; import java.sql.*; import java.util.*; import javax.swing.*; public class ConnectJ extends JFrame implements ActionListener, WindowListener { Connection con; int iColumnCount; JButton jb = new JButton("Connect"); JLabel jlUserID = new JLabel("UserID:"), jlPassword = new JLabel( "Password:"), jlTable = new JLabel("Table:"); JPanel jpCenter = new JPanel(), jpNorth = new JPanel(), jpSouth = new JPanel(); JPasswordField jpfPassword = new JPasswordField( 10 ); JTextArea jta = new JTextArea( 10, 30 ); JTextField jtUserID = new JTextField( 10 ), jtTable = new JTextField( "JJJJData", 10 ); JScrollPane jsp = new JScrollPane( jta ); ResourceBundle rbConnect; ResultSet rs; ResultSetMetaData rsmd; Statement stmt; // insert String variables here // end insert public ConnectJ() { super("ConnectJ"); try // get the PropertyResourceBundle { // insert code to Access the ResourceBundle and // retrieve the driver name, databaseURL here. // end insert } catch( MissingResourceException mre ) { System.err.println( "ResourceBundle problem for " + srbName + ", program ends." ); System.err.println("Specific error: " + mre.getMessage() ); endApp(); // exit on error } jb.addActionListener( this ); jpNorth.add( jlUserID ); jpNorth.add( jtUserID ); jpNorth.add( jlPassword ); jpNorth.add( jpfPassword ); jpCenter.add( jb ); jpCenter.add( jlTable ); jpCenter.add( jtTable ); jpSouth.add( jsp ); Container cp = getContentPane(); cp.add( jpNorth, BorderLayout.NORTH ); cp.add( jpCenter, BorderLayout.CENTER ); cp.add( jpSouth, BorderLayout.SOUTH ); addWindowListener( this ); pack(); show(); } // end constructor public void doConnect() { try // Attempt to load the JDBC driver { // with newInstance Class.forName( sDriver ).newInstance(); } catch( Exception e ) // error { jta.setText("Failed to load current driver."); return; } // end catch try { con = DriverManager.getConnection ( sURL, sUserID, sPassword); stmt = con.createStatement(); } catch ( SQLException SQLe) { reportSQLError( SQLe, "problems connecting to " + sURL + ":" ); if( con != null) { try { con.close(); } catch( Exception e ) {} } return; } // end catch try { // insert code to query data // end insert boolean bFirst = true; StringBuffer sb = new StringBuffer(30); while( rs.next() ) // if data was returned { if( bFirst ) { // insert first pass code using MetaData to // get and display ColumnCount number of // ColumnLabels in jta. Reset StringBuffer, // set bFirst to false // end insert } // end if( bFirst ) // insert code to obtain ColumnCount number // of columns using getObject, append to // jta and reset StringBuffer. // end insert } // end while( result.next() ) jta.setCaretPosition( 0 ); } // end try catch ( SQLException SQLe) { String s = null; try { s = "Native SQL was: " + con.nativeSQL( sQuery + sTable ); } catch( Exception e ) { /* don't care */ } reportSQLError( SQLe, s ); } finally { try { stmt.close(); } catch( Exception e ) {} try { con.close(); } catch( Exception e ) {} } // end finally clause } // end doConnect public void reportSQLError( SQLException SQLe, String s ) { jta.setText( s + "\n" ); jta.append( SQLe.getMessage() + "\n" ); jta.append( "SQL State: " + SQLe.getSQLState() + "\n" ); } // end reportSQLError public void endApp() { dispose(); System.exit(0); } // ActionListener implementation public void actionPerformed(ActionEvent e) { // insert code to get user keyed info, reset the // textarea and invoke doConnect(). // end insert sTable = jtTable.getText(); if( sTable.equals("") ) { jta.setText( "Table must have an entry." ); return; } sUserID = jtUserID.getText(); sPassword = jpfPassword.getText(); jta.setText( "" ); doConnect(); } // end actionPerformed // Window Listener Implementation public void windowOpened(WindowEvent e) {} public void windowClosing(WindowEvent e) { endApp(); } public void windowClosed(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} // End Window Listener Implementation public static void main (String args[]) { new ConnectJ(); } // end main } // end class ConnectJ