我目前正在编写 java 的更新语句。单击按钮
后,它将获取您放入文本字段
中的整数数据,并从下拉列表中读取姓名并提供相应的员工 ID。数据库连接是通过不同的类完成的。
ActionListener myActionListener = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
DatabaseConnection connection = new DatabaseConnection();
if (connection.openConnection()) {
String ID = input1.getText();
int orderID = Integer.parseInt(ID);
String firstName = (String) cb.getSelectedItem();
System.out.println(firstName);
System.out.println(orderID);
if (firstName == "Patrick") {
int employeeId = 10;
String sql = "UPDATE barorder SET statusId=2, employeeId='" + employeeId + "' where id='" + orderID + "' ;";
}
}
}
};
这是我到目前为止所得到的,但我现在不知道如何执行 SQL 字符串。有什么建议吗?
最佳答案
您可以使用 PreparedStatement
来更新查询,如下所示:
ActionListener myActionListener = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
DatabaseConnection connection = new DatabaseConnection();
PreparedStatement preparedStatement = null;
if (connection.openConnection())
{
String ID = input1.getText();
int orderID = Integer.parseInt(ID);
String firstName = (String) cb.getSelectedItem();
System.out.println(firstName);
System.out.println(orderID);
if (firstName == "Patrick")
{
int employeeId = 10;
String sql = "UPDATE barorder SET statusId=2, employeeId= ? where id= ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, employeeId);
preparedStatement.setInt(2, orderID);
preparedStatement.executeUpdate(sql);
}
}
}
};
关于java - 执行和sql字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37790519/