SQL syntax error : correspond to your MariaDB server for the right syntax

纵饮孤独 提交于 2020-06-01 05:49:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!