SQL语法错误:与您的MariaDB服务器对应的正确语法 [英] SQL syntax error : correspond to your MariaDB server for the right syntax

查看:187
本文介绍了SQL语法错误:与您的MariaDB服务器对应的正确语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击创建按钮以将供应商记录添加到我的数据库中时,我收到上述错误消息。

When I click on the Create button to add a supplier record in to my database, I receive the above error message.

有人可以帮助我我在哪里出错了:insert语句是否有问题。

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


推荐答案

您列名(以及连接字符串)中不允许使用空格。如果您确实需要使用带空格的列名称,请将其放在反引号中:

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 (?,?,?,?,?,?)";

但是,最好使用蛇形作为列名: full_name 等。

However, it would be better to use snake case for the column names: full_name, etc.

这篇关于SQL语法错误:与您的MariaDB服务器对应的正确语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆