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; String sDriver, sDriverKey = "CSDriver", sPassword, sQuery = "SELECT * FROM ", srbName = "ConnectJ", sTable, sURL, sURLKey="CSURL", sUserID; public ConnectJ() { super("ConnectJ"); try // get the PropertyResourceBundle { rbConnect = ResourceBundle.getBundle( srbName ); sDriver = rbConnect.getString( sDriverKey ); sURL = rbConnect.getString( sURLKey ); } 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 { rs = stmt.executeQuery( sQuery + sTable ); boolean bFirst = true; StringBuffer sb = new StringBuffer(30); while( rs.next() ) // if data was returned { if( bFirst ) { rsmd = rs.getMetaData(); iColumnCount = rsmd.getColumnCount(); for( int i = 1; i <= iColumnCount; i++ ) { sb.append( rsmd.getColumnLabel( i ) + "\t" ); } jta.append( sb.toString() + "\n"); sb.replace( 0, sb.length(), "" ); bFirst = false; } // end if( bFirst ) for( int i = 1; i <= iColumnCount; i++ ) { sb.append( rs.getObject( i ) + "\t" ); } jta.append( sb.toString() + "\n"); sb.replace( 0, sb.length(), "" ); } // 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) { 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