/** Save this into a file called 'DatabaseCheck.java' **/

/** 
 * DatabaseCheck.java 
 *    written by Marie Woods. (c) University of Liverpool, 2001
 *
 *  This program may be redistributed or modified but must always contain
 *  this notice. It may not be sold individually or as part of any other
 *  package.
 *
 *  For information about LUSID (Liverpool University Student
 *  Interactive Database) please see http://lusid.liv.ac.uk/
 *
 */
  

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

final public class DatabaseCheck extends HttpServlet {
 
 // **************************************************************** 
 // The following FOUR constants need to be set for your environment
 // Simply fill in your details, compile the servlet:
 //
 // $ javac DatabaseCheck.java
 //
 // install in your servlet zone and invoke the servlet. If all works
 // then you are ready to install LUSID.
 // ****************************************************************
 final private static String DB_URL = "jdbc:oracle:thin:IPaddress:1521:lusl";
 final private static String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
 final private static String DB_PASSWORD = "password";
 final private static String DB_USERNAME = "username";

 private Statement stmt;
 private Connection dbCon;
 private HttpServletRequest req;
 private HttpServletResponse res;
 private PrintWriter out;

 final public void doGet(HttpServletRequest req, HttpServletResponse res)
            throws IOException {

    try {

      res.setContentType("Text/html");
      out = res.getWriter();

      out.println("<HTML>");
      out.println("<HEAD><TITLE>Simple Database Test</TITLE></HEAD>");
      out.println("<BODY BGCOLOR=\"#FFFFFF\">");
      out.println("<CENTER>");
      out.println("<H3><FONT COLOR=#4040FF>Simple Database Test!</FONT></H3>");
      out.println("<BR><BR>");

      // Load DB Driver class
      out.println("<P><FONT COLOR=#C00000><B>Loading DB Driver.............");
      Class.forName(DB_DRIVER);
      out.println("OK</FONT></P></B>");

      out.println("<P><FONT COLOR=#C00000><B>Opening Connection.............");
      dbCon = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
      out.println("OK</FONT></P></B>");   

      createTable();
      doInsert();
      printResultsTable();
      dropTable();
      getSomeDBMetaData();

    } 
    catch(SQLException sqle) {
      out.println("<blockquote><UL><FONT COLOR=#00C000>Get Method - SQL Exception error:</FONT></UL></blockquote>" + sqle);
    }
    catch(IOException ioe) {
      out.println("<blockquote><UL><FONT COLOR=#00C000>Get method - Input/Output Error: </FONT></UL></blockquote>" + ioe);
    }
    catch(ClassNotFoundException cnfe) {
     out.println("<blockquote><UL><FONT COLOR=#00C000>Get method - Class not found error: </FONT></UL> </blockquote>" + cnfe);
    }

    // tidy up
    finally {
      try {
        dbCon.close();
      }
      catch (SQLException sqle) {}
    }  
  }


  final private void createTable() throws SQLException, IOException {

    stmt = dbCon.createStatement();
  
    try {
 
      out.println("<P><B><FONT COLOR=#C00000>Creating Table.............");
      String createCommand = "CREATE TABLE TestTable(id integer, name varchar(64))";
      stmt.execute(createCommand);

      out.println("OK</FONT></P></B>");
    }
    catch (SQLException sqle) {
      out.println("<blockquote><UL><FONT COLOR=#00C000>createTable - Error creating table:</FONT></UL></blockquote>" + sqle + sqle.getMessage());
    }
    catch (Exception e) {
      out.println("<blockquote><UL><FONT COLOR=#00C000>createTable - Error creating table:</FONT></UL></blockquote>" + e + e.getMessage());
    }
    finally {
      try {
        stmt.close();
      }
      catch (SQLException sqle) {}
    } 
  }


  final public void doInsert() throws SQLException, ClassNotFoundException {
    try {

      out.println("<P><FONT COLOR=#C00000><B>Inserting Data into Table.............");
      stmt = dbCon.createStatement();
      String sql = "INSERT INTO TestTable values ('1";
      sql = sql + "', 'Marie Woods')";

      int insertResult = stmt.executeUpdate(sql);

      out.println("OK</P></FONT></B>");
    }
    catch(SQLException sqle) {
      out.println("<blockquote><UL><FONT COLOR=#00C000>doInsert - Error inserting into table:</FONT></UL></blockquote>" + sqle + sqle.getMessage());
    }
    finally {
      try {
        stmt.close();
      }
      catch (SQLException sqle) {}
    }
  }    

  final private void printResultsTable() throws SQLException {
   
    try {

      stmt = dbCon.createStatement();                               
      ResultSet rs = stmt.executeQuery("SELECT * FROM lusid.TestTable");         
                                                                                
      //Print start of table and column headers                              
      out.println("<TABLE CELLSPACING=\"0\" CELLPADDING=\"3\" BORDER=\"1\">");                                                                               
      out.println("<TR><TH>ID</TH><TH>NAME</TH></TR>");                      
                                                                                
      //Loop through results of query.   
      while(rs.next()) { 
        out.println("<TR>");                                                
        out.println("<TD>" + rs.getString("ID") + "</TD>");              
        out.println("<TD>" + rs.getString("Name") + "</TD>");               
        out.println("</TR>");
        out.println("<P></P>");                                               
      }                                                                      

      out.println("</TABLE>");

    }
    catch(SQLException sqle) {
        out.println("<blockquote><UL><FONT COLOR=#00C000>printResultsTable - Error selecting values from the database:</FONT></UL></blockquote>" + sqle +sqle.getMessage());
    }       
    finally {
      try {
        stmt.close();
      }
      catch (SQLException sqle) {}
    }                                                                 
  }


  final private void dropTable() throws SQLException {
    try {

      out.println("<P><FONT COLOR=#C00000><B>Dropping Table.............");
      stmt = dbCon.createStatement();
      String sql = "DROP TABLE testTable";
      int insertResult = stmt.executeUpdate(sql);

      out.println("OK</P></FONT></B>");
    }
    catch(SQLException sqle) {
      out.println("<blockquote><UL><FONT COLOR=#00C000>dropTable - Error dropping table:</FONT></UL></blockquote>" + sqle + sqle.getMessage());
    }
    finally {
      try {
        stmt.close();
      }
      catch (SQLException sqle) {}
    }
  }


  final private void getSomeDBMetaData() throws SQLException {
    try {
      DatabaseMetaData dbMetaData = dbCon.getMetaData();

      out.println("<H3><FONT COLOR=#4040FF>Database Product Information......</FONT></H3>");
      out.println("<B>Product Name:</B> " + dbMetaData.getDatabaseProductName());
      out.println("<BR><B>Version     :</B> " + dbMetaData.getDatabaseProductVersion());
    }
    catch (SQLException sqle) {
      out.println("<blockquote><UL><FONT COLOR=#00C000>getSomeDBMetaData - Error getting metadata information from database!</FONT></UL></blockquote>" + sqle + sqle.getMessage());
    }
  }
}



