简体   繁体   中英

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:

java.sql.sqldataexception: the syntax of the string representation of a datetime value is incorrect

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.

Thank you.

Your problem is that you're trying to embed a Date value (or a String representation of one) into the INSERT statement. Instead of concatenating variables into the query literal, you should use parameterized SQL through a PreparedStatement . 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.

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. 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.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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