Hi there

I need to create a command line interface in java. I have something that connects to an oracle database and allows me to do a predetermined select steps. I need some sort of java code that will allow me to have a prompt:

Which you like to do?
1. input fields into the table?
2. delete the table
etc....

PLEASE SELECT NUMBER

And then excute the multiple sql commands - I know the commands i just need the structure for the java program - where to put them and how to calll them if the user inputs a value of "1" say. I have an example below - this allows you to just run one command - any ideas how this could be expanded?

import java.sql.*; 

  public class  JDBCEmp
    {
    public static void  main( String args[] ) throws Exception
    {
//           Find the class...
      Class.forName( "oracle.jdbc.driver.OracleDriver" );

   // Open a connection...
       Connection myConnection = DriverManager.getConnection(
           "jdbc: oracle:thin:@oracle:1521: ora1","", "");

       //    Create a statement...
       Statement myStatement = myConnection.createStatement();

      //    Execute a query...
       try
     {
         //    Execute the query...
          myStatement.execute( "select * from EMP where EMPNO between 7499 and 7788" );

          //    Get the result set...
          ResultSet mySet = myStatement.getResultSet();

         //    Advance to the next row...
          while ( mySet.next() )
          { 
             //    Get the data...
             int empno = mySet.getInt( "empno" );
             String ename = mySet.getString( "ename" );
             long salary = mySet.getLong( "sal" );

             //    Print it all out...
             System.out.println( Integer.toString( empno ) + " - " +
                   ename + " - " + Long.toString( salary ) );
          } 
       }
       catch ( SQLException e )
       {
          System.out.println( "SQL Error: " + e.toString() );
       }

       //    Close everything up...
       myStatement.close(); 
       myConnection.close(); 
    }

 }

Edited 3 Years Ago by pyTony: fixed formatting

This article has been dead for over six months. Start a new discussion instead.