首页 > 解决方案 > 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 命令)可以成功运行。任何人都可以帮我解决这个问题吗?谢谢

标签: javasql-serverms-accesssql-updatesqlconnection

解决方案


我通过更改代码解决了这个问题。我使用 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();
        }
    }

推荐阅读