问题
When I click on the Create button to add a supplier record in to my database, I receive the above error message.
Can someone help me where did I went wrong: is there something wrong with the insert statement.
private void jbtnCreateActionPerformed(java.awt.event.ActionEvent evt) {
try {
String sql = "INSERT INTO supplier"
+"(Full Name, Address Line 1, Address Line 2, Post Code, Email Address, Phone Number)"
+"VALUES (?,?,?,?,?,?)";
connection = DriverManager.getConnection("jdbc:mysql://localhost/inventory management", "root", "");
ps = connection.prepareStatement(sql);
ps.setString(1, txtname.getText());
ps.setString(2, txtaddressline1.getText());
ps.setString(3, txtaddressline2.getText());
ps.setString(4, txtpostcode.getText());
ps.setString(5, txtemail.getText());
ps.setString(6, txtphone.getText());
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "Supplier Added");
} catch(SQLException | HeadlessException ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
回答1:
You have whitespaces in the column names (as well as in the connection string) which is not allowed. If you really need to use column names with spaces, put them in backticks:
String sql = "INSERT INTO supplier"
+"(`Full Name`, `Address Line 1`, `Address Line 2`, `Post Code`, `Email Address`, `Phone Number`)"
+"VALUES (?,?,?,?,?,?)";
However, it would be better to use snake case for the column names: full_name
, etc.
来源:https://stackoverflow.com/questions/61934907/sql-syntax-error-correspond-to-your-mariadb-server-for-the-right-syntax