I'm trying to create a java program that connects to MySQL database. But its giving me an error, "You have an error with your SQL syntax", and is referring to this line:
String sql = "INSERT INTO items_in_hand (Item Name, Price (each), Quantity (available)) VALUES (?,?,?)";
Here's the block:
DefaultTableModel model = (DefaultTableModel) itemTable.getModel();
if(!txt_item.getText().trim().equals("")){
try{
String sql = "INSERT INTO items_in_hand (Item Name, Price (each), Quantity (available)) VALUES (?,?,?)";
st = con.prepareStatement(sql);
st.setString(1,txt_item.getText());
st.setString(2,txt_price.getText());
st.setString(3,txt_quantity.getText());
st.execute();
JOptionPane.showMessageDialog(null , "Saved.");
//model.addRow(new Object[] {txt_item.getText(),txt_price.getText(),txt_quantity.getText()});
}catch(Exception ex){
JOptionPane.showMessageDialog(null , ex);
}
}
Can anyone help?
1 Answer 1
Replace the query
String sql = "INSERT INTO items_in_hand (Item Name, Price (each), Quantity (available)) VALUES (?,?,?)";
to like below
String sql = "INSERT INTO items_in_hand (Item, Price, Quantity) VALUES (?,?,?)";
Assuming column Item, Price and Quantity are present in the items_in_hand table.
answered Sep 18, 2014 at 8:58
Prabhakaran Ramaswamy
26.1k10 gold badges61 silver badges65 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default
Price (each)is an invalid column name due to the space and parenthesis. You need to quote that, e.g."Price (each)"(if ANSI mode is enabled)