0

我正在创建一个员工登录验证类,我可以在其中将他们的信息插入 mysql 数据库,然后进行验证,我试图只选择行的密码。这是我的代码:

     import java.sql.*;
        import java.sql.PreparedStatement;
        import java.sql.Connection;


    public class Employee 
    {

    private PreparedStatement statement;
    private Connection con;
    private String pass;
    public Employee()
    {   

    }
    public Employee(String firstName, String lastName, String userID, String userPassword, String role )
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
            statement = con.prepareStatement("Insert into employee(firstName, lastName, userID, " +
                    "password, role)values(?, ?, ?, ?, ?)");    

            statement.setString(1, firstName);
            statement.setString(2, lastName);
            statement.setString(3, userID);
            statement.setString(4, userPassword);
            statement.setString(5, role);
            statement.executeUpdate();
            con.close(); 
        }
        catch(Exception e)
        {

        }
    }
    public void setPassword(String userID)
    {
        try
        {
            statement = con.prepareStatement("SELECT * from employee WHERE userID = ?");
            statement.setString(1, userID);
            ResultSet rs = statement.executeQuery();
            while(rs.next()){
                pass = rs.getString(4);
            }
        }
        catch(Exception e)
        {
            System.out.print(e);
        }
    }
    public String getPassword()
    {
        return pass;
    }


    }
and here is my main

        public class TestMain 
        {
        public static void main(String argsp[])
    {
        Employee emp = new Employee();
        emp.setPassword("ecka12");  
    }
    }

我有一个空指针异常,我不知道为什么。

4

3 回答 3

1

您正在使用以下方法创建对象:-

   Employee emp = new Employee();

在您的 0-arg 构造函数中,您尚未初始化connection

public Employee() {

}

所以,当你consetPassword()方法中使用时,你会得到NullPointerException: -

    // con is not initialized
    statement = con.prepareStatement("SELECT * from employee WHERE userID = ?");
    statement.setString(1, userID);

要更正此问题,您需要将以下代码移至0-arg constructor.. 并在您的其他构造函数中: - 添加this()为您的第一条语句..

public Employee() {
    try {
         Class.forName("com.mysql.jdbc.Driver");
         con = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/test","root","");
    } catch (Exception e) {
         e.printStackTrace();
    }
}



public Employee(String firstName, String lastName) {
     this(); // Initialize con..

     try {
        // You don't need to initialize your con here again..
        // Your remaining code.. 
     }
}
于 2012-10-05T12:55:46.940 回答
0

发生空指针异常是因为您有一个额外的构造函数 public Employee() , con 没有初始化,因此导致空指针异常。

祝你好运!

于 2012-10-05T12:59:04.240 回答
0

1.确保在employee table执行代码之前创建了表,因为您还没有在java代码中创建表。

2.Employee(String,,,,)构造函数也没有使用,因为这个Connection没有被实例化,你已经关闭了构造函数本身的连接。因此它可能在搜索表中的数据之前就已经存在。

于 2012-10-05T13:23:21.257 回答