[SOLVED] SQL error or missing database (near “Name”: syntax error)


This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under
CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

Issue

try {
                String query = "insert into TotalStock (Item Name, Perishable, Stock, Price) values (?,?,?,?)";
                
                PreparedStatement pst = connection.prepareStatement (query);
                
                pst.setString(1, textField.getText());
                pst.setString(2, textField_3.getText());
                pst.setString(3, textField_2.getText());
                pst.setString(4, textField_1.getText());
                
                pst.execute();
                JOptionPane.showMessageDialog(null, "Item Saved!") ;
                pst.close();
                } catch (Exception e1) {
                e1.printStackTrace();
                }
            
        }

Solution

Blanks in column names is a very bad idea. You should rename the column.

For now you can also use backticks to escape the blank:

String query = "insert into TotalStock (`Item Name`, Perishable, Stock, Price) values (?,?,?,?)";

Answered By – Jens

people found this article helpful. What about you?