java - net.ucanaccess.jdbc.UcanaccessSQLException:UCAExc:::4.0.2 用户缺少权限或找不到对象:WHERE
问题描述
public void update(String tempid,String name,String sid,int age,String course,String department) throws SQLException {
try {
int result = 0;
s = con.createStatement();
result = s.executeUpdate("Update db set Name="+name+", Age="+age+
",Course=" +course+ ",Department="+department+", where StudentID="+tempid);
if(result > 0) {
System.out.println("更新成功");
}
}catch(Exception e) {
e.printStackTrace();
}finally {
con.close();
}
}
这个编码部分有什么问题?只有更新命令不能成功运行,其他如插入、删除、选择(sql 命令)可以成功运行。任何人都可以帮我解决这个问题吗?谢谢
解决方案
我通过更改代码解决了这个问题。我使用 PreparedStatement 从 input 获取数据,然后更新到 database(db) ,错误得到解决。
public void update(String tempid,String name,String sid,int age,String course,String department) throws SQLException {
try {
int result = 0;
PreparedStatement pst = con.prepareStatement("Update db set Name=? , StudentID=? , Age=? , Course=? , Department=? where StudentID=?");
pst.setString(1, name);
pst.setString(2, sid);
pst.setInt(3, age);
pst.setString(4, course);
pst.setString(5, department);
pst.setString(6, tempid);
result = pst.executeUpdate();
pst.close();
if(result > 0) {
System.out.println("更新成功");
}
}catch(Exception e) {
e.printStackTrace();
}finally {
con.close();
}
}
推荐阅读
- vue.js - 根据条件将不同的子元素添加到动态 Vue 组件中?
- php - Wordpress 页面内容和帖子在同一页面中
- javafx - JavaFX Raspberry WiFi 搜索器
- windows - 无法打开acer switch 10;黑屏
- java - 当手指移过按钮时在 logcat 中显示文本
- gradle - Gradle Kotlin allprojects 依赖导致构建失败
- php - 正则表达式匹配我不想再匹配的东西
- wordpress - Wordpress 不显示超过 2 天的帖子
- c# - WPF MVVM 与 Razor 页面 MVVM
- redis - Spring Boot 微服务未与 Redis 通信