/* -- ' Copyright by Erica Andrews aka CyLiKoN JeZuZ aka Li'L CJ, ' Febuary 2000. No one is permitted to implement this JavaBean ' or any portion of it on their site without the express written consent ' of the author (cylikon@hotmail.com - ./IcedPinky/). ' All rights reserved. ' The purpose of this JavaBean (found in the package JdbcBean.jar) is to ' query and print ResultSets from any database which can be connected to using ' a completely JDBC-compliant driver (This Bean has NOT been tested with ' and is not intended for ODBC-JDBC bridge connections.). To accomodate varying ' numbers of columns in a ResultSet, this Bean uses vectors rather than normal ' ResultSet processing. No one is permitted to use or modify the code below. ' Instead, the Bean itself (JdbcBean.jar) should be used by downloading it at: ' ./ButchWhipAppeal/JdbcBean.jar ' The ONLY modifications permitted for this Bean are through bean editors such ' as Sun's BDK (Beans Development Kit), NetBeans, etc. NOTE: You NEED to ' modify the Bean in a bean editor BEFORE using it, in order to configure it ' to work with your specific database and JDBC driver. Customizable properties ' include: JDBC Driver, Database URL, Password, Username, and colors. By default, ' (until you have made the necessary modifications, the Bean is set to use ' the gweMySQL JDBC driver and a bogus Username/Password combo. This component ' is a Panel, so your best bet for using it is to use a Bean editor tool to ' drop the whole panel onto a new Frame or JFrame, add some Window-Closing handlers ' to the Frame or JFrame, and your set. It is assumed you know how to add ' Window-Closing handlers to an application. However, if you don't, scroll down further ' in this source file to the "WindowListener Methods" section for a brief example. ' Another option would be to turn ' this component into an applet to be viewed by JDK's appletviewer. Some Bean ' editors (such as the BDK) have a quick "make applet" option, which should make ' using this component easy. ' This JavaBean is Freeware and freely available for non-profit use ' provided NO modifications are made to the Bean or source, EXCEPT the modifications ' your JavaBean editor tool permits. In particular, the tiny credits underneath ' the textfield must be left as-is. In addition, NO ONE is permitted to use this ' component and/or source code in any applications or projects which currently or ' will be used for profits of any sort. ' This JavaBean was hand-coded in Java by Erica Andrews on Feb. 2, 2000 ' All rights reserved. -- */ package cylikon.jdbc; import java.io.*; import java.util.*; import java.sql.*; import java.awt.*; import java.awt.Font; import java.awt.event.*; import java.util.Vector; public class JdbcGUI extends Panel implements MouseListener,Serializable { TextArea textscreen; Button doit; TextField query; Label label; Label credits; Panel panel1; Panel panel2; // --- now creating the modifiable properties for the Bean. -------- private String driver = "gwe.sql.gweMysqlDriver"; private String querystring = "SELECT DISTINCT thesites.id,thesites.itsgroup,thesites.title,thesites.url,thesites.thedate FROM thesites,keywords WHERE keyword LIKE '%vrml%' AND thesites.url=keywords.url"; private String connection = "jdbc:mysql://:3306/YourDatabaseName"; private String password = "yourPassword"; private String username = "yourUsername"; public JdbcGUI () { setLayout(null); setBackground(Color.cyan); setForeground(Color.black); setBounds(new Rectangle(5,5,790,570)); textscreen = new TextArea ( 5, 80 ); textscreen.setBackground(Color.blue); textscreen.setForeground(Color.white); textscreen.setBounds(new Rectangle(10,175,772,340)); doit = new Button("Query"); doit.setForeground(Color.black); doit.setBackground(Color.white); doit.addMouseListener( this ); doit.setBounds(new Rectangle(700,95,70,70)); query = new TextField(querystring,30); query.setBackground(Color.cyan); query.setForeground(Color.black); query.setBounds(new Rectangle(30,95,660,20)); credits = new Label("Simple JDBC Query by CJ (cylikon@hotmail.com)"); credits.setAlignment(Label.LEFT); credits.setForeground(Color.black); credits.setBackground(Color.cyan); credits.setFont(new Font("Helvetica", Font.ITALIC, 12)); credits.setBounds(new Rectangle(30,125,500,20)); label = new Label("SQL Query Powered By Java"); label.setAlignment(Label.CENTER); label.setForeground(Color.white); label.setBackground(Color.blue); label.setFont(new Font("Helvetica", Font.BOLD, 26)); label.setBounds(new Rectangle(2,25,790,60)); add (label); add (query); add (credits); add (doit); add (textscreen ); resize ( 300, 200); show (); } // end GUI constructor // --- Set - Get Bean Modification Methods --------------------- // ----- allowing for modification of the JDBC url in DriverManager.getConnection public String getConn() { return connection; } public void setConn(String connect) { connection = connect; } // ------ allowing for modification of the JDBC driver public String getDriver() { return driver; } public void setDriver(String driv) { driver = driv; } // ------ allowing for modification of the default SELECT statement shown on startup public String getQuery() { return querystring; } public void setQuery(String querys) { querystring = querys; } // ------ allowing for modification of username public String getUsername() { return username; } public void setUsername(String user) { username = user; } // ----- allowing for modification of password public String getPassword() { return password; } public void setPassword(String pass) { password = pass; } //********** WindowListener methods ************** // ****************************************************************************** /* You should add your WindowListeners and Window Closing Events to whatever Frame or JFrame you add this whole component to. An easy way to do this is to make the new Frame or JFrame implement WindowListener, then add the lines below to your class file containing the new Frame or JFrame : public void windowActivated ( WindowEvent e ) { } public void windowDeactivated ( WindowEvent e ) { } public void windowOpened ( WindowEvent e ) { } public void windowClosed ( WindowEvent e ) { } public void windowClosing ( WindowEvent e ) { frame.hide (); frame.dispose (); System.exit(0); } public void windowIconified ( WindowEvent e ) { } public void windowDeiconified ( WindowEvent e ) { } */ // ************************************************************************* //**** MouseListener methods public void mouseClicked ( MouseEvent e ) { } public void mousePressed ( MouseEvent e ) { doit.setBackground(Color.red); label.setForeground(Color.red); label.setBackground(Color.white); label.setText("SEARCHING..."); } public void mouseReleased ( MouseEvent e ) { DoQuery(); } // --- This is the actual method that will be executed when the button is pressed & released public void DoQuery() { // - showing the SELECT query statement in the Textarea "textscreen" String yourquery = query.getText(); //- getting the query statement from Textfield textscreen.setText(yourquery+"\n \n"); try { // Load the GWE Mysql JDBC driver Class.forName (driver); Connection conn = DriverManager.getConnection (connection,username,password); // Create a Statement Statement stmt = conn.createStatement (); // Select all columns from the STOCKS table ResultSet rset = stmt.executeQuery (yourquery); // Iterate through the result and print the records ResultSetMetaData rsmd = rset.getMetaData(); int colnum = rsmd.getColumnCount(); // number of columns in the ResultSet int count = 0; Vector v = new Vector(); int rowpos; // --- a number representing the position IN the row before we move to the next row String breakoff = "break"; while (rset.next ()) { rowpos = 1; while (rowpos < colnum+1) { String element=rset.getString(rowpos).trim(); v.addElement(element); // -- adding each string in the row to the Vector rowpos++; // - moving on to the next string in the row } // end of while // --- now adding a string to the Vector which will be used to indicate the end of // --- a row - String break v.addElement(breakoff); count++; } //end of first while clause /* Now printing out the elements in the vector into the textarea named "textscreen" */ int end = v.size(); // number representing the size of the vector int x = 0; // just another number used for increments while (x < end) { // ----- String representing the data in the rows String rowelement = v.elementAt(x).toString(); /* ---- Now, looking for the any strings in the Vector that read "break" (from ---- above, to denote the start of a new row. Every time the word "break" is found in the vector, a new line character "\n" will be printed to the TextArea "textscreen". */ if (rowelement.equals("break")) { textscreen.appendText("\n"); } else { textscreen.appendText(rowelement+" "); } x++; // -- now move on to the next element in the Vector } // ---end of while statement textscreen.appendText("\n\n\n QUERY FOUND "+count+" RESULTS."); conn.close(); } catch(SQLException es) { textscreen.appendText("A PROBLEM OCCURRED: "+es); } catch(ClassNotFoundException ec) { textscreen.appendText("A PROBLEM OCCURRED: "+ec); } doit.setBackground(Color.pink); label.setForeground(Color.white); label.setBackground(Color.blue); label.setText("SQL Query Powered By Java"); } //---------- end of method for MouseReleased public void mouseEntered ( MouseEvent e ) { doit.setBackground(Color.green); } public void mouseExited ( MouseEvent e ) { doit.setBackground(Color.white); } public static void main(String args[]) { new JdbcGUI(); } } // end of Class