[英]How to update date in ms access through 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 =” +更新; PreparedStatement ps_Statement = connection.prepareStatement(query); ps_Statement.executeUpdate(); JOptionPane.showMessageDialog(panelID,“記錄更新成功”); connection.close(); } catch(ClassNotFoundException e){e.printStackTrace(); } catch(SQLException e){e.printStackTrace(); }
您在where子句之前有一個。 如果是日期,則DOB應該用單引號引起來。 您確定Mobile是整數嗎?
"update Table1 set Name='" + Name + "' , Email='" + Email + "' , Mobile=" + Mobile + ", Address= '" + Address
+ "', DOB='" +Dob + "' where ID=" + Update;
但是無論如何,請考慮將PreparedStatement與參數傳遞一起使用。 您使用的SQL容易受到SQL Injection攻擊。
請嘗試以下方法:
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.