gpt4 book ai didi

java - 执行和sql字符串

转载 作者:行者123 更新时间:2023-12-01 21:28:08 25 4
gpt4 key购买 nike

我目前正在编写 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com