/*
 * test_withpass.java - test of JDBC with password and menu
 *
*/

import java.sql.*;
import java.util.Enumeration;
import java.io.*;

public class test_withpass2 {
  public static void main(String [] args) {
    final String tnspre = "jdbc:oracle:oci8:@";
   // String user = "123"; //edit this
   // String pass = "123"; //edit this
       String user, pass;
       user = readEntry("userid : ");
       pass = readEntry("pass: ");
    
    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), field2 number(5))");
      boolean exitflag = false;
      while ( exitflag == false)
      {    
      	// print a little menu 
	System.out.println("");
      	System.out.println("");
	System.out.println("(a) insert this is a test");
      	System.out.println("(b) insert this is a silly test");
	System.out.println("(c) exit");
     	 String ch = 
      	          readEntry("Type in your option: ").toLowerCase();
   
     	 switch(ch.charAt(0))
               {
  	        case 'a':
   	          //then add a row to the table
   	            stmt.execute("INSERT INTO jdbctest VALUES ('This is a test',5)");
  	             break;     
 	          case 'b':
 	            //then add a row to the table
	              stmt.execute("INSERT INTO jdbctest VALUES ('This is a silly test',3)");
	              break;
	           case 'c':
		// exit
		exitflag = true;
		break;
                         default:
		break;
	   }    
        
	if (exitflag == false)
 	{
  	  //now fetch that row
   	   ResultSet result = stmt.executeQuery("SELECT * FROM jdbctest");
      
	      //output column 'text' of result set; I know it's a String.

	   while 	    (  result.next())
	   {
      	
	      System.out.print("Result: ");
	      System.out.println(result.getString("text") + ", " + result.getString("field2"));
                  }

    	      ch = 
      	          readEntry("Press enter to continue: ").toLowerCase();
     		 //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);
    }
}  
   static String readEntry(String prompt) {
    try 
    {
        StringBuffer buffer = new StringBuffer();
        System.out.print(prompt);
        System.out.flush();
        int c = System.in.read();
        while(c != '\n' && c != -1) 
        {
            buffer.append((char)c);
            c = System.in.read();
        }
        return buffer.toString().trim();
    }
    catch (IOException e) 
    {
         return "";
    }


}
}

