import java.awt.*; import java.awt.event.*; import java.sql.*; import java.util.*; import javax.swing.*; import com.ibm.eou.swingchart.*; public class ChartJ extends JFrame implements ActionListener, WindowListener { boolean bFirstPass = true; Connection con; int i, iCount, ndx; JButton jb = new JButton("Connect"); JLabel jlUserID = new JLabel("UserID:"), jlPassword = new JLabel( "Password:"); JPanel jpCenter = new JPanel(), jpNorth = new JPanel(), jpSouth = new JPanel(); JPasswordField jpfPassword = new JPasswordField( 10 ); JTextArea jta = new JTextArea( "Errors show here.", 3, 30 ); JTextField jtUserID = new JTextField( 10 ); ResourceBundle rbConnect; ResultSet rs; ResultSetMetaData rsmd; Statement stmt; String sDriver, sDriverKey = "CSDriver", sPassword, sQuery = "SELECT Cups, Type " + "FROM JJJJData " + "ORDER BY Type", sQueryD = "SELECT DISTINCT Type " + "FROM JJJJData", srbName = "ConnectJ", sURL, sURLKey="CSURL", sUserID; // JJJJData value holders int iCups; // alphaworks Chart variables Color[] cuc = { Color.blue, Color.cyan, Color.green, Color.magenta, Color.red, Color.yellow }; double[][] ducValues; String[] sucLegends; UniversalChart ucChart = new UniversalChart(); public ChartJ() { super("ChartJ"); 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( jta ); // Position and add the chart component ucChart.setChartType( Chart.PARALLEL ); ucChart.setShowGridLines( false ); jpSouth.add( ucChart ); Container cp = getContentPane(); cp.add( jpNorth, BorderLayout.NORTH ); cp.add( jpCenter, BorderLayout.CENTER ); cp.add( jpSouth, BorderLayout.SOUTH ); addWindowListener( this ); pack(); ucChart.setVisible( false ); show(); } // end constructor public void doConnect() { String sSaveValue = null, sValue = null; 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) { jta.setText( "problems connecting to " + sURL + ":" ); jta.append( SQLe.getMessage() ); jta.append( "SQL State: " + SQLe.getSQLState() ); if( con != null) { try { con.close(); } catch( Exception e ) {} } return; } // end catch try { bFirstPass = true; i = iCount = ndx = 0; // get number of distinct values rs = stmt.executeQuery( sQueryD ); while( rs.next() ) { ndx++; } // create sized value arrays ducValues = new double[ndx] [1]; sucLegends = new String[ndx]; // retrieve values in order rs = stmt.executeQuery( sQuery ); while( rs.next() ) { // if data was returned iCups = rs.getInt( 1 ); sValue = rs.getString( 2 ); if( bFirstPass ) { sSaveValue = sValue; sucLegends[i] = sValue; bFirstPass = false; } if( !sSaveValue.equals( sValue ) ) { // if the value changed i++; // Safeguard against additions if( i >= ndx ) { jta.append( "Not all values were " + "reported. Please retry." ); break; } sSaveValue = sValue; iCount = 0; sucLegends[i] = sValue; } ducValues[i][0] += iCups; } // end while ucChart.setColors( cuc ); ucChart.setLegends( sucLegends ); ucChart.setValues( ducValues ); if( !ucChart.isVisible() ) { ucChart.setVisible( true ); } } // end try catch ( SQLException SQLe) { jta.append( SQLe.getMessage() ); jta.append( "SQL State: " + SQLe.getSQLState() ); SQLe.printStackTrace(); } finally { try { stmt.close(); } catch( Exception e ) {} try { con.close(); } catch( Exception e ) {} } // end finally clause } // end doConnect public void endApp() { dispose(); System.exit(0); } // ActionListener implementation public void actionPerformed(ActionEvent e) { 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 ChartJ(); } // end main } // end class ChartJ