BEE
BEE

Reputation: 107

MySQL Query Through Java Syntax Error

I have this piece of code which inserts records into the accounts table:

String accNumber = jTextField5.getText();
String accName = jTextField4.getText();
String accAddress1 = jTextField3.getText();
String accAddress2 = jTextField2.getText();
String accCity = jTextField6.getText();
String accCounty = jTextField7.getText();
String accPostCode = jTextField9.getText();
String accContact = jTextField8.getText();

String query = "Insert into accounts (AccNo, name, address, address2, address3, City, County, PostCode, contact) values (?, ?, ?, ?, ?, ?, ?, ?, ?)";

try{

    connect.pst = connect.con.prepareStatement(query);
    connect.pst.setString(1, accNumber);
    connect.pst.setString(2, accName);
    connect.pst.setString(3, accAddress1);
    connect.pst.setString(4, accAddress2);
    connect.pst.setString(5, null);
    connect.pst.setString(6, accCity);
    connect.pst.setString(7,accCounty);
    connect.pst.setString(8, accPostCode);
    connect.pst.setString(9, accContact);
    connect.pst.execute();
    JOptionPane.showMessageDialog(null, "Saved");

}catch(Exception e){
    JOptionPane.showMessageDialog(null, e);
}

This works just fine and inserts records into the accounts table. Next I have this piece of code which is meant to update the orderstable table which contains 6 fields; Order Number, AccNo, Incvoice Number, Description, Amount, VAT. The Order Number field has been set to auto increment The code:

String accNumber = jTextField29.getText();
String invNo = jTextField20.getText();
String description = jTextField21.getText();
String vat = jTextField22.getText();
String amount = jTextField23.getText();

String query = "Insert into orderstable (Order Number, AccNo, Invoice Number, Description, Amount, VAT) values (?, ?, ?, ?, ?, ?)";
try{

    connect.pst = connect.con.prepareStatement(query);
    connect.pst.setString(1, "3");
    connect.pst.setString(2, accNumber);
    connect.pst.setString(3, invNo);
    connect.pst.setString(4, description);
    connect.pst.setString(5, amount);
    connect.pst.setString(6, vat);
    connect.pst.execute();
    JOptionPane.showMessageDialog(null, "Saved");

}catch(Exception e){
    JOptionPane.showMessageDialog(null, e);
}

This code above returns a SQL syntax error. I am not sure why. What could the reason be for this SQL syntax error?

Upvotes: 0

Views: 52

Answers (2)

ayaz321
ayaz321

Reputation: 1

The auto increment field order number should not be include in the attribute section.

Your query should look like this

String query = "Insert into orderstable (AccNo, Invoice Number, Description, Amount, VAT) values (?, ?, ?, ?, ?)"; 

order number column should not be include in field list, because it is auto increment

Upvotes: 0

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

You can not have the column names with space and if you do so you need to have then in backticks

Insert into orderstable 
(`Order Number`, AccNo, `Invoice Number`, Description, Amount, VAT)

Check here for more details http://dev.mysql.com/doc/refman/5.0/en/identifiers.html

Upvotes: 3

Related Questions