首页 > 解决方案 > 是什么导致错误“用户缺少权限或找不到对象:FieldName”

问题描述

使用 MS Access 数据库保存我的信息,它一直运行良好,直到此时我创建了一个名为CreateLicense()的新方法,它接收我创建的许可证类并将其写入数据库。

运行代码时,它会吐出一条错误消息,指出“用户缺少权限或找不到对象:已过期”

已经尝试更改 Expired 的字段名称,以防它是保留字或其他内容。还尝试删除 Expired 字段,但随后标记相同的错误,但改为使用CONTACT


INSERT INTO Licenses (Name, DateRedeemed, Expired, LicenseLength, Contact) VALUES ('name','25/Jun/2019','02/Jul/2019','2','contact')
user lacks privilege or object not found: EXPIRED

这是我的 SQL 语句的输出 这是代码

public boolean CreateLicense(License newLicense) {
        try {
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
            File currentDir = new File(""); // Creates a new file
            String newFilePath = "jdbc:ucanaccess://" + currentDir.getAbsolutePath().toString() + "\\store\\data\\database1.accdb";
            Connection conn = DriverManager.getConnection(newFilePath);
            Statement stmt = conn.createStatement();
            stmt.executeUpdate("INSERT INTO Licenses (Name, DateRedeemed, Expired, LicenseLength, Contact) VALUES "
                    //Values for the SQL Statement
                    + "('" + newLicense.getName()//Start Line
                    + "','" + newLicense.getRedeemed()//Middle Lines
                    + "','" + newLicense.getExpired()//Middle Lines
                    + "','" + newLicense.getLicenseLength()//Middle Lines
                    + "','" + newLicense.getContact()+ "')");//End Line

            conn.close();
            return true;
        } catch (Exception ex) {
            String message = ex.getMessage();
            System.out.println(message);
            return false;
        }
    }

帮助解决的一些附加信息表名称是许可证 字段名称及其类型

更新 我使用访问中的查询设计创建了一个查询,这很好地向数据库提供了信息,所以问题不是 sql。

更新 当我重命名数据库中的表并运行应用程序时,我得到了同样的错误,但是用表名代替,我将创建一个新数据库,看看是否能解决任何问题

找到解决方案

标签: sqlms-accessjdbcucanaccess

解决方案


LicenseLength 是一个 Int 但你用引号将它括起来,就像它是一个字符串一样。另外,您是否尝试过使用参数化查询,例如

String sql = "INSERT INTO Licenses (Name, DateRedeemed, Expired, LicenseLength, Contact) VALUES (?, ? ,?, ?, ?)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, newLicense.getName());
stmt.setString(2, newLicense.getRedeemed());
stmt.setString(3, newLicense.getExpired());
stmt.setInt(4, newLicense.getLicenseLength());
stmt.setString(5, newLicense.getContact());

推荐阅读