2017-04-07 7 views
0

重複としてマークする前に、私がstackoverflowで持っているNULLポインタの例外に関するスレッドを見ていることに注意してください。残念ながら、私は問題を解決することはできませんでした。誰かが私を正しい方向に向けることができれば、私はかなり長い間立ち往生しています。Java/MySQL Prepared Statementエラー

私は 次の行preparedStatement.setString(1、txtUserName.getTextで sample.LoginWindow.setBtnLogin(LoginWindow.java:39))の時点でエラー

のjava.lang.NullPointerExceptionを取得しています

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(); 
     }); 
    } 
} 
:()

私は、データベース接続といくつかのクロージングメソッドを保持するクラスDBUtilitiesを持っています接続を使用しています

クラス:

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'が初期化されていません – 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)。それがなければ、あなたの答えははるかに教育的価値が低くなります。あなたが今質問している人だけでなく、将来の読者の質問に答えていることを忘れないでください!説明を追加するためにあなたの答えを[編集]し、どんな制限と前提が適用されるかを示してください。 –

関連する問題