2017-04-07 96 views
0

在將其標記爲重複之前,請注意,我查看了關於在stackoverflow上存在的空指針異常的線程。不幸的是,我還沒有設法解決這個問題,如果有人能指引我走向正確的方向,我會感激不盡,因爲我現在陷入了一段時間。Java/MySQL準備語句錯誤

我正在一個錯誤

顯示java.lang.NullPointerException在 sample.LoginWindow.setBtnLogin(LoginWindow.java:39))點在 以下行了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); 
    } 
} 
+0

'preparedStatement' not initialised – Satya

+0

PreparedStatement preparedStatement = null;然後connection.prepareStatement(sqlQuery);.這是不夠的?對不起,我是mySQL的新手。 – helloumarian

+1

它返回一個PreparedStatement。你需要捕捉它。 https://docs.oracle.com/javase/7/docs/api/java/sql/Connection.html#prepareStatement(java.lang.String) –

回答

1
preparedStatement = connection.prepareStatement(sqlQuery); 

創建用於發送參數的SQL語句到數據庫的PreparedStatement對象。

+0

非常感謝! – helloumarian

0

初始化準備語句,如下

preparedStatement時=各種Connection.prepareStatement(的SQLQuery);

+0

雖然這段代碼片段是受歡迎的,並且可能會提供一些幫助,但它會[如果它包含一個解釋](/ meta.stackexchange.com/q/114762)* [如何解決該問題]會[大大改進]。沒有這些,你的答案就沒有什麼教育價值了 - 記住,你正在爲將來的讀者回答這個問題,而不僅僅是現在問的人!請編輯您的答案以添加解釋,並指出適用的限制和假設。 –