简体   繁体   English

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

[英]Error in SQL syntax while connecting to database (JAVA)

I'm trying to create a java program that connects to MySQL database. 我正在尝试创建一个连接MySQL数据库的Java程序。 But its giving me an error, "You have an error with your SQL syntax", and is referring to this line: 但是它给了我一个错误,“您的SQL语法有错误”,并指向以下行:

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? 有人可以帮忙吗?

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. 假设items_in_hand表中存在Item, Price and Quantity列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM