Java/MySQL 准备语句错误

标签 java mysql

在将其标记为重复之前,请注意,我查看了 stackoverflow 上有关空指针异常的线程。不幸的是,我还没有解决这个问题,如果有人能指出我正确的方向,我将不胜感激,因为我已经被困了很长一段时间了。

我收到错误

java.lang.NullPointerException at sample.LoginWindow.setBtnLogin(LoginWindow.java:39)) point at the following line preparedStatement.setString(1, txtUserName.getText()

我有课DBUtilities它保存我的数据库连接和一些关闭方法:

 public class DBUtilities {
    // Getting connection to the database.
    public static Connection getConnection() throws SQLException {
        String url = "jdbc:mysql://localhost:3306/studentmanagementdb";
        String user = "admin";
        String pass = "administrator";
        Connection connection = DriverManager.getConnection(url, user, pass);
        return connection;
    }
    // Close prepared statement.
    public static void closePreparedStatement(PreparedStatement pStmt) {
        try {
            if(pStmt != null) {
                pStmt.close();
            }
        }catch(SQLException sExepction) {
            showErrorMsg("SQL Database Error:", "Prepared Statement could not be closed.");
        }
    }
    // Close result set.
    public static void closeResultSet(ResultSet resSet) {
        try {
            if (resSet != null){
                resSet.close();
            }
        }catch (SQLException sException) {
            showErrorMsg("SQL Database Error:", "Result Set could not be closed.");
        }
    }
    public static void closeConnection(Connection connect) {
        try {
            if(connect != null) {
                connect.close();
            }
        }catch (SQLException sException) {
            showErrorMsg("SQL Database Error:", "Database Connection could not be close.");
        }
    }
    // Shows error message.
    public static void showErrorMsg(String title, String msg) {
        Platform.runLater(() -> {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle(title);
            alert.setHeaderText(msg);
            alert.setWidth(200);
            alert.setHeight(200);
            alert.showAndWait();
        });
    }
}

使用连接的类:

public class LoginWindow implements Initializable{

    @FXML
    private TextField txtUserName;
    @FXML
    private PasswordField txtPassword;
    @FXML
    private Button btnLogin;

    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    DBUtilities dbUtil =  new DBUtilities();



    // Setting the login button.
    @FXML
    private void setBtnLogin(ActionEvent event) {

        try {
            connection = dbUtil.getConnection();
            String sqlQuery = "SELECT * FROM 'User_Login_Details' WHERE 'User_Name' = ? AND 'User_Password' = ?";
            connection.prepareStatement(sqlQuery);
            preparedStatement.setString(1, txtUserName.getText());
            preparedStatement.setString(2, txtPassword.getText());
            resultSet = preparedStatement.executeQuery();

        }catch (Exception exception) {
            //dbUtilities.showErrorMsg("Database Connection Error:", "Could not connect to the database.");
            exception.printStackTrace();
        }finally {
            dbUtil.closePreparedStatement(preparedStatement);
            dbUtil.closeResultSet(resultSet);
            dbUtil.closeConnection(connection);
        }
    }
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        btnLogin.setOnAction(this::setBtnLogin);
    }
}

最佳答案

preparedStatement = connection.prepareStatement(sqlQuery);

创建 PreparedStatement用于将参数化 SQL 语句发送到数据库的对象。

关于Java/MySQL 准备语句错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43275365/

相关文章:

java - Spring 社交登录和 Spring Security 自定义身份验证管理器

java - 如何在调用其他代码之前等待备份完成

mysql - SQL 映射表可以加快查询速度吗?你如何使用它们?

MySQL 访问前一行值

mysql替换字符串+下一个字符

java - 需要 Jar 文件来读取响应中返回的 JSON 数组,具有所有这三种类型 JSONObject、JSONParse、JSONArray

java - 我可以在 Java3D 中使用点 Sprite 吗?

java - 在 Spring 中使用 Java 驱动程序的无尽 MongoDB ReplicaSetStatus 更新程序异常

python - 自动增量主键和链接表(Mysql)

mysql - 我试图在我的 ruby​​ on rails 项目中放置一个缩略图,我如何将图像保存在 mysql 中?