private void SubmitActionPerformed(java.awt.event.ActionEvent evt) {                                       
        try
        {
        Statement s = con.createStatement();
        Statement s1 = con.createStatement();
        Statement s2 = con.createStatement();
        Statement s3 = con.createStatement();
                
        ResultSet rs = s.executeQuery("select MAX(Guest_ID) from Guests");
        rs.next();
       int maxGuestID = rs.getInt(1) + 1;
       //s1.execute("insert into Guests values(" + maxGuestID + ",'" + txtUserName.getText() + "',' " + txtUserAddress.getText() + "','"+ txtUserPhone.getText() + "','"+ txtUserEmail.getText() + "','" + cmbCreditCardType.getSelectedItem() + "','"+ txtCreditCardNumber.getText() + "')");

        // insert data into reservation table
        ResultSet rs1 = s2.executeQuery("select MAX(Reservation_ID) from Reservation");
       rs1.next();
       int maxResID = rs1.getInt(1) + 1;
  //i'm getting error for date     
       s3.execute("insert into Reservation values(" + maxResID + "," + maxGuestID +",'"+ChkPrepaid.getSelectedObjects()+",'"+txtCheckInDate.getText()+"','"+txtCheckOutDate.getText()+ "'");
       
       
        }
        catch(Exception e)
        {
                    System.out.println("Error: " + e);
        }

Dani AI

Generated

A few concrete causes stand out in 's snippet: a syntax mistake in the constructed VALUES list (missing punctuation can produce a "missing operator" or syntax error), using getSelectedObjects() for a single checkbox (that returns an Object[]), and passing raw text for date columns. Rather than debugging string concatenation, use parameterized SQL so types, quoting and date conversion are handled by JDBC.

try (PreparedStatement pst = con.prepareStatement(
    "INSERT INTO Reservation (Reservation_ID, Guest_ID, Prepaid, CheckInDate, CheckOutDate) VALUES (?, ?, ?, ?, ?)")) {

    pst.setInt(1, maxResID);
    pst.setInt(2, maxGuestID);
    pst.setBoolean(3, ChkPrepaid.isSelected()); // checkbox -> boolean

    java.time.format.DateTimeFormatter fmt = java.time.format.DateTimeFormatter.ofPattern("d/M/yyyy");
    java.time.LocalDate in = java.time.LocalDate.parse(txtCheckInDate.getText().trim(), fmt);
    java.time.LocalDate out = java.time.LocalDate.parse(txtCheckOutDate.getText().trim(), fmt);

    pst.setDate(4, java.sql.Date.valueOf(in));
    pst.setDate(5, java.sql.Date.valueOf(out));
    pst.executeUpdate();
}

Notes: always include the column list in INSERT to avoid ordering issues. Match the date parser to your UI format or use a date picker to ensure valid values. Prefer PreparedStatement both to avoid syntax/quoting mistakes and to prevent SQL injection. As and suggested, logging the failing SQL helps for quick checks, but migrating to parameter binding is the robust fix.

Recommended Answers

All 2 Replies

You print your failing query string on screen.
Copy it and execute in mysql query browser or phphmyadmin or msql prompt directly. There you will find the actual problem.

System.out.println("insert into Reservation values(" + maxResID + "," + maxGuestID +",'"+ChkPrepaid.getSelectedObjects()+",'"+txtCheckInDate.getText()+"','"+txtCheckOutDate.getText()+ "'");

i suggest building the sql query or non-query so that when it fails you can print the query and check whats happening, its usually a problem with the way the query is built.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.