Reputation: 206
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?
Upvotes: 1
Views: 48
Reputation: 26094
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.
Upvotes: 1