import java.awt.*; import java.awt.event.*; import java.sql.*; import java.util.*; import javax.swing.*; public class ScalarJ extends JFrame implements ActionListener, WindowListener { Connection con = null; DatabaseMetaData dbmd; DefaultComboBoxModel dcbm = new DefaultComboBoxModel(); JButton jbConnect = new JButton("Connect"), jbFN = new JButton( "Get Functions" ), jbDoFn = new JButton( "Get CURDATE" ); JComboBox jcb = new JComboBox( dcbm ); JLabel jlUserID = new JLabel("UserID:"), jlPassword = new JLabel( "Password:"); JPanel jpCenter = new JPanel( new BorderLayout() ), jpCenterEast = new JPanel(), jpCenterWest = new JPanel( new BorderLayout() ), jpCenterWestNorth = new JPanel( new GridLayout( 3,2 ) ), jpCenterWestSouth = new JPanel( new GridLayout( 2,1 ) ), jpNorth = new JPanel( new BorderLayout() ), jpNorthNorth = new JPanel(), jpNorthSouth = new JPanel(); JPasswordField jpfPassword = new JPasswordField( 10 ); JRadioButton jrbNumeric = new JRadioButton( "Numeric" ), jrbString = new JRadioButton( "String" ), jrbTD = new JRadioButton( "Date/Time" ), jrbSystem = new JRadioButton( "System" ); JTextArea jta = new JTextArea( "Errors show here.", 4, 30 ); JTextField jtUserID = new JTextField( 10 ); ResultSet rs = null; Statement stmt = null; ResourceBundle rbConnect; String sDriver, sDriverKey = "CSDriver", sPassword, srbName = "ConnectJ", sURL, sURLKey="CSURL", sUserID; public ScalarJ() { super("ScalarJ"); 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 } jbConnect.addActionListener( this ); jbDoFn.addActionListener( this ); jbFN.addActionListener( this ); jrbTD.setSelected( true ); ButtonGroup bg = new ButtonGroup(); bg.add( jrbNumeric ); bg.add( jrbString ); bg.add( jrbTD ); bg.add( jrbSystem ); jpCenterWestNorth.add( jrbNumeric ); jpCenterWestNorth.add( jrbString ); jpCenterWestNorth.add( jrbTD ); jpCenterWestNorth.add( jrbSystem ); jbFN.setEnabled( false ); jbDoFn.setEnabled( false ); jpCenterWestSouth.add( jbFN ); jpCenterWestSouth.add( jbDoFn ); jpCenterWest.add( jpCenterWestNorth, BorderLayout.NORTH ); jpCenterWest.add( jpCenterWestSouth, BorderLayout.SOUTH ); jcb.setMaximumRowCount( 5 ); jpCenterEast.add( jcb ); jpCenter.add( jpCenterWest, BorderLayout.WEST ); jpCenter.add( jpCenterEast, BorderLayout.EAST ); jpNorthNorth.add( jlUserID ); jpNorthNorth.add( jtUserID ); jpNorthNorth.add( jlPassword ); jpNorthNorth.add( jpfPassword ); jpNorthSouth.add( jbConnect ); JScrollPane jsp = new JScrollPane( jta ); jpNorthSouth.add( jsp ); jpNorth.add( jpNorthNorth, BorderLayout.NORTH ); jpNorth.add( jpNorthSouth, BorderLayout.SOUTH ); Container cp = getContentPane(); cp.add( jpNorth, BorderLayout.NORTH ); cp.add( jpCenter, BorderLayout.CENTER ); 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(); // insert code to get DatabaseMetaData // end insert jbConnect.setEnabled( false ); LoadFunctions(); jbFN.setEnabled( true ); } catch ( SQLException SQLe) { reportSQLError( SQLe, "problems in DoConnect():" ); if( con != null ) { try { con.close(); } catch( Exception e ) {} stmt = null; } return; } // end catch } // end doConnect public void doInvokeFn() { String sTemp1 = null, sTemp2 = null, sTemp3 = null; if( jrbNumeric.isSelected() ) { sTemp1 = "PI is: "; sTemp2 = "PI()"; } else if( jrbString.isSelected() ) { sTemp1 = "LTRIM result is: "; sTemp2 = "LTRIM(' Right')"; } else if( jrbTD.isSelected() ) { sTemp1 = "CURDATE is: "; sTemp2 = "CURDATE()"; } else if( jrbSystem.isSelected() ) { sTemp1 = "USER is: "; sTemp2 = "USER()"; } try { // insert code execute the query, including // scalar syntax, get the returned value // and close the ResultSet // end insert jta.setText( sTemp1 + sTemp3 ); } // end try catch ( SQLException SQLe) { reportSQLError( SQLe, "problems in doInvokeFn():" ); SQLe.printStackTrace(); } } // end doInvokeFn public void LoadFunctions() { String sFunctions = null; StringTokenizer st = null; dcbm.removeAllElements(); try { // insert code to get avalable scalar functions // for the current selection // end insert jbDoFn.setEnabled( false ); if( sFunctions.equals( "" ) ) { dcbm.addElement( "None Returned." ); return; } else { // insert code to get each function name // from the returned String // end insert } if( jrbNumeric.isSelected() ) { jbDoFn.setText( "Get PI" ); if( dcbm.getIndexOf( "PI" ) >= 0 ) { jbDoFn.setEnabled( true ); } } if( jrbString.isSelected() ) { jbDoFn.setText( "Get LTRIM" ); if( dcbm.getIndexOf( "LTRIM" ) >= 0 ) { jbDoFn.setEnabled( true ); } } if( jrbTD.isSelected() ) { jbDoFn.setText( "Get CURDATE" ); if( dcbm.getIndexOf( "CURDATE" ) >= 0 ) { jbDoFn.setEnabled( true ); } } if( jrbSystem.isSelected() ) { jbDoFn.setText( "Get USER" ); if( dcbm.getIndexOf( "USER" ) >= 0 ) { jbDoFn.setEnabled( true ); } } } // end try catch ( SQLException SQLe) { reportSQLError( SQLe, "problems in doInvokeFn():" ); SQLe.printStackTrace(); } } // end LoadFunctions public void endApp() { if( stmt != null) { try { stmt.close(); } catch( Exception e ) {} } if( con != null) { try { con.close(); } catch( Exception e ) {} } dispose(); System.exit(0); } // end endApp // ActionListener implementation public void actionPerformed(ActionEvent e) { Object oSource = e.getSource(); jta.setText( "No Errors." ); if( oSource == jbDoFn ) { doInvokeFn(); return; } if( oSource == jbFN ) { LoadFunctions(); return; } if( oSource == jbConnect ) { sUserID = jtUserID.getText(); sPassword = jpfPassword.getText(); doConnect(); return; } } // end actionPerformed 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 // 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 ScalarJ(); } // end main } // end class ScalarJ