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 ?

1ac0
  • 2,875
  • 3
  • 33
  • 47
  • 1
    How is this method related to an `ActionListener` ? Also, print the stack trace of the Exception you might catch. – Arnaud Mar 29 '16 at 08: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..!! – Vishal Gajera Mar 29 '16 at 08:09
  • You should add stack tracing in your catch block. Probably a SQL Exception is thrown, but you cannot see it. – Sergiy Medvynskyy Mar 29 '16 at 08:10
  • 1
    See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) – Andrew Thompson Mar 29 '16 at 08: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. – Andrew Thompson Mar 29 '16 at 08:12
  • 1
    You really should be using [Prepared Statements](http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html). 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 – MadProgrammer Mar 29 '16 at 08:15
  • @Kranolabar can you please insert code for table creation? looks like you have wrong columns names – 1ac0 Mar 29 '16 at 08:49
  • @LadislavDANKO Thanks for noticing that ! Yes, I changed the SQL query – Kranolabar Mar 29 '16 at 08:54
  • I think that you shall follow @MadProgrammer advice and use PreparedStatement. It seems that you are trying to insert a Date as a String. Also you don't have to use ['] simple quote in the column names. Maybe this example helps http://stackoverflow.com/questions/11804906/insert-row-into-database-with-preparedstatement – RubioRic Mar 29 '16 at 09:16

1 Answers1

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