java - 语法错误>>检查与您的 MariaDB 服务器版本相对应的手册,以获取在第 1 行 '?' 附近使用的正确语法

标签 java sql sqlexception

我希望从下拉列表中选择设备 ID 后,其他字段值会自动提取到文本字段中。 但出现以下错误:

java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; 
    check the manual that corresponds to your MariaDB server version 
    for the right syntax to use near '?' at line 1

enter image description here

textField.addActionListener(new ActionListener() 
{
    public void actionPerformed(ActionEvent e) 
    {
        String DeviceID=textField.getSelectedItem().toString(); 
        String DeviceName=textField_1.getText();
        String SerialNumber=textField_2.getText();
        String ModelNumber=textField_3.getText();
        String Make=textField_4.getText();
        try 
        {
            Connection con=DB.getConnection();
            Statement ps=con.createStatement();

             ResultSet rs=ps.executeQuery("Select DeviceID,DeviceName,SerialNumber,ModelNumber,Make FROM deviceadd WHERE DeviceID =?");
             if(rs.next())
             {
            String ID = rs.getString("DeviceName");
            textField_1.setText(ID);
            String FN = rs.getString("SerialNumber");
            textField_2.setText(FN);
            String LN = rs.getString("ModelNumber");
            textField_3.setText(LN);
            String Des = rs.getString("Make");
            textField_4.setText(Des);
             }
        } 
        catch (SQLException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
});

最佳答案

您使用的 SQL 语句是准备好的语句,您可以在其中添加参数。

您正在使用正常的语句执行哪里?不会被替换为导致 sql 服务器无法理解的查询的参数。

您应该查找PreparedStatement以了解如何使用它(例如https://www.javatpoint.com/PreparedStatement-interface)

关于java - 语法错误>>检查与您的 MariaDB 服务器版本相对应的手册,以获取在第 1 行 '?' 附近使用的正确语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56257000/

相关文章:

java - 在 mysql 和 java 中存储 Google 用户 ID 值的最佳方法

java - JBoss - 在运行时加载新模块

mysql - 使用 REPLACE 和 SUBSTRING_INDEX 在 MySQL 中拆分字符串

sql - 在 Microsoft Access SQL 中使用带有数字文本值的运算符

java - 当从 PreparedStatement.close 引发 SQLException 时,我应该采取什么措施?

java - 参数索引超出范围(3 > 参数数量,即 2)

使用 byte[] 的 Java 快速 atoi

mysql - 如何计算每个类别的职位的唯一点击次数

postgresql - 如何使用第三方库将 .aar 文件部署为服务(在 WSO2 应用程序服务器中)

java - 在 Java 中存储三个值的数组