This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12个答案)
2年前关闭。
在将其标记为重复之前,请注意,我已经看过关于stackoverflow上的空指针异常的线程。不幸的是,我还没有解决问题,如果有人能指出正确的方向,我将不胜感激,因为我已经呆了很长时间了。
我收到一个错误
java.lang.NullPointerException在
sample.LoginWindow.setBtnLogin(LoginWindow.java:39))指向
下一行prepareStatement.setString(1,txtUserName.getText()
我有
使用连接的类:
创建一个PreparedStatement对象,用于将参数化的SQL语句发送到数据库。
(12个答案)
2年前关闭。
在将其标记为重复之前,请注意,我已经看过关于stackoverflow上的空指针异常的线程。不幸的是,我还没有解决问题,如果有人能指出正确的方向,我将不胜感激,因为我已经呆了很长时间了。
我收到一个错误
java.lang.NullPointerException在
sample.LoginWindow.setBtnLogin(LoginWindow.java:39))指向
下一行prepareStatement.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语句发送到数据库。
08-17 13:33