连接到数据库时的SQL语法错误(JAVA)

时间:2014-09-18 08:54:17

标签: java mysql

我正在尝试创建一个连接MySQL数据库的java程序。但它给了我一个错误,"您的SQL语法错误",并指的是这一行:

String sql = "INSERT INTO items_in_hand (Item Name, Price (each), Quantity (available)) VALUES (?,?,?)";

这是块:

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);
            }

        }

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

替换查询

String sql = "INSERT INTO items_in_hand (Item Name, Price (each), Quantity (available)) VALUES (?,?,?)";

喜欢以下

 String sql = "INSERT INTO items_in_hand (Item, Price, Quantity) VALUES (?,?,?)";

假设Item, Price and Quantity表中存在列items_in_hand