我希望从下拉列表中选择设备 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
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/