How to update date in ms access through netbeans

孤街醉人 提交于 2019-12-12 04:33:49

问题


*Exception : *

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

code is:

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

try {

            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 + ", where ID=" + Update; PreparedStatement ps_Statement = connection.prepareStatement(query); ps_Statement.executeUpdate(); JOptionPane.showMessageDialog(panelID, "Record Updated Successfully"); connection.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }


回答1:


You have a , before the where clause. And DOB if it is a date should come within single quotes. And are you sure Mobile is a integer?

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

But anyway consider using PreparedStatement with argument passing. The SQL you are using is vulnerable for SQL Injection attacks.




回答2:


Please try this instead:

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>


来源:https://stackoverflow.com/questions/13699162/how-to-update-date-in-ms-access-through-netbeans

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!