0

I'm trying to insert data into my SQL tables. I have a Jframe:

enter image description here

The purpose of the form is to input whatever is written on these text fields into the database. My problem is that when I press Send Request, nothing happens.. Am I missing something?

This is my code for the Send Request button:

private void SendRequestActionPerformed(java.awt.event.ActionEvent evt) {                                            
    try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = (Connection)
            DriverManager.getConnection("jdbc:mysql://localhost:3306/database","my-user","my-pass");
            Statement stmt = con.createStatement();

            //execute commands that collects
            //the text/items from  the fields and converts them to strings
            String MedRole = ofMedRoleComboBox.getSelectedItem().toString();
            String PtID = ofPatientIDField.getText();
            String Date = ofDateField.getText();
            String Time = ofTimeField.getText();
            String Purpose = ofPurposeComboBox.getSelectedItem().toString();


            // sql query
            String insertOrdersDB = "INSERT INTO Orders ('PatientID','MedicalRole','Date','Time','Purpose') VALUES ("+MedRole+","+PtID+","+Date+","+Time+","+Purpose+")";

            // executing sql query
            stmt.executeUpdate(insertOrdersDB);

        }
        catch (Throwable e) {
        e.printStackTrace();
        }
}

In e.printStackTrace(); is:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 
You have an error in your SQL syntax; check the manual 
that corresponds to your MySQL server version for the 
right syntax to use near ''PatientID','MedicalRole','Date','Time','Purpose') VALUES (Nurse,7,2016-04-13,12' at line 1

I was thinking it was the date syntax, which is different from the SQL. So I changed it to match the SQL table format. But still got the same error.

Now I think it's regarding the Time ?

9
  • 1
    How is this method related to an ActionListener ? Also, print the stack trace of the Exception you might catch. Commented Mar 29, 2016 at 8:08
  • 1
    please, add e.printStrackTrace() into your catch-block so that you will get what error are you phasing if any then, and post it over here, we all help you to come out from this..!! Commented Mar 29, 2016 at 8:09
  • 1
    See What is a stack trace, and how can I use it to debug my application errors? Commented Mar 29, 2016 at 8:11
  • 1
    Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and use it consistently. Commented Mar 29, 2016 at 8:12
  • 1
    You really should be using Prepared Statements. You're also ignoring the one piece of information which would tell you what the likely cause of the problem is. Add e.printStackTrace(); to the body of your catch block Commented Mar 29, 2016 at 8:15

1 Answer 1

0

Okay so I followed some of your comments and feedbacks. Thank you so much for that. I'm still learning Java and I know I've a long way to go.

But anyway, thanks to your guidance, I managed to get my Form to work.

PreparedStatement pstmt = null;
    try {
        Class.forName("com.mysql.jdbc.Driver");
            Connection con = (Connection)
        DriverManager.getConnection("jdbc:mysql://localhost:3306/database","my-user","my-pass");    

        String sql = "INSERT INTO Orders (PatientID, MedicalRole, Date, Time, Purpose) VALUES (?,?,?,?,?)";    

        pstmt = con.prepareStatement(sql);
        pstmt.setInt(1, Integer.parseInt(ofPatientIDField.getText()));
        pstmt.setString(2, ofMedRoleComboBox.getSelectedItem().toString());
        pstmt.setObject(3, ofDateField.getValue());
        pstmt.setObject(4, ofTimeField.getText());
        pstmt.setString(5, ofPurposeComboBox.getSelectedItem().toString());

        pstmt.executeUpdate();

    } catch (Exception e) {
        e.printStackTrace();
    }

So yes, I replaced this with my previous try and it worked !

Thank you for telling me to use Prepared Statements :) @MadProgrammer

Sign up to request clarification or add additional context in comments.

1 Comment

Oh and e.printStackTrace() is a really good tip too ! Helped me figure out what I was missing

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.