gpt4 book ai didi

java - 如何更新连接到 JTable 的数据库 (JavaDatabase) 中的一行条目?

转载 作者:行者123 更新时间:2023-12-02 09:16:01 26 4
gpt4 key购买 nike

我正在制作一个程序,但对编程不太了解...我使用了一些 youtube 视频来帮助我。

我的程序是为厨师设计的,他可以编辑用户和食物并收集检查员的评分和建议。编辑用户详细信息的厨师部分有效。

但是,检查员的评级不会,因为它会抛出错误:SQLSyntaxException:在第 1 行第 65 列遇到“素食主义者”。我相信这是因为以错误的方式获取评级值(整数)。 .

'

    public void getConnection(){
try{
myconObj = DriverManager.getConnection("jdbc:derby://localhost:1327/MyApp", "Me", "Me");
mystatObj=myconObj.createStatement();
myresObj=mystatObj.executeQuery("Select * from Me.Food");
tableRateFood.setModel(DbUtils.resultSetToTableModel(myresObj));
}
catch (SQLException e){
e.printStackTrace();
}
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
try{
String sql = "update Me.Food set Name = '" + nameText.getText()
+ "',Type = '" + typeText.getText()
+ "', Rating = '" + ratingText.getText()
+ ", 'Vegetarian = '" + vegetarianText.getText()
+ "', ShownOnMenu = '" + showText.getText()
+ "' where Id = " + idText.getText();
//tried the following... did not work either
/*+ " Rating = " + Integer.parseInt(ratingText.getText()));*/
Statement update= myconObj.createStatement();
update.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "Updated successfully!");
}
catch(SQLException E){
E.printStackTrace();
}
getConnection();
}

最佳答案

您忘记了 ", 'Vegetarian = '"中的引述

在谈论构建查询字符串时,您应该避免 +-ing 值并依赖 prepared statements用sql参数代替。允许数据库缓存查询并避免sql注入(inject)攻击。并让您省去格式化的麻烦,考虑日期值。

关于java - 如何更新连接到 JTable 的数据库 (JavaDatabase) 中的一行条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58982018/

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