Search code examples
javajdbciseries-navigator

Java: Connection class extending to login class


I'm practicing some java coding and I'm having some problems with the connection class extending to the login class. What I'm wanting to do is prompt the user to login with a username and password. The getUser() is going to store the username and getPassword() stores the username's password obliviously. When I run the driver class it prompts user for the username, but never prompts for the password. What changes do I need to make so it will also prompt for the password.

//Security class
import java.io.IOException;
import java.sql.SQLException;
import java.util.Scanner;

class SecurityDriver {

public String url = "myurl"; //Don't want to display my url to the public
public static String user;
public static String password;



public static String getUser() {
    Scanner in = new Scanner(System.in);
    System.out.println("Please enter username: ");

    return user = in.nextLine();
}

public String getPassword() {
    Scanner in = new Scanner(System.in);
    System.out.println("Please enter password: ");
    return password = in.nextLine();
}



public static void main(String[] args) throws IOException {

    SecurityTest stObject = new SecurityTest();
    stObject.simpleQuery();
}

My next bit of code is the connection class.

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class SecurityTest extends SecurityDriver{




public void simpleQuery() throws IOException{


    SecurityDriver userObject = new SecurityDriver();
    SecurityDriver.getUser();


    Connection con;
    Statement stmt;
    ResultSet rs;

    try {
        Class.forName("com.ibm.as400.access.AS400JDBCDriver");
        con = DriverManager.getConnection(url, user, password);
        System.out.println("CONNECTED");
    }
    catch (ClassNotFoundException e)
    {
          System.err.println("Could not load JDBC driver");
          System.out.println("Exception: " + e);
          e.printStackTrace();
        }

        catch(SQLException ex)                                              
        {
          System.err.println("SQLException information");
          while(ex!=null) {
            System.err.println ("Error msg: " + ex.getMessage());
            System.err.println ("SQLSTATE: " + ex.getSQLState());
            System.err.println ("Error code: " + ex.getErrorCode());
            ex.printStackTrace();
            ex = ex.getNextException(); // For drivers that support chained exceptions
          }
        }


}

}


Solution

  • It seems you're only calling the getUser() method in your simpleQuery() method.

    Try calling the getPassword() method here also.

    EDIT: Something like this:

    public void simpleQuery() throws IOException{
    
    
        SecurityDriver userObject = new SecurityDriver();
        SecurityDriver.getUser();
        SecurityDriver.getPassword();
    
    //...rest of code