next up previous contents
Next: APPENDIX D: NORMAL FORMS Up: Contents Previous: APPENDIX B: JAVA DATABASE   Contents

APPENDIX C: SQL APPLET [50]

SQLmain.java:

import java.awt.*;
import java.applet.*;
 
public class SQLmain {
  public static void main(String args[]) {
     Applet applet = new SQLApplet();
     Frame frame = new SQLFrame(applet);
   }
}

class SQLFrame extends Frame {

  public SQLFrame(Applet applet) {
    super("Java Interface to SQL");
    MenuBar menubar = new MenuBar();
    Menu file = new Menu("File",true);
    menubar.add(file);
    file.add("Quit");
    this.setMenuBar(menubar);

    this.add("Center",applet);
    this.resize(620,680);
    applet.init();
    this.show();
  }

  public boolean action(Event e, Object arg) {
    if (e.target instanceof MenuItem) {
       String label = (String) arg;
       if (label.equals("Quit")) System.exit(0);
    }
    return false;
  }
}



SQL.html [51]

<html>
<head>
<title>Java Interface to SQL</title>
</head>
<body>
<applet
    code=SQLApplet.class
    id=SQL
    width=620
    height=840 >
</applet>
</html>



SQLApplet.java [52]

import java.awt.*;
import java.applet.*;
import java.net.URL;
import exjava.sql.*;
import exgwe.sql.*;

public class SQLApplet extends Applet {
  Button runsqlbutton, quitbutton;
  TextArea textArea1;
  Label insqllabel;
  Label dblabel;
  Label protolabel;
  Label portlabel;
  Label userlabel;
  Label passlabel;
  Label hostlabel;
  TextField sql1; 
  TextField sql2; 
  TextField sql3; 
  TextField dbfield;
  TextField protofield;
  TextField portfield;
  TextField hostfield;
  TextField userfield;
  TextField passfield;



Applet [53]

  void runsql_Clicked(Event event) {
    // Load the gweMysqlDriver JDBC Driver
    String driverName = "exgwe.sql.gweMysqlDriver";
    // The url of the database we wish to connect to
    String proto = protofield.getText();
    String host  = hostfield.getText();
    String port  = portfield.getText();
    String db    = dbfield.getText();
    String user  = userfield.getText();
    String pass  = passfield.getText();
    String url   = proto + "//" + host + ":" + port + "/" + db;
  
    String query = sql1.getText() + " " + sql2.getText() + " " + 
                   sql3.getText();
  Connection con = null;
  Statement stmt = null;
  ResultSet rs = null;
  ResultSetMetaData md = null;
    try {
      Class.forName(driverName);
      con = DriverManager.getConnection(url,user,pass); 
      stmt = con.createStatement();
      if (stmt.execute(query)) { // true means the SQL was a SELECT
         rs = stmt.getResultSet();
         md = rs.getMetaData(); 
         textArea1.appendText(SQLutil.getRow(rs,md,true)+"\n");
         while(rs.next()) 
           textArea1.appendText(SQLutil.getRow(rs,md,false)+"\n"); 
         textArea1.appendText("\n"); 
         rs.close();
       } 
       stmt.close();
       con.close();
    } catch (Exception e) {
       if (e.getMessage().equals("mysql exception: No columns returned."))
         textArea1.appendText("Update completed.\n");
       else textArea1.appendText(e.getMessage()+"\n");
      }
  }



Applet (continued) [54]

  public void init() {
    super.init();
    setLayout(null);
    dblabel = new Label("Database:"); 
    dblabel.reshape(20,10,60,30);
    add(dblabel);
    dbfield = new TextField(150); 
    dbfield.reshape(90,10,150,30);
    dbfield.setText("suppDB");
    add(dbfield);
    userlabel = new Label("User:"); 
      userlabel.reshape(250,10,60,30);
    add(userlabel);
    userfield = new TextField(100); 
    userfield.reshape(320,10,100,30);
    userfield.setText("any");
    add(userfield);
    passlabel = new Label("Password:"); 
    passlabel.reshape(430,10,62,30);
    add(passlabel);
    passfield = new TextField(100); 
    passfield.reshape(500,10,100,30);
    passfield.setText("any");
    add(passfield);
    hostlabel = new Label("Host:"); 
    hostlabel.reshape(20,50,32,30);
    add(hostlabel);
    hostfield = new TextField(150); 
    hostfield.reshape(60,50,180,30);
    hostfield.setText("gold.mcs.csuhayward.edu");
    add(hostfield);
    protolabel = new Label("Protocol:"); 
    protolabel.reshape(250,50,60,30);
    add(protolabel);
    protofield = new TextField(100); 
    protofield.reshape(320,50,100,30);
    protofield.setText("jdbc:mysql:");
    add(protofield);



Applet (continued) [55]

   
    portlabel = new Label("Port:"); 
    portlabel.reshape(430,50,60,30);
      add(portlabel);
    portfield = new TextField(100); 
    portfield.reshape(500,50,100,30);
    portfield.setText("3333");
    add(portfield);
    insqllabel = new Label("SQL Input:"); 
    insqllabel.reshape(20,90,100,30);
      add(insqllabel);
    sql1 = new TextField(600); 
    sql1.reshape(20,120,580,30);
    add(sql1);
    sql2 = new TextField(600); 
    sql2.reshape(20,160,580,30);
    add(sql2);
    sql3 = new TextField(580); 
    sql3.reshape(20,200,580,30);
    add(sql3);
    textArea1 = new java.awt.TextArea();
    textArea1.reshape(20,270,580,300);
    Font font = new Font("Courier",Font.PLAIN,10);
    textArea1.setFont(font);
    add(textArea1);
    runsqlbutton = new java.awt.Button("Run SQL");
    runsqlbutton.reshape(20,240,70,25);
    add(runsqlbutton);
  }
  public boolean handleEvent(Event event) {
    if (event.target == runsqlbutton && event.id == Event.ACTION_EVENT) {
      runsql_Clicked(event);
    }
    return super.handleEvent(event);
  }
}



SQLutil.java [56]

import exjava.sql.*;
import exgwe.sql.*;

public class SQLutil {
  public static double toDouble(String s) {
  Double d = new Double(0.0);
    try {
      d = Double.valueOf(s);
    } catch(Exception e) {return 0.0;}
    return d.doubleValue(); 
  }
  public static int toInt(String s) {
  Integer i = new Integer(0); 
    try {
      i = Integer.valueOf(s);
    } catch(Exception e) {return 0;}
    return i.intValue(); 
  }
  public static int max(int i, int j) {
    if (i > j) return i; else return j;
  }



SQLutil.java (continued) [57]

public static String getRow(ResultSet rs, ResultSetMetaData md, 
                            boolean getNames) {
  boolean padLeft;
  String d;
    try {
      String s = "";
      String s1 = "";
      for (int i=1; i<=md.getColumnCount(); i++) {
        String name = md.getColumnName(i);
        String d1 = "";
        if (getNames) d = name; else d = rs.getString(i);
        int len = max(md.getColumnDisplaySize(i), name.length());
        String type = md.getColumnTypeName(i);
        if (type.equals("INTEGER") || type.equals("FLOAT") || 
            type.equals("DOUBLE"))
          padLeft = true;
        else 
          padLeft = false; 
        if (getNames) {
          d = pad(d,len," ",padLeft);
          d1 = pad(d1,len,"-",padLeft);
        } else 
          d = pad(d,len," ",padLeft); 
        s = s + d + " ";
        if (getNames) s1 = s1 + d1 + " "; 
      }
      if (getNames) s = s + "\n" + s1;
      return s; 
    } catch( Exception e ) {return e.getMessage();}
  }
  public static String pad(String s, int width, String padChar, 
                           boolean padLeft) {
    for (int i=s.length()+1; i<=width; i++)
      if (padLeft) 
        s = padChar + s; 
      else 
        s = s + padChar;
    return s;
  }
}



Ted Billard 2001-10-29