简体   繁体   English

将JXDatePicker中的日期添加到Netbeans中的SQL数据库中

[英]Add date from JXDatePicker into SQL Database in Netbeans

I would like to add a date value from JXDatePicker into my SQL database, however I'm getting this error when running it: 我想将JXDatePicker中的日期值添加到我的SQL数据库中,但是运行它时出现此错误:

java.sql.sqldataexception: the syntax of the string representation of a datetime value is incorrect java.sql.sqldataexception:日期时间值的字符串表示形式的语法不正确

This is my code: 这是我的代码:

try {
    String url = "jdbc:derby://localhost:1527/Members";
    String username = "admin1";
    String password = "admin1";

    Connection con = DriverManager.getConnection(url, username, password);
    Statement stmt = con.createStatement();

    String query = "INSERT INTO BOOKING(MEMBERID, NAME, CONTACT, "
            + "EMAILADDRESS, RESERVATIONDATE, RESERVATIONTIME) "
            + "VALUES('"+txtMemberID.getText()+"', '"+txtName.getText()+"', "
            + "'"+txtContact.getText()+"', '"+txtEmail.getText()+"', "
            + "'"+comboDate.getDate()+"', '"+comboTime.getSelectedItem()+"')";

    stmt.execute(query);

    JOptionPane.showMessageDialog(null, "Booking created");

    txtMemberID.setText(null);
    txtName.setText(null);
    txtContact.setText(null);
    txtEmail.setText(null);
    comboDate.setDate(null);
    comboTime.setSelectedItem("00");

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

The datatype specified for the Date attribute in my database is Date. 在数据库中为Date属性指定的数据类型为Date。

Thank you. 谢谢。

Your problem is that you're trying to embed a Date value (or a String representation of one) into the INSERT statement. 您的问题是您试图将Date值(或一个的String表示形式)嵌入INSERT语句。 Instead of concatenating variables into the query literal, you should use parameterized SQL through a PreparedStatement . 代替将变量连接到查询文字中,您应该通过PreparedStatement使用参数化的SQL In addition to protecting your code from SQL injection , parameterized statements are re-usable by the database, which means that the DB doesn't need to parse the SQL before each execution -- this is especially important if you're running a large number of queries in a loop. 除了保护代码不被SQL注入外 ,参数化的语句还可以由数据库重用,这意味着DB不需要在每次执行前解析SQL -如果您要运行大量的代码,这尤其重要循环查询。

Another thing that you should take care of, is closing the resources you've opened. 您需要注意的另一件事是关闭您打开的资源。 In your example code, the Connection and Statement are left open after they are no longer needed. 在您的示例代码中,不再需要ConnectionStatement之后将它们保持打开状态。 This is easy to fix using the try-with-resources statement , which was introduced in Java 7. The resources declared within the try clause get automatically closed after the statement is executed. 使用Java 7中引入的try-with-resources语句很容易解决此问题。在执行该语句后,将自动关闭try子句中声明的资源。

Putting it all together, here's an example of what the modified code could look like: 综上所述,这是修改后的代码的示例:

String query = "INSERT INTO BOOKING(MEMBERID, NAME, CONTACT, "
        + "EMAILADDRESS, RESERVATIONDATE, RESERVATIONTIME) "
        + "VALUES(?, ?, ?, ?, ?, ?)";

try (Connection con = DriverManager.getConnection(url, username, password);
     PreparedStatement ps = con.prepareStatement(query)) {

    ps.setString(1, txtMemberID.getText());
    ps.setString(2, txtName.getText());
    ps.setString(3, txtContact.getText());
    ps.setString(4, txtEmail.getText());
    ps.setDate(5, new java.sql.Date(comboDate.getDate().getTime()));
    ps.setString(6, comboTime.getSelectedItem().toString());

    ps.executeUpdate();

    JOptionPane.showMessageDialog(null, "Booking created");

    /*clear the UI components etc.*/

} catch(SQLException ex) {
    JOptionPane.showMessageDialog(null, ex.toString(), JOptionPane.ERROR_MESSAGE);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM