2016-03-29 12 views
0

SQLテーブルにデータを挿入しようとしています。SQL DBにデータを挿入するときにアクションボタンが機能しない - Java

enter image description here

フォームの目的は、データベースにこれらのテキストフィールドの上に書かれているものは何でも入力することです:私はJFrameのを持っています。 私の問題は、を送信すると、を送信すると、何も起こりません。何か不足していますか?

これはSEND要求ボタンの私のコードです:e.printStackTrace(で

private void SendRequestActionPerformed(java.awt.event.ActionEvent evt) {            
    try { 
      Class.forName("com.mysql.jdbc.Driver"); 
      Connection con = (Connection) 
      DriverManager.getConnection("jdbc:mysql://localhost:3306/database","my-user","my-pass"); 
      Statement stmt = con.createStatement(); 

      //execute commands that collects 
      //the text/items from the fields and converts them to strings 
      String MedRole = ofMedRoleComboBox.getSelectedItem().toString(); 
      String PtID = ofPatientIDField.getText(); 
      String Date = ofDateField.getText(); 
      String Time = ofTimeField.getText(); 
      String Purpose = ofPurposeComboBox.getSelectedItem().toString(); 


      // sql query 
      String insertOrdersDB = "INSERT INTO Orders ('PatientID','MedicalRole','Date','Time','Purpose') VALUES ("+MedRole+","+PtID+","+Date+","+Time+","+Purpose+")"; 

      // executing sql query 
      stmt.executeUpdate(insertOrdersDB); 

     } 
     catch (Throwable e) { 
     e.printStackTrace(); 
     } 
} 

)。は:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 
You have an error in your SQL syntax; check the manual 
that corresponds to your MySQL server version for the 
right syntax to use near ''PatientID','MedicalRole','Date','Time','Purpose') VALUES (Nurse,7,2016-04-13,12' at line 1 

私はそれがSQLとは異なる日付の構文だと思っていました。だから私はそれをSQLテーブル形式と一致するように変更しました。しかし、同じエラーがまだあります。

今は時間に関するものだと思いますか?

+1

?また、catchする可能性のある例外のスタックトレースを出力します。 – Berger

+1

e.printStrackTrace()をキャッチブロックに追加して、エラーが発生している場合はそれを取り除き、ここに投稿してください。 –

+0

スタックトレースをcatchブロックに追加する必要があります。おそらくSQL例外がスローされますが、あなたはそれを見ることはできません。 –

答えて

0

私はあなたのコメントとフィードバックのいくつかをフォローしました。それほどありがとうございました。私はまだJavaを学んでいて、私は長い道のりがあることを知っています。

しかし、とにかく、あなたの指導のおかげで、私は自分のフォームを稼働させることができました。

PreparedStatement pstmt = null; 
    try { 
     Class.forName("com.mysql.jdbc.Driver"); 
      Connection con = (Connection) 
     DriverManager.getConnection("jdbc:mysql://localhost:3306/database","my-user","my-pass");  

     String sql = "INSERT INTO Orders (PatientID, MedicalRole, Date, Time, Purpose) VALUES (?,?,?,?,?)";  

     pstmt = con.prepareStatement(sql); 
     pstmt.setInt(1, Integer.parseInt(ofPatientIDField.getText())); 
     pstmt.setString(2, ofMedRoleComboBox.getSelectedItem().toString()); 
     pstmt.setObject(3, ofDateField.getValue()); 
     pstmt.setObject(4, ofTimeField.getText()); 
     pstmt.setString(5, ofPurposeComboBox.getSelectedItem().toString()); 

     pstmt.executeUpdate(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

はそうです、私はを試してみて、それが働いていた私の前でこれを置き換えます!このメソッドは `ActionListener`に関連してどのように

Prepared Statementsを使用するように私に言ってくれてありがとう:) @MadProgrammer

+0

私が逃したものを理解するのを助けた – Kranolabar

関連する問題