【发布时间】:2019-06-25 10:17:05
【问题描述】:
使用 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;
}
}
一些额外的信息来帮助解决 表名是Licenses 字段名称及其类型
- ID -
Autonumber - 姓名 -
String - 赎回日期 -
String - 已过期 -
String - 许可证长度-
int - 联系方式-
String
更新 我使用 access 中的查询设计创建了一个查询,这很好地向数据库提供了信息,所以问题不是 sql。
更新 当我重命名数据库中的表并运行应用程序时,我得到了同样的错误,但是使用了表名,我将创建一个新数据库,看看是否能解决任何问题
找到解决方案
【问题讨论】:
-
请不要将值连接到 SQL 字符串中。学会正确使用
PreparedStatement
标签: sql ms-access jdbc ucanaccess