next up previous
Next: About this document ... Up: No Title Previous: Formatting HTML Tables

Application - SQL Query Processor

import oracle.html.*;  // Java Cartridge Toolkit classes
import java.sql.*;     // JDBC classes
public class app1jdbc {
  public static void main (String args[]) 
        throws SQLException {
    HtmlHead hd = new HtmlHead("JavaApp Main Page");
    HtmlBody bd = new HtmlBody();
    HtmlPage hp = new HtmlPage(hd, bd);
    hp.printHeader();
    Form form1 = new Form("GET",
              "http://tinman.cs.gsu.edu:9001/"+
              "book2/java/run/app2jdbc");

    try {
      Class.forName ("oracle.jdbc.driver.OracleDriver");
    } catch (ClassNotFoundException e) {
        bd.addItem(
          new SimpleItem("Could not load the driver")); 
        hp.print();
        return;
      }
    Connection conn = DriverManager.getConnection
                      ("jdbc:oracle:oci7:book/book");
    Statement stmt = conn.createStatement ();

    String query = "select db_name from db_list";
    ResultSet rset = stmt.executeQuery(query);

    Select select1 = new Select("dbname");
    while (rset.next ()) { 
      select1.addOption(new Option(rset.getString(1)));
    } 
    form1.addItem(select1);

    Submit submit1 = new Submit("submit1",
                                "Select_A_Database");
    form1.addItem(submit1);

    bd.addItem(form1);
    conn.close();
    hp.print();
  } // end of main
} // end of class app1jdbc

import oracle.html.*;
import java.sql.*;
public class app2jdbc {
  public static void main (String args[]) 
          throws SQLException {
    HtmlHead hd = new HtmlHead("SQL Query Page");
    HtmlBody bd = new HtmlBody();
    HtmlPage hp = new HtmlPage(hd, bd);
    hp.printHeader();

    String sDbname;
    sDbname = getArgument(args,"dbname");
    bd.addItem(new SimpleItem(
                  "Database Schema for ").setBold());
    bd.addItem(new SimpleItem(sDbname).setBold());
    bd.addItem(SimpleItem.LineBreak);
    bd.addItem(SimpleItem.HorizontalRule);
    try {
      Class.forName ("oracle.jdbc.driver.OracleDriver");
    } catch (ClassNotFoundException e) {
        bd.addItem(new SimpleItem(
              "Could not load the driver")); 
        hp.print();
        return;
      }
    Connection conn = DriverManager.getConnection
                      ("jdbc:oracle:oci7:book/book");
    Statement stmt = conn.createStatement (); 
    String query = 
       "select table_name, column_name, data_type " +
       "from db_schema where db_name = '" + sDbname + "'";
    ResultSet rset = stmt.executeQuery(query);

    Form form1 = new Form("POST",
            "http://tinman.cs.gsu.edu:9001/book2/"+
            "java/run/app3jdbcmeta");

    boolean newEntry = false;
    String prev = new String();
    int i=0;
    while (rset.next ()) { 
      if (i == 0) {
        bd.addItem(new SimpleItem(rset.getString(1)));
        bd.addItem(new SimpleItem("("));
        prev = new String(rset.getString(1));
      }
      else if (!prev.equals(rset.getString(1))) {
            bd.addItem(new SimpleItem(");"));
            bd.addItem(SimpleItem.LineBreak);
            bd.addItem(new SimpleItem(rset.getString(1)));
            bd.addItem(new SimpleItem("("));
            prev = new String(rset.getString(1));
            newEntry = false;
           }
      if (newEntry) 
        bd.addItem(new SimpleItem(", "));
      newEntry = true;
      bd.addItem(new SimpleItem(rset.getString(2)));
      bd.addItem(new SimpleItem(":"));
      bd.addItem(new SimpleItem(rset.getString(3)));
      i++;
    } 
    bd.addItem(new SimpleItem(");"));
    bd.addItem(SimpleItem.LineBreak);
    bd.addItem(SimpleItem.HorizontalRule);

  TextField tf1 = new TextField(
                      "select_clause",100,50,""); 
  TextField tf2 = new TextField("from_clause",100,50,""); 
  TextArea ta1 = new TextArea("where_clause",50,10);

  DynamicTable tab = new DynamicTable(2);
  TableRow rows[] = new TableRow[3];
  rows[0] = new TableRow();
  rows[0].addCell(new TableDataCell(new SimpleItem(
                         "SELECT").setBold()))
         .addCell(new TableDataCell(tf1));
  tab.addRow(rows[0]);
  rows[1] = new TableRow();
  rows[1].addCell(new TableDataCell(new SimpleItem(
                         "FROM").setBold()))
         .addCell(new TableDataCell(tf2));
  tab.addRow(rows[1]);
  rows[2] = new TableRow();
  rows[2].addCell(new TableDataCell(new SimpleItem(
                         "WHERE").setBold()))
         .addCell(new TableDataCell(ta1));
  tab.addRow(rows[2]);

    form1.addItem(tab);

    Hidden hdd = new Hidden("dbname",sDbname);
    form1.addItem(hdd);

    Submit submit1 = new Submit();
    form1.addItem(submit1);

    Reset reset1 = new Reset();
    form1.addItem(reset1);

    bd.addItem(form1);
    conn.close();
    hp.print();

  }
} // end of class app2jdbc

import oracle.html.*;
import java.util.*;
import java.sql.*;
public class app3jdbcmeta {
  public static void main (String args[]) 
        throws SQLException, Exception {
    HtmlHead hd = new HtmlHead("Query Results Page");
    HtmlBody bd = new HtmlBody();
    HtmlPage hp = new HtmlPage(hd, bd);
    hp.printHeader();
    String select_string;
    String from_string;
    String where_string_temp;
    String where_string;
    String sDbname;
    sDbname = getArgument(args,"dbname");
    select_string = getArgument(args,"select_clause");
    from_string = getArgument(args,"from_clause");

    where_string_temp = getArgument(args,"where_clause");
    int nn;
    StringBuffer sb = new StringBuffer(
                            where_string_temp.length());
    for (int k = 0; k < where_string_temp.length(); k++) {
        nn = (int) where_string_temp.charAt(k);
        if ((nn == 13) || (nn == 10))
          sb.insert(k,(char) 32); 
        else
          sb.insert(k,where_string_temp.charAt(k));
    }
    where_string = sb.toString();

    // construct entire query from 
    // select_string,from_string, where_string
    String query;
    if (where_string.length() == 0)
      query = "select distinct " + select_string + 
                     " from " + from_string;
    else
      query = "select distinct " + select_string + 
                     " from " + from_string + 
                     " where " + where_string;
    bd.addItem(new SimpleItem("Query is: "))
      .addItem(new SimpleItem(query))
      .addItem(SimpleItem.Paragraph);
    int nCols;
    Link link1,link2;
    try {
      Class.forName ("oracle.jdbc.driver.OracleDriver");
    } catch (ClassNotFoundException e) {
        bd.addItem(new SimpleItem("Error")); 
        hp.print();
        return;
      }
    Connection conn = DriverManager.getConnection
                      ("jdbc:oracle:oci7:book/book");
    Statement stmt = conn.createStatement ();

    ResultSet rset;
    ResultSetMetaData rsetmd;
    try {
      rset = stmt.executeQuery(query);
      rsetmd = rset.getMetaData();
    } catch (SQLException e) {
        bd.addItem(new SimpleItem("Query not OK"))
          .addItem(SimpleItem.Paragraph);
        bd.addItem(SimpleItem.HorizontalRule)
          .addItem(SimpleItem.Paragraph);
        link1 = new Link (
            "http://tinman.cs.gsu.edu:9001/book2/"+
            "java/run/app2jdbc?dbname="
            +sDbname, "Another Query?");
        link2 = new Link (
            "http://tinman.cs.gsu.edu:9001/book2/"+
            "java/run/app1jdbc", 
            "Query Another Database?");
        bd.addItem(link1).addItem(SimpleItem.Paragraph);
        bd.addItem(link2).addItem(SimpleItem.Paragraph);
        hp.print();
        return;
      }

    nCols = rsetmd.getColumnCount();
    DynamicTable tab = new DynamicTable(nCols);
    tab.setBorder(2);
    TableRow rows[] = new TableRow[1000];
    rows[0] = new TableRow();
    for (int i1 = 1; i1 <= nCols; i1++) {
      rows[0].addCell(new TableHeaderCell(
          new SimpleItem(rsetmd.getColumnName(i1))));
    }
    tab.addRow(rows[0]);

    int nRows = 0;
    while (rset.next ()) { 
      rows[nRows] = new TableRow();
      for (int c=1; c <= nCols; c++) {
        rows[nRows].addCell(new TableDataCell(
              new SimpleItem(rset.getString(c))));
      }
      tab.addRow(rows[nRows]);
      nRows++;
    }

    if (nRows > 0) {
      bd.addItem(tab);
      bd.addItem(SimpleItem.Paragraph);
      bd.addItem(new SimpleItem("Number of rows="))
        .addItem(new SimpleItem(nRows))
        .addItem(SimpleItem.Paragraph);
      bd.addItem(SimpleItem.HorizontalRule)
        .addItem(SimpleItem.Paragraph);
      link1 = new Link (
         "http://tinman.cs.gsu.edu:9001/book2/java/"+
         "run/app2jdbc?dbname="
         +sDbname, "Another Query?");
      link2 = new Link (
         "http://tinman.cs.gsu.edu:9001/book2/java/"+
         "run/app1jdbc", 
         "Query Another Database?");
      bd.addItem(link1).addItem(SimpleItem.Paragraph);
      bd.addItem(link2).addItem(SimpleItem.Paragraph);
    }

    if (nRows == 0) {
      bd.addItem(new SimpleItem("No rows in result"))
        .addItem(SimpleItem.Paragraph);
      bd.addItem(SimpleItem.HorizontalRule)
        .addItem(SimpleItem.Paragraph);
      link1 = new Link (
          "http://tinman.cs.gsu.edu:9001/book2/java/"+
          "run/app2jdbc?dbname="
          +sDbname, "Another Query?");
      link2 = new Link (
          "http://tinman.cs.gsu.edu:9001/book2/java/"+
          "run/app1jdbc", 
          "Query Another Database?");
      bd.addItem(link1).addItem(SimpleItem.Paragraph);
      bd.addItem(link2).addItem(SimpleItem.Paragraph);
    }
    hp.print();
  }
} // end of class app3jdbcmeta


Dr. Raj Sunderraman
7/2/1998