Link to home
Start Free TrialLog in
Avatar of g_currier
g_currierFlag for Germany

asked on

Return results from Oracle DB to command line using java

OK, so here's the problem:
Create a DB with transparent encryption - check.
populate it with "sensitive info" - check.
write a java program to access the db and return a query of said sensitive info - half check...

I've got the program.  I'm sure it can be done better (i'm working at my ability level as you can see).  I have hit a roadblock when it comes to printing out the results.  I've tested each sql statement (including the one that's an sqlj token) and they work in sqlplus and sqlDeveloper.  What the program is not doing however, is printing the results.  It runs up to the while loop and ends.  No printout whatsoever.  Line 86 is the last line printed than it returns to a prompt.  

Apart from developing th rest of the code to prevent sql injection and to also obscure the password entry input, I have no idea what to do as everything looks ok to me.  Would someone be so kind as to look over my work and tell me where I made a wrong turn (even if it's the whole thing)?

Thanks


 
/*
 * Glen Currier
 * Homework assignment 9
 * Prof. Jimmy Robertson
 * gcurrierhw9.sqlj
*/

import java.sql.SQLException;
import java.io.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.Scanner;
import oracle.sqlj.runtime.Oracle;

public class gcurrierhw9 {
    public static final String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
    public static final String URL = "jdbc:oracle:thin:@localhost:1521:academic";
    public static final String username = "cmis420";
    public static final String password = "cmis420";
    Connection con = null;
    ResultSet rs = null;
    Statement stmt = null;
    
    
    public static void main(String args[]) throws SQLException,ClassNotFoundException,
                            IOException {
        gcurrierhw9 myhw9 = new gcurrierhw9();
        myhw9.dbSYSLogin();
    }
    
    void  dbSYSLogin () throws SQLException, ClassNotFoundException,
                             IOException {
        gcurrierhw9 myhw9 = new gcurrierhw9();  
        try{
             Oracle.connect(gcurrierhw9.class, "connect.properties");
            
            #sql{alter system set wallet open identified by "Slap_U_S1lly"};
        }
        catch(SQLException e){
            System.err.println("Error while altering wallet"+e);
        }
        finally {              
            try{
                Oracle.close(); 
                myhw9.dbUserLogin();
            }
            catch (SQLException e){
                System.out.println("Error Closing first connection to the Database: " + e);
            }
        }
    }
    
    private void dbUserLogin() throws SQLException, ClassNotFoundException, IOException {
        boolean readOnly = true;
        try{
            Class.forName(DRIVER);
            con = DriverManager.getConnection(URL,username,password);
            try{
                con.setReadOnly(readOnly);
                stmt = con.createStatement();
                readOnly = con.isReadOnly();
               
                try{
                    if (!readOnly){
                        System.out.println("DB Connection is not read only. Terminating program.");
                        con.close();
                        
                    }
                        else if(readOnly){
                            Scanner input = new Scanner(System.in);
                            System.out.print("Please enter your username and password.\n");
                            System.out.println("User name :");
                            String uname = input.next();
                            System.out.println("Password :");
                            String pword = input.next();
                            String[] info={uname,pword};
                            //System.out.println(info[0]+" "+info[1]);
                            
                            rs = stmt.executeQuery("SELECT username,password,chargecardnumber" +
                                " FROM studentaccounts" +
                                " WHERE username=' "+info[0]+" ' AND password=' "+info[1]+" ' ");
                            System.out.println("Your Personal info:\n" +
                                                   "-------------------------------------");
                            while(rs.next()){
                                System.out.println("User Name:   " + rs.getString(1)+"\n");
                                System.out.println("Password:      " + rs.getString(2)+"\n");
                                System.out.println("Card Number:" + rs.getString(3));
                                
                            }
                            input.close();
                        }
                   
                }
                catch (Exception e){
                    System.out.println("Error in dbUserLogin Method: "+e);
                }
            }
            catch(Exception e){
                System.out.println(e);
            }
            stmt.close();
            rs.close();
            con.close();
        }
        catch(Exception e){
            System.out.println(e);
        }
        stmt = null;
        rs = null;
        con = null;

    }
}

Open in new window

Avatar of Mick Barry
Mick Barry
Flag of Australia image

>     public static final String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
>     public static final String URL = "jdbc:oracle:thin:@localhost:1521:academic";

driver does not match the connection string
Avatar of g_currier

ASKER

You are right about the driver but it still does the same thing.  I altered the code to obscure the password input with console.readPassword.  THAT result makes me enter the password twice before it continues and then doesn't display query reslts...

/*
 * Glen Currier
 * Homework assignment 9
 * Prof. Jimmy Robertson
 * gcurrierhw9.sqlj
*/

import java.sql.SQLException;
import java.io.*;
import java.io.Console;
import java.util.Arrays;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.Scanner;
import java.util.logging.ConsoleHandler;

import oracle.sqlj.runtime.Oracle;

public class gcurrierhw9 {
    public static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
    public static final String URL = "jdbc:oracle:thin:@localhost:1521:academic";
    public static final String username = "cmis420";
    public static final String password = "cmis420";
    Connection con = null;
    ResultSet rs = null;
    Statement stmt = null;
    
    
    public static void main(String args[]) throws SQLException,ClassNotFoundException,
                            IOException {
        gcurrierhw9 myhw9 = new gcurrierhw9();
        myhw9.dbSYSLogin();
    }
    
    void  dbSYSLogin () throws SQLException, ClassNotFoundException,
                             IOException {
        gcurrierhw9 myhw9 = new gcurrierhw9();  
        try{
             Oracle.connect(gcurrierhw9.class, "connect.properties");
            
            #sql{alter system set wallet open identified by "Slap_U_S1lly"};
        }
        catch(SQLException e){
            System.err.println("Error while altering wallet"+e);
        }
        finally {              
            try{
                Oracle.close(); 
                myhw9.dbUserLogin();
            }
            catch (SQLException e){
                System.out.println("Error Closing first connection to the Database: " + e);
            }
        }
    }
    
    private void dbUserLogin() throws SQLException, ClassNotFoundException, IOException {
        boolean readOnly = true;
        try{
            Class.forName(DRIVER);
            con = DriverManager.getConnection(URL,username,password);
           
                        
            try{
                con.setReadOnly(readOnly);
                stmt = con.createStatement();
                readOnly = con.isReadOnly();
               
                try{
                    if (!readOnly){
                        System.out.println("DB Connection is not read only. Terminating program.");
                        con.close();
                     }
                        else if(readOnly){
                            System.out.print("Please enter your username and password.\n");
                            Console console = System.console();
                            String uname = console.readLine("User Name? ");
                            char[] pword = console.readPassword("Password? ");
                            try{
                                if ((console.readLine()) != null && (console.readPassword()) != null) {
                                    Arrays.fill(pword, ' ');
                                    rs = stmt.executeQuery("SELECT username,password,chargecardnumber" +
                                                           " FROM studentaccounts" +
                                                           " WHERE username=' "+uname+" ' AND password=' "+pword+" ' ");
                                    System.out.println("Your Personal info:\n" +
                                                       "-------------------------------------");
                                    while(rs.next()){
                                        System.out.println("User Name:   " + rs.getString(1)+"\n");
                                        System.out.println("Password:      " + rs.getString(2)+"\n");
                                        System.out.println("Card Number:" + rs.getString(3));
                                        }    
                                }
                            }
                            catch(Exception e){
                                System.out.println(e);
                            }
                        }
                   
                }
                catch (Exception e){
                    System.out.println("Error in dbUserLogin Method: "+e);
                }
            }
            catch(Exception e){
                System.out.println(e);
            }
            stmt.close();
            rs.close();
            con.close();
        }
        catch(Exception e){
            System.out.println(e);
        }
        stmt = null;
        rs = null;
        con = null;

    }
}

Open in new window

>                                                            " WHERE username=' "+uname+" ' AND password=' "+pword+" ' ");

if you're using a character array for password then that also needs to change to


                                                           " WHERE username=' "+uname+" ' AND password=' "+new String(pword)+" ' ");


(And will only find a record if one exists with a blank password)
This is nuts...what you're saying makes sense once I see it, but it is simply not wanting to work.  I get the same result:  Enter the password once, twice (nothing) and on the third it prints the title line.  I am going to try a DSN to the local DB (that connection I KNOW works).
 
try{
                con.setReadOnly(readOnly);
                stmt = con.createStatement();
                readOnly = con.isReadOnly();

               
                try{
                    if (!readOnly){
                        System.out.println("DB Connection is non-existent. Terminating program.");
                        con.close();
                     }
                        else if(readOnly){
                            System.out.print("Please enter your username and password.\n");
                            Console console = System.console();
                            String uname = console.readLine("User Name? ");
                            char[] pword = console.readPassword("Password? ");
                            try{
                                if ((console.readLine()) != null && (console.readPassword()) != null) {
                                    Arrays.fill(pword, ' ');
                                    rs = stmt.executeQuery("SELECT username,password,chargecardnumber" +
                                                           " FROM studentaccounts" +
                                                           " WHERE username=' "+uname+" ' AND password=' "+new String(pword)+" ' ");
//                                  " WHERE username='jsmith ' AND password='ABC123** ' ");
                                    System.out.println("Your Personal info:\n" +
                                                       "-------------------------------------");
                                    while(rs.next()){
                                        System.out.println("User Name:   " + rs.getString(1)+"\n");
                                        System.out.println("Password:      " + rs.getString(2)+"\n");
                                        System.out.println("Card Number:" + rs.getString(3));
                                        }    
                                }
                            }
                            catch(Exception e){
                                System.out.println(e);
                           }
                        }
                   
                }
                catch (Exception e){
                    System.out.println("Error in dbUserLogin Method: "+e);
                }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think I just saw two parts to the problem.  First the DSN connection worked, which means my oracle drivers aren't where my machine believes them to be (env vars).  No matter, i can run with the DSN connection for now.
THe second is this error I get after the db accepts the three password inputs (for each result set presumably):
java.sql.SQLException: ResultSet is closed

...after the title line with no line number to indicate.
returns all rows without the user name and password (or rather, with, but just as junk data to get past the conditions.

http://www.screencast.com/users/gcurrier/folders/Jing/media/31ace149-ff75-423d-9478-32de434fcd18
                           char[] pword = console.readPassword("Password? ");
                            try{
                                if ((console.readLine()) != null && (console.readPassword()) != null) {


you call readPassword() twice
This after making the changes:

http://www.screencast.com/users/gcurrier/folders/Jing/media/65fa423d-60ed-4917-af0e-b803f4ad7509
 I added a line to output the vars (to see what would be returned)  Pass word is only entered twice this time...
else if(readOnly){
                            System.out.print("Please enter your username and password.\n");
                            Console console = System.console();
                            String uname = console.readLine("User Name? ");
                            char[] pword = console.readPassword("Password? ");
                            try{
                                if ((console.readLine()) != null) {
                                    Arrays.fill(pword,' ');
                                    rs = stmt.executeQuery("SELECT username,password,chargecardnumber" +
                                                           " FROM studentaccounts");
                                    System.out.println(uname+" "+new String(pword));
                                                           //" WHERE username=' "+uname+" ' AND password=' "+new String(pword)+" ' ");
//                                  " WHERE username='jsmith ' AND password='ABC123** ' ");
                                    System.out.println("Your Personal info:\n" +
                                                       "-------------------------------------");
                                    while(rs.next()){
                                        System.out.println("User Name:   " + rs.getString(1)+"\n");
                                        System.out.println("Password:      " + rs.getString(2)+"\n");
                                        System.out.println("Card Number:" + rs.getString(3)+"\n");
                                        System.out.println("-------------------------------------");
                                        }
                                        
                                }
                            }
                            catch(Exception e){
                                System.out.println(e);
                           }

Open in new window