gpt4 book ai didi

Java derby 数据库不工作

转载 作者:行者123 更新时间:2023-11-29 10:41:03 25 4
gpt4 key购买 nike

这几行代码有问题吗?我得到的只是“无效条目”。我有一个名为“生产”的数据库,其中有一个名为“生产”的表。

try {
String mk = jTextField1.getText();
String mn = jTextField2.getText();
String ab = (String) jComboBox1.getSelectedItem();
String bc = (String) jComboBox2.getSelectedItem();

try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:1527/production");
{
String host = "jdbc:mysql://localhost:1527/production";
JOptionPane.showMessageDialog(this, "connection success");
Statement stmt = con.createStatement();

String query = "update PRODUCT set FACTORY='" + ab + "' PRODUCT_NAME = '" + mk + "' UNIT= '" + bc + "' and OPENING_BALANCE'" + mn + "');";
stmt.executeUpdate(query);
JOptionPane.showMessageDialog(this, "Record has been inserted");
stmt.close();
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "invalid entry");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error in Connectivity", "Message", 2);
}

最佳答案

您的查询不正确:

  1. 您必须在字段之间使用 ,
  2. 设置字段时不需要使用 and(and OPENING_BALANCE'"+ mn + "')
  3. 查询末尾有一个右括号 ),您不需要它

但是你的方法容易出现语法错误和 SQL Inection,你必须使用PreparedStatement 来代替,它更安全,更有帮助:

query = "update PRODUCT set FACTORY = ?, PRODUCT_NAME = ?, UNIT= ?, OPENING_BALANCE = ?";
try (PreparedStatement update = connection.prepareStatement(query)) {

update.setString(1, ab);
update.setString(2, mk);
update.setString(3, bc);
update.setString(4, mn);

update.executeUpdate();
}

关于Java derby 数据库不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45479539/

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