我正在练习一些Java编码,并且连接类扩展到登录类时遇到了一些问题。我要执行的操作是提示用户使用用户名和密码登录。 getUser()将存储用户名,而getPassword()将存储用户名的密码。当我运行驱动程序类时,它提示用户输入用户名,但从不提示输入密码。我需要进行哪些更改,因此还将提示您输入密码。
//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();
}
我的下一段代码是连接类。
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
}
}
}
}
答案 0 :(得分:1)
似乎您只是在simpleQuery()方法中调用了getUser()方法。
也尝试在此处调用getPassword()方法。
编辑:类似这样的东西:
public void simpleQuery() throws IOException{
SecurityDriver userObject = new SecurityDriver();
SecurityDriver.getUser();
SecurityDriver.getPassword();
//...rest of code