/*
 * test.java - test of JDBC stuff
 *
 * based on creation by Kenneth G. Franqueiro
 * on 2/17/04.
 */

import java.sql.*;
import java.util.Enumeration;

public class test {
  public static void main(String [] args) {
    final String tnspre = "jdbc:oracle:oci8:@";
    String user = "pepperk"; //edit this
    String pass = "123"; //edit this

    try { //to catch SQLException
      //the error outputs here sort of explain these steps...
      System.err.println("Registering driver...");
      DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
      System.err.println("Attempting connection...");
      Connection x = DriverManager.getConnection(tnspre, user, pass);
      
      System.err.println("Creating statement...");
      Statement stmt = x.createStatement();
      
      System.err.println("Submitting query...");
      //note no semicolon in the SQL query

      //before starting drop the table
      stmt.execute ("DROP TABLE jdbctest");
      //first set up a testing table
      stmt.execute("CREATE TABLE jdbctest (text varchar2(127))");
      
      //then add a row to the table
      stmt.execute("INSERT INTO jdbctest VALUES ('This is a test')");
      
      //now fetch that row
      ResultSet result = stmt.executeQuery("SELECT * FROM jdbctest");
      
      //output column 'text' of result set; I know it's a String.
      //I also know that in this case, there's only one row.
      
      //the next line is to fix an error that popped up
      //apparently ResultSet's pointer initializes before the first row
      result.next();
      
      System.out.print("Result: ");
      System.out.println(result.getString("text"));
  
      //don't hold up resources:
      result.close();
      x.close();
    } catch (SQLException se) {
      System.err.println("Caught an SQLException!");
      se.printStackTrace();
      System.err.println("Available drivers:");
      Enumeration e = DriverManager.getDrivers();
      while (e.hasMoreElements()) {
        System.err.println(e.nextElement());
      }
      System.exit(1);
    }
  }
}
