2013-01-09 42 views
1

我收到一個錯誤,我不完全明白,它爲什麼顯示出來?
我有一個表單,可以在相關的JTextBoxes中輸入詳細信息,並且應該將這些文本框中的數據發送到我在本地設置的數據庫。com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException錯誤JAVA

完整的錯誤消息

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 ')VALUES'asd','asd',",",",",",",'Home','Female','White 
British' at line 1 

代碼

conn = (Connection) SQLConnect.ConnectDb(); 
    //inserting all values to database 
    String sql = "INSERT INTO w1283057.criminalrecords " 
      + "(FName, MName, Sname, DOB, Address, HPhone, BPhone, MPhone " 
      + "ResidentStatus, Sex, Race, IncidentLocation, Zone, PremiseType " 
      + "DateRecorded, TimeRecorded, Weapons, CrimeOffences) " 
      + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 
    try{ 
     //Declaring all variable that wiil be collected from JTextField 
     pst = conn.prepareStatement(sql); 
     //coverting JText to String 
     String fname = FName.getText(); 
     String mname = MName.getText(); 
     String surname = Surname.getText(); 
     String dob = DOB.getText(); 
     String fullAddress = FullAddress.getText(); 
     String hPhone = HPhone.getText(); 
     String bPhone = BPhone.getText(); 
     String mPhone = MPhone.getText(); 
     String incidentLocation = IncidentLocation.getText(); 
     String incidentZone = IncidentZone.getText(); 
     String incidentPrType = IncidentPrType.getText(); 
     String incidentDate = IncidentDate.getText(); 
     String incidentTime = IncidentTime.getText(); 
     String incidentWeapons = IncidentWeapons.getText(); 
     String crimeOffences = CrimeOffences.getText(); 
     String radioText = ""; 

     //collecting input data and assign them to variables    
      pst.setString(1, fname); 
      pst.setString(2, mname); 
      pst.setString(3, surname); 
      pst.setString(4, dob); 
      pst.setString(5, fullAddress); 
      pst.setString(6, hPhone); 
      pst.setString(7, bPhone); 
      pst.setString(8, mPhone); 
      //radio buttons Resident Status 
      if(RHome.isSelected()) 
      { 
       radioText = RHome.getText(); 
       pst.setString(9, radioText); 
      } 
      else if(RForeign.isSelected()) 
      { 
       radioText = RForeign.getText(); 
       pst.setString(9, radioText); 
      } 
      //radio buttons Sex 
      if(Male.isSelected()) 
      { 
       radioText = Male.getText(); 
       pst.setString(10, radioText); 

      } 
      else if(Female.isSelected()) 
      { 
       radioText = Female.getText(); 
       pst.setString(10, radioText); 
      } 
      //radio button Race 
      if(WhiteBritish.isSelected()) 
      { 
       radioText = WhiteBritish.getText(); 
       pst.setString(11, radioText); 

      } 
      else if(WhiteOther.isSelected()) 
      { 
       radioText = WhiteOther.getText(); 
       pst.setString(11, radioText); 
      } 
      else if(Black.isSelected()) 
      { 
       radioText = Black.getText(); 
       pst.setString(11, radioText); 
      } 
      else if(Asian.isSelected()) 
      { 
       radioText = Asian.getText(); 
       pst.setString(11, radioText); 
      } 
      else if(Indian.isSelected()) 
      { 
       radioText = Indian.getText(); 
       pst.setString(11, radioText); 
      } 
      ///////////////////////////////////// 
      pst.setString(12, incidentLocation); 
      pst.setString(13, incidentZone); 
      pst.setString(14, incidentPrType); 
      pst.setString(15, incidentDate); 
      pst.setString(16, incidentTime); 
      pst.setString(17, incidentWeapons); 
      pst.setString(18, crimeOffences); 
      pst.executeUpdate(); 
      System.out.println("records added sucessfully"); 

    } 
    catch (SQLException a) 
     { 
      JOptionPane.showMessageDialog(null, a); 
     } 

enter image description here

+0

我也建議不要在你的sql中使用單引號。 – Smit

+0

準備的聲明將照顧報價要求 – Bryan

+0

@BryanMoyles我知道它沒有太大的區別,但它提供了簡單的閱讀。 – Smit

回答

4

您的代碼

String sql = "INSERT INTO w1283057.criminalrecords " 
     + "(FName, MName, Sname, DOB, Address, HPhone, BPhone, MPhone " 
     + "ResidentStatus, Sex, Race, IncidentLocation, Zone, PremiseType " 
     + "DateRecorded, TimeRecorded, Weapons, CrimeOffences) " 
     + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 

嘗試......之後的字段(MPhone,PremiseType)缺乏逗號

String sql = "INSERT INTO w1283057.criminalrecords " 
     + "(FName, MName, Sname, DOB, Address, HPhone, BPhone, MPhone, " 
     + "ResidentStatus, Sex, Race, IncidentLocation, Zone, PremiseType, " 
     + "DateRecorded, TimeRecorded, Weapons, CrimeOffences) " 
     + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 

問候。

+0

我們已經想出了正確的答案:)我只是在等待有人提交它,供大家參考:)所以謝謝 –

5

你必須在你的價值列表的末尾逗號,擺脫它,或用適當的填充它值。

+ "DateRecorded, TimeRecorded, Weapons, CrimeOffences,) " <--- 
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,)"; <---- 
+0

感謝您的回覆,我按照您的說法完成了這些操作,但仍然出現此錯誤 –

+0

@MaciejCygan檢查您是否在預編譯語句中列出了與列出的列名相同的列名。我的意思是你應該有18個列名和'?',如你的代碼所述。 – Smit

+0

你能否更新你的代碼來顯示你目前有什麼?我必須同意@smit並假設你的列數不匹配,或者你沒有從我的答案中刪除結尾的逗號。 – Bryan

相關問題