import java.awt.*; import java.awt.event.*; import java.sql.*; import java.util.*; import javax.swing.*; public class ScrollResultJ extends JFrame implements ActionListener, WindowListener { static int[] aiValidTypes = { Types.BIGINT, Types.BIT, Types.CHAR, Types.DATE, Types.DECIMAL, Types.DOUBLE, Types.FLOAT, Types.INTEGER, Types.NUMERIC, Types.REAL, Types.SMALLINT, Types.TIME, Types.TIMESTAMP, Types.VARCHAR }; boolean[] abValid; char[] achHeader; char[] achOutput; char[] achUnderline; int[] aiColumnLengths; String[] asColumnLabels; static final int iPAGE_SIZE = 10; static final int iSET_PREV = iPAGE_SIZE * 2; int iPAGE_COUNT, iROW_WIDTH; boolean bFirst = true, bHasNextPage = false, bHasPrevPage = false; Connection con = null; DatabaseMetaData dbmd; int ndx, ndx2; JButton jbConnect = new JButton("Connect"), jbNext = new JButton("Next"), jbPrev = new JButton("Prev"), jbSelect = new JButton("Select"); JFrame jf; JLabel jlOrderBy = new JLabel("Order By:"), jlUserID = new JLabel("UserID:"), jlPassword = new JLabel( "Password:"), jlTable = new JLabel("Table:"); JPanel jpCenter = new JPanel(), jpNorth = new JPanel(), jpNorthCenter = new JPanel( new GridLayout( 3, 2) ), jpSouth = new JPanel( new BorderLayout() ), jpSouthSouth = new JPanel(); JPasswordField jpfPassword = new JPasswordField( 10 ); JScrollPane jsp, jspMD; JTextArea jta = new JTextArea( iPAGE_SIZE + 3, 30 ), jtaMD = new JTextArea( 10, 25 ); JTextField jtOrderBy = new JTextField( 10 ), jtTable = new JTextField( "CoffeeComp", 10 ), jtUserID = new JTextField( 10 ); ResourceBundle rbConnect; ResultSet rsMetaData, rsScroll; ResultSetMetaData rsmd; Statement stmt = null; String sDriver, sDriverKey = "CSDriver", sOrderBy, sPassword, sQuery = "SELECT * FROM ", srbName = "ConnectJ", sTable, sTemp, sURL, sURLKey="CSURL", sUserID; public ScrollResultJ() { super("ScrollResultJ"); addWindowListener( this ); 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 } setLocation( new Point( 50, 100 ) ); jf = new JFrame( "Login" ); jf.addWindowListener( this ); jbConnect.addActionListener( this ); jpNorthCenter.add( jlUserID ); jpNorthCenter.add( jtUserID ); jpNorthCenter.add( jlPassword ); jpNorthCenter.add( jpfPassword ); jpNorthCenter.add( new JLabel( "" ) ); jpNorthCenter.add( jbConnect ); jpNorth.add( jpNorthCenter ); Container cp = jf.getContentPane(); cp.add( jpNorth, BorderLayout.NORTH ); jf.pack(); jbNext.addActionListener( this ); jbNext.setEnabled( false ); jbPrev.addActionListener( this ); jbPrev.setEnabled( false ); jbSelect.addActionListener( this ); jbSelect.setEnabled( false ); jpCenter.add( jbSelect ); jpCenter.add( jlTable ); jpCenter.add( jtTable ); jpCenter.add( jlOrderBy ); jpCenter.add( jtOrderBy ); Font font = jta.getFont(); font = new Font( "Monospaced", font.getStyle(), font.getSize() ); jta.setFont( font ); jta.setEditable( false ); jsp = new JScrollPane( jta, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS ); jpSouthSouth.add( jbNext ); jpSouthSouth.add( jbPrev ); jpSouth.add( jsp, BorderLayout.CENTER ); jpSouth.add( jpSouthSouth, BorderLayout.SOUTH ); cp = getContentPane(); cp.add( jpCenter, BorderLayout.CENTER ); cp.add( jpSouth, BorderLayout.SOUTH ); pack(); Rectangle r = getBounds(); jf.setLocation( r.x + r.width + 20, 100 ); show(); jf.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); // insert code to get Metadata, create a scrollable // ResultSet and set off autocommit // end insert code } catch ( SQLException SQLe) { reportSQLError( SQLe, "problems in DoConnect():" ); if( con != null ) { try { con.commit(); con.close(); } catch( Exception e ) {} stmt = null; } return; } // end catch jf.setVisible( false ); jbSelect.setEnabled( true ); jf.removeWindowListener( this ); jf.setTitle( "MetaData" ); jtaMD.setEditable( false ); jspMD = new JScrollPane( jtaMD ); jpNorth.remove( jpNorthCenter ); jpNorth.add( jspMD ); jf.pack(); jf.show(); } // end doConnect public void doRetrieval() { int iCount = 0, iLength = 0; try { // insert code to get primary key info, // append it to textarea, close rsMetadata // end insert code // insert code to execute the query, // determine that at least one row was returned, // get and display scrolltype - if forward only // display message and return to display. // end insert code // insert code to get ResultSet Metadata // and display Catalog, Schema and Table names // end insert code iCount = rsmd.getColumnCount(); abValid = new boolean[ iCount ]; aiColumnLengths = new int[ iCount ]; asColumnLabels = new String[ iCount ]; // get names, lengths, types, valid for( ndx = 1; ndx <= iCount; ndx++ ) { // get Column Label asColumnLabels[ ndx - 1 ] = rsmd.getColumnLabel( ndx ); aiColumnLengths[ ndx - 1 ] = rsmd.getColumnDisplaySize( ndx ); // use greater of length, label length iLength = asColumnLabels[ ndx - 1 ].length(); if( iLength > aiColumnLengths[ ndx - 1 ] ) { aiColumnLengths[ ndx - 1 ] = iLength; } abValid[ ndx - 1 ] = false; // get the sql.Type int iType = rsmd.getColumnType( ndx ); // Determine if we will report this column for( ndx2 = 0; ndx2 < aiValidTypes.length; ndx2++ ) { if( iType == aiValidTypes[ ndx2 ] ) { abValid[ ndx - 1 ] = true; break; } } // end for ndx2... jtaMD.append( rsmd.getColumnName( ndx ) + ( abValid[ ndx - 1 ] ? "" : " ( not shown, )" ) + " is a Type " + rsmd.getColumnType( ndx ) + " - " + rsmd.getColumnClassName( ndx ) + ", Display Size is " + rsmd.getColumnDisplaySize( ndx ) + ".\n" ); } // end for ndx... // get Row Width for( ndx = 0, iROW_WIDTH = 0; ndx < aiColumnLengths.length; ndx++ ) { if( !abValid[ndx] ) { continue; } iROW_WIDTH += aiColumnLengths[ ndx ] + 1; } iROW_WIDTH -= 1; achHeader = new char[iROW_WIDTH]; achOutput = new char[iROW_WIDTH]; achUnderline = new char[iROW_WIDTH]; blankFill( achHeader ); // get Header for( ndx = ndx2 = 0; ndx < asColumnLabels.length; ndx++ ) { if( !abValid[ndx] ) { continue; } sTemp = asColumnLabels[ ndx ]; sTemp.getChars( 0, asColumnLabels[ ndx ].length(), achHeader, ndx2 ); ndx2 += aiColumnLengths[ ndx ] + 1; } // get Underline for( ndx = 0; ndx < iROW_WIDTH; ndx++ ) { achUnderline[ndx] = '-'; } for( ndx = ndx2 = 0; ndx < aiColumnLengths.length - 1; ndx++ ) { if( !abValid[ndx] ) { continue; } ndx2 += aiColumnLengths[ ndx ]; // can happen due to bypassed columns if( ndx2 >= achUnderline.length ) { break; } achUnderline[ndx2] = ' '; ndx2++; } rsScroll.beforeFirst(); doNext(); // load the first page } // end try catch ( SQLException SQLe) { reportSQLError( SQLe, "problems in DoRetrieve():" ); } } // end doRetrieval public void doNext() { jta.setText( new String( achHeader ) +"\n" ); jta.append( new String( achUnderline ) +"\n" ); try { // insert code to disable/enable "Prev" button, // get and load iPAGE_SIZE rows, // disable/enable "Next" button, // ensure positioning for next page // end insert code } // end try catch ( SQLException SQLe) { reportSQLError( SQLe, "problems in DoNext():" ); } // end catch } // end doNext public void doPrev() { int i = 0; try { // insert code to set cursor for next read // end insert code doNext(); } // end try catch ( SQLException SQLe) { reportSQLError( SQLe, "problems in DoPrev():" ); } // end catch } // end doPrev public void loadRow() throws SQLException { blankFill( achOutput ); for( ndx = 1, ndx2 = 0; ndx <= asColumnLabels.length; ndx++ ) { if( !abValid[ ndx - 1 ] ) { continue; } sTemp = rsScroll.getObject( ndx ).toString(); sTemp.getChars( 0, sTemp.length(), achOutput, ndx2 ); ndx2 += aiColumnLengths[ ndx - 1 ] + 1; } // end for jta.append( new String( achOutput ) +"\n" ); } // end loadRow public void blankFill( char[] ach ) { for( int i = 0; i < ach.length; i++ ) { // blank init ach[ i ] = ' '; } } // end blankFill public void endApp() { if( stmt != null) { try { stmt.close(); } catch( Exception e ) {} } if( con != null) { try { con.commit(); con.close(); } catch( Exception e ) {} } jf.dispose(); dispose(); System.exit(0); } // end endApp // ActionListener implementation public void actionPerformed(ActionEvent e) { Object oSource = e.getSource(); if( oSource == jbNext ) { doNext(); return; } if( oSource == jbPrev ) { doPrev(); return; } if( oSource == jbSelect ) { jta.setText( "" ); sTable = jtTable.getText(); sOrderBy = jtOrderBy.getText(); if( sOrderBy.length() > 0 ) { sOrderBy = "ORDER BY " + sOrderBy; } bHasNextPage = false; jbNext.setEnabled( false ); bHasPrevPage = false; jbPrev.setEnabled( false ); doRetrieval(); return; } if( oSource == jbConnect ) { jta.setText( "No Errors." ); sUserID = jtUserID.getText(); sPassword = jpfPassword.getText(); doConnect(); return; } } // end actionPerformed public void reportSQLError( SQLException SQLe, String s ) { boolean bFirst = true; while( SQLe != null) { if( bFirst ) { SQLe.printStackTrace(); jta.setText( s + "\n" ); bFirst = false; } jta.append( SQLe.getMessage() + "\n" ); jta.append( "SQL State: " + SQLe.getSQLState() + "\n" ); jta.append( "Vendor Error Code: " + SQLe.getErrorCode() + "\n" ); SQLe = SQLe.getNextException(); } bHasNextPage = false; jbNext.setEnabled( false ); bHasPrevPage = false; jbPrev.setEnabled( false ); } // 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 ScrollResultJ(); } // end main } // end class ScrollResultJ