0

I have my project which is a driver's license data base.

When the user dill out the fields, it is normal, but when the user pressed the 'Submit' button, everything is messed up.

The error Parameter index out of range error is shown, so here's my code.

private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             

    String sql = "INSERT INTO dr_record_table (Driver's ID, Last Name, First Name, Middle Name, Street, Barangay, City/Municipality, Province, Country, Month, Day, Year, Gender) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

    try {
        pst = conn.prepareStatement(sql);
        pst.setString(1, idTxtFld.getText());
        pst.setString(2, lnTxtFld.getText());
        pst.setString(3, fnTxtFld.getText());
        pst.setString(4, mnTxtFld.getText());
        pst.setString(5, saTxtFld.getText());
        pst.setString(6, baTxtFld.getText());
        pst.setString(7, caTxtFld.getText());
        pst.setString(8, paTxtFld.getText());
        pst.setString(9, cntryTxtFld.getText());
        pst.setString(10, String.valueOf(monthCBox.getSelectedItem()));
        pst.setString(11, String.valueOf(dayCBox.getSelectedItem()));
        pst.setString(12, String.valueOf(yearCBox.getSelectedItem()));
        pst.setString(13, getSelectedButtonText(bg));
        pst.execute();

        JOptionPane.showMessageDialog(null, "Entry Saved!");

    } catch (SQLException | HeadlessException e) {
        JOptionPane.showMessageDialog(null, e);
    }

}   

1 Answer 1

1

Escape the single quote in Driver's ID. Something like,

String sql = "INSERT INTO dr_record_table (Driver''s ID, Last Name, "
    + "First Name, Middle Name, Street, Barangay, City/Municipality, "
    + "Province, Country, Month, Day, Year, Gender) VALUES "
    + "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you sir! I finally fixed this one! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.