2012-12-04 30 views
0

毫秒的存取更新日期*例外:*如何通過NetBeans的

 java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error in  UPDATE statement 

代碼:

String Name = txtName.getText();` 
    String Email = txtEmail.getText(); 
    String Mobile = txtMobile.getText(); 
    String Address = txtAddress.getText(); 
    String Dob = txtDob.getText(); 

嘗試 {

  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 

      connection = DriverManager.getConnection("jdbc:odbc:NewPData"); 
      String query = "update Table1 set Name='" + Name + "' , Email='" + Email + "' , Mobile=" + Mobile + ", Address= '" + Address 

+「」,DOB =「+ Dob +」,其中ID =「+ Update; PreparedStatement ps_Statement = connection.prepareStatement(query); ps_Statement.executeUpdate(); JOptionPane.showMessageDialog(panelID,「記錄已成功更新」); connection.close(); catch(ClassNotFoundException e){ e.printStackTrace(); catch(SQLException e){ e.printStackTrace(); }

回答

0

你有一個,在where子句之前。如果是日期,DOB應該在單引號內。你確定Mobile是一個整數嗎?

"update Table1 set Name='" + Name + "' , Email='" + Email + "' , Mobile=" + Mobile + ", Address= '" + Address 
+ "', DOB='" +Dob + "' where ID=" + Update; 

但無論如何,考慮使用帶有參數傳遞的PreparedStatement。您正在使用的SQL容易受到SQL注入攻擊。

+0

是移動,所以我沒有把單引號....出生日期我也把單引號,但得到的同樣的例外 – Shiv

+0

你可以把一行打印查詢變量到控制檯並粘貼生成的SQL嗎?的System.out.println(查詢); – shazin

+0

我不明白你在說什麼?和雅手機案件我得到了同樣的錯誤因爲我沒有把單引號...所以我認爲有一個問題,在我的查詢..我也已經把#DOB#但它沒有奏效 – Shiv

0

請改爲嘗試這樣的:在一個整數

String yourFormat = "dd-MMM-yy"; 
Date yourDateVariable = null; 

SimpleDateFormat sdf = new SimpleDateFormat(yourFormat); 

try { 
    yourDateVariable = sdf.parse(Dob); 
} catch (Exception ex) { 
    // Do something 
} 

// Continue your code<code> 
+0

問題不是因爲移動它是因爲日期..我已經將手機轉換爲整數相同,因爲你寫了 – Shiv

+0

我很抱歉,我從來沒有想過一些變量實際上是一個日期。在這種情況下,我會編輯我的答案。 –

+0

邁克爾可以將該字符串轉換爲日期,然後嘗試更新? – Shiv