Tony Park
Tony Park

Reputation: 1

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

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

Upvotes: 0

Views: 577

Answers (1)

Nowhere Man
Nowhere Man

Reputation: 19555

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.

Upvotes: 1

Related Questions