/* -- ' Copyright by Erica Andrews aka CyLiKoN JeZuZ aka Li'L CJ, ' January 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. ' This is a simple but very sleek looking GUI created using a combination of ' Swing and Awt. It's job is to query a table in my database called ' siteindex2 and display the results in a TextArea. Due to the nature ' of the query in the RMI implementation this GUI is only prepared to handle ' ResultSets with exactly FIVE columns - to handle other amounts of columns the ' way the ResultSet is printed to the textarea in the DoQuery() method will have ' to be adjusted. ' This GUI was hand-coded in Java by Erica Andrews on Jan. 27, 1999 ' The implementation uses the ' gweMySQLJDBC 0.9.2 driver and MySQL 3.22.9 for database connectivity. -- */ import java.io.*; import java.util.*; import java.sql.*; import java.awt.*; import java.awt.Font; import java.awt.event.*; import javax.swing.*; public class MySQLWindow extends Frame implements WindowListener,MouseListener { //Object definitions JTextField Query = new JTextField("SELECT DISTINCT * FROM thesites,keywords WHERE keyword LIKE '%vrml%' AND thesites.url=keywords.url"); Label QueryLabel = new Label("Query:"); TextArea QueryResults = new TextArea(); JPasswordField Password = new JPasswordField(""); JTextField Username = new JTextField(""); JTextField Database = new JTextField("siteindex2"); Label DatabaseLabel = new Label("Database:"); Label UsernameLabel = new Label("Username:"); Label PasswordLabel = new Label("Password:"); Button DoQuery = new Button("Query!"); Label AppTitle = new Label("MySQL Query Powered By Java"); //Common procedures void addPosition( Component c,String lo,int x,int y,int w,int h ){ //adds component add(c); c.setBounds(x,y,w,h); } void setParameters( Component c,int fr,int fg,int fb,int br,int bg, //set component parameters int bb,String font,int style,int size){ try{ //set if possible c.setForeground(new Color(fr,fg,fb)); c.setBackground(new Color(br,bg,bb)); c.setFont(new Font(font,style,size)); } catch (Exception e){ System.out.print("A Problem Occurred: "+e); } } public MySQLWindow() { //Constructor setTitle("MySQL Query Powered By Java"); setLayout(null); setSize(776,442); setForeground(new Color(254,254,254)); setBackground(new Color(0,186,222)); setFont(new Font("Serif",1,12)); addPosition(Query,"",58,104,525,19); // TextField Query setParameters(Query,0,0,0,0,255,255,"Serif",1,12); addPosition(QueryLabel,"",10,105,44,18); // Label QueryLabel setParameters(QueryLabel,0,0,0,0,186,222,"Serif",0,12); addPosition(QueryResults,"",13,140,751,291); // TextArea QueryResults setParameters(QueryResults,0,0,0,103,216,232,"Serif",1,14); addPosition(Password,"LTLT",462,73,125,19); // TextField Password setParameters(Password,0,0,0,0,254,254,"Serif",1,12); addPosition(Username,"LTLT",260,74,116,19); // TextField Username setParameters(Username,0,0,0,0,254,254,"Serif",1,12); addPosition(Database,"",63,74,119,19); // TextField Database setParameters(Database,0,0,0,0,254,254,"Serif",1,12); addPosition(DatabaseLabel,"",11,75,48,17); // Label DatabaseLabel setParameters(DatabaseLabel,0,0,0,0,186,222,"Serif",0,12); addPosition(UsernameLabel,"",203,75,50,17); // Label UsernameLabel setParameters(UsernameLabel,0,0,0,0,186,222,"Serif",0,12); addPosition(PasswordLabel,"",404,75,50,17); // Label PasswordLabel setParameters(PasswordLabel,0,0,0,0,186,222,"Serif",0,12); addPosition(DoQuery,"LTLT",604,75,157,54); // Button DoQuery setParameters(DoQuery,255,255,255,0,0,0,"Serif",1,30); DoQuery.addMouseListener(this); addWindowListener(this); addPosition(AppTitle,"",2,25,773,43); // Label AppTitle setParameters(AppTitle,254,254,254,0,124,220,"SansSerif",1,31); AppTitle.setAlignment(Label.CENTER); pack(); show (); } // --- End of GUI Constructor // ************** Listener Events ****************** //**** WindowListener methods 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 ) { this.hide (); this.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 ) { // changing the colors of the query button DoQuery to red and white and displaying // the word "Searching..." - will be restord to its normal state AFTER the // query has been execute and the result set has been printed to the textarea. DoQuery.setBackground(Color.white); DoQuery.setForeground(Color.red); DoQuery.setLabel("Searching..."); } public void mouseReleased ( MouseEvent e ) { RunQuery(); } public void RunQuery() { // - the method referred to above for the mouseReleased event // - showing the SELECT query statement in the Textarea "QueryResults" String yourquery = Query.getText(); //- getting the query statement from textfield String yourdb = Database.getText(); //- getting the DB name from textfield Database QueryResults.setText(yourquery+"\n \n"); // display the query statement in the textarea try { // Load the GWE Mysql JDBC driver Class.forName ("gwe.sql.gweMysqlDriver"); String jdbcUrl = "jdbc:mysql://:3306/"+Database.getText(); Connection conn = DriverManager.getConnection (jdbcUrl,Username.getText(),Password.getText()); // 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 int count = 0; // --- used to count the number of results found while (rset.next ()) { String id=rset.getString(1).trim(); String itsgroup=rset.getString(2).trim(); String url=rset.getString(3).trim(); String title=rset.getString(4).trim(); String thedate=rset.getString(5).trim(); count++; // print the data from the result set into the textarea QueryResults.appendText(id+" "+itsgroup+" "+url+" "+title+" "+thedate+"\n\n"); } // - end of while statement QueryResults.appendText("\n\n\n QUERY FOUND "+count+" RESULTS."); // counting results found conn.close(); // Close the connection to the database // Error handling - all errors will be printed into the textarea QueryResults // rather than doing a System.out.print kinda thing } catch(SQLException es) { QueryResults.appendText("A PROBLEM OCCURRED: "+es); } catch(ClassNotFoundException ec) { QueryResults.appendText("A PROBLEM OCCURRED: "+ec); } // --- Changing the Button "DoQuery" back to its original colors and text. ------- DoQuery.setBackground(Color.black); DoQuery.setForeground(Color.white); DoQuery.setLabel("Query!"); } //---------- end of method for mouseReleased event // --- Doing some simple hover/mouse-over color changes to the button, // --- will turn turquoise when the mouse is over the DoQuery button, and return to // --- the normal black and white colors when the mouse leaves the button. public void mouseEntered ( MouseEvent e ) { DoQuery.setBackground(Color.cyan); DoQuery.setForeground(Color.black); } public void mouseExited ( MouseEvent e ) { DoQuery.setBackground(Color.black); DoQuery.setForeground(Color.white); } public static void main(String args[]){ //main routine new MySQLWindow(); } // --- instantiating a new MySQLWindow object } //--- End of Class MySQLWindow