sql - 是什么导致错误“用户缺少权限或找不到对象: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;
}
}
帮助解决的一些附加信息表名称是许可证 字段名称及其类型
- ID -
Autonumber
- 姓名 -
String
- 赎回日期 -
String
- 已到期 -
String
- 许可证长度-
int
- 接触-
String
更新 我使用访问中的查询设计创建了一个查询,这很好地向数据库提供了信息,所以问题不是 sql。
更新 当我重命名数据库中的表并运行应用程序时,我得到了同样的错误,但是用表名代替,我将创建一个新数据库,看看是否能解决任何问题
找到解决方案
解决方案
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());
推荐阅读
- javascript - TypeScript:是否可以动态更改枚举的值
- c++ - 错误 LNK2019 未解析的外部符号 __imp_freopen 在函数 qh_initflags 中引用
- oracle - 将 ETL 作业从 IBM Datastage 转换为 Apache Spark
- java - 使用 IsEmpty() 和 isFull() JUnit 测试来测试堆栈是否为空/满
- html - 带有边框半径的 css 图像
- node.js - Cpanel 上的 Nodejs 环境变量
- java - 为什么java队列在某些情况下无法轮询数据?
- python - 为什么我在 pygame 上的圈子不断消失和重新生成?
- git - 将两台不同机器上的两个预先存在的本地文件夹合并到一个共享的在线存储库中
- unreal-engine4 - 在虚幻引擎 4 中构建光照后的黑色树叶