1

I am getting a error that I do not fully understand, why is it showing up ?
I have a form where I enter details into relevant JTextBoxes and data from those textboxes should be sent to a database I have set up on my local.

Full error message

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 ')VALUES'asd','asd',",",",",",",'Home','Female','White  
British' at line 1

Code

conn = (Connection) SQLConnect.ConnectDb();
    //inserting all values to database
    String sql = "INSERT INTO w1283057.criminalrecords "
            + "(FName, MName, Sname, DOB, Address, HPhone, BPhone, MPhone "
            + "ResidentStatus, Sex, Race, IncidentLocation, Zone, PremiseType "
            + "DateRecorded, TimeRecorded, Weapons, CrimeOffences) "
            + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    try{
        //Declaring all variable that wiil be collected from JTextField
        pst = conn.prepareStatement(sql);
        //coverting JText to String
        String fname = FName.getText();
        String mname = MName.getText();
        String surname = Surname.getText();
        String dob = DOB.getText();
        String fullAddress = FullAddress.getText();
        String hPhone = HPhone.getText();
        String bPhone = BPhone.getText();
        String mPhone = MPhone.getText();
        String incidentLocation = IncidentLocation.getText();
        String incidentZone = IncidentZone.getText();
        String incidentPrType = IncidentPrType.getText();
        String incidentDate = IncidentDate.getText();
        String incidentTime = IncidentTime.getText();
        String incidentWeapons = IncidentWeapons.getText();
        String crimeOffences = CrimeOffences.getText();
        String radioText = "";

        //collecting input data and assign them to variables            
            pst.setString(1, fname);
            pst.setString(2, mname);
            pst.setString(3, surname);
            pst.setString(4, dob);
            pst.setString(5, fullAddress);
            pst.setString(6, hPhone);
            pst.setString(7, bPhone);
            pst.setString(8, mPhone);
            //radio buttons Resident Status
            if(RHome.isSelected())
            {
                radioText = RHome.getText();
                pst.setString(9, radioText);
            }
            else if(RForeign.isSelected())
            {
                radioText = RForeign.getText();
                pst.setString(9, radioText);
            }
            //radio buttons Sex
            if(Male.isSelected())
            {
                radioText = Male.getText();
                pst.setString(10, radioText);

            }
            else if(Female.isSelected())
            {
                radioText = Female.getText();
                pst.setString(10, radioText);
            }
            //radio button Race
            if(WhiteBritish.isSelected())
            {
                radioText = WhiteBritish.getText();
                pst.setString(11, radioText);

            }
            else if(WhiteOther.isSelected())
            {
                radioText = WhiteOther.getText();
                pst.setString(11, radioText);
            }
            else if(Black.isSelected())
            {
                radioText = Black.getText();
                pst.setString(11, radioText);
            }
            else if(Asian.isSelected())
            {
                radioText = Asian.getText();
                pst.setString(11, radioText);
            }
            else if(Indian.isSelected())
            {
                radioText = Indian.getText();
                pst.setString(11, radioText);
            }
            /////////////////////////////////////
            pst.setString(12, incidentLocation);
            pst.setString(13, incidentZone);
            pst.setString(14, incidentPrType);
            pst.setString(15, incidentDate);
            pst.setString(16, incidentTime);
            pst.setString(17, incidentWeapons);
            pst.setString(18, crimeOffences);
            pst.executeUpdate();
            System.out.println("records added sucessfully");

    }
    catch (SQLException a)
        {
            JOptionPane.showMessageDialog(null, a);
        }

enter image description here

5
  • I will also suggest not use single quotes in your sql. Commented Jan 9, 2013 at 20:28
  • The prepared statement will take care of the quotation requirement Commented Jan 9, 2013 at 20:29
  • @BryanMoyles I know Its does not make much difference, but it provides easy reading. Commented Jan 9, 2013 at 20:31
  • The error message shows a missing opening bracket (after values) , yet your code includes it. Is it possible you are not showing us the code you actually run? Commented Jan 9, 2013 at 20:35
  • I have copied exactly the code i have in my compiler Commented Jan 9, 2013 at 20:38

2 Answers 2

5

You have a comma at the end of your value list, get rid of it, or populate it with an appropriate value.

+ "DateRecorded, TimeRecorded, Weapons, CrimeOffences,) " <---
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,)"; <----
Sign up to request clarification or add additional context in comments.

13 Comments

thank you for your reply, i have done as you said but i still get this error
@MaciejCygan Check you have same column name listed as those of ? in prepared statement. I mean you should have 18 column names and ? as stated in your code.
Can you please update your code to show what you currently have? I'll have to agree with @smit and make the assumption that your column count isn't matching, or that you haven't removed the ending comma from both of the lines I have in my answer
This is kinda nice typo mistake . You are missing , at the end of sql at MPhone and PremiseType . Just add those , after these two words and I think you are good to go.
@MaciejCygan Better take a look at my previous comment. I pointed a mistake which could cause problem.
|
4

Your code

 String sql = "INSERT INTO w1283057.criminalrecords "
        + "(FName, MName, Sname, DOB, Address, HPhone, BPhone, MPhone "
        + "ResidentStatus, Sex, Race, IncidentLocation, Zone, PremiseType "
        + "DateRecorded, TimeRecorded, Weapons, CrimeOffences) "
        + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

Try this... After the fields (MPhone, PremiseType) lack the comma

 String sql = "INSERT INTO w1283057.criminalrecords "
        + "(FName, MName, Sname, DOB, Address, HPhone, BPhone, MPhone, "
        + "ResidentStatus, Sex, Race, IncidentLocation, Zone, PremiseType, "
        + "DateRecorded, TimeRecorded, Weapons, CrimeOffences) "
        + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

Greetings.

1 Comment

We have already figured out the right answer :) i was just waiting for someone to submit it, for everyone reference :) so thank you

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.