UCanAccess:向带有附件的表中插入新行时出现错误

时间:2017-02-21 20:48:18

标签: java access ucanaccess

经过几个小时的尝试和失败之后,我已经确定了UCanAccess在尝试将一个新行插入一个恰好具有附件类型字段的表时看起来好奇的行为。

具体来说,我在Access中有一个简单的表格如下:

   ID: Autonumber 
   FirstName: String (not required)
   Age: Numnber (not required)
   Picture: Attachment (not required)

我使用以下代码使用Java和UCanAccess向表中添加一行:

Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");  
String database = "jdbc:ucanaccess://" + dbFileName +  "";                           
Connection conn = DriverManager.getConnection(database, "", "");
Statement s = conn.createStatement();

String sql = "INSERT INTO Test(FirstName, Age) VALUES ('TestName',32)";
s.execute(sql);

代码产生以下错误:

java.lang.NullPointerException
    at net.ucanaccess.commands.InsertCommand.persist(InsertCommand.java:153)
    at net.ucanaccess.jdbc.UcanaccessConnection.flushIO(UcanaccessConnection.java:315)
    at net.ucanaccess.jdbc.UcanaccessConnection.commit(UcanaccessConnection.java:205)
    at net.ucanaccess.jdbc.AbstractExecute.executeBase(AbstractExecute.java:217)
    at net.ucanaccess.jdbc.Execute.execute(Execute.java:46)
    at net.ucanaccess.jdbc.UcanaccessStatement.execute(UcanaccessStatement.java:152)
    at projectmigrator.ProjectMigrator.tester2(ProjectMigrator.java:62)
    at projectmigrator.ProjectMigrator.main(ProjectMigrator.java:43)
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::4.0.1 null
    at net.ucanaccess.jdbc.UcanaccessStatement.execute(UcanaccessStatement.java:154)
    at projectmigrator.ProjectMigrator.tester2(ProjectMigrator.java:62)
    at projectmigrator.ProjectMigrator.main(ProjectMigrator.java:43)
Caused by: net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::4.0.1 null
    at net.ucanaccess.jdbc.UcanaccessConnection.commit(UcanaccessConnection.java:212)
    at net.ucanaccess.jdbc.AbstractExecute.executeBase(AbstractExecute.java:217)
    at net.ucanaccess.jdbc.Execute.execute(Execute.java:46)
    at net.ucanaccess.jdbc.UcanaccessStatement.execute(UcanaccessStatement.java:152)
    ... 2 more
Caused by: net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::4.0.1 null
    at net.ucanaccess.jdbc.UcanaccessConnection.flushIO(UcanaccessConnection.java:348)
    at net.ucanaccess.jdbc.UcanaccessConnection.commit(UcanaccessConnection.java:205)
    ... 5 more
Caused by: java.lang.NullPointerException
    at net.ucanaccess.commands.InsertCommand.persist(InsertCommand.java:153)
    at net.ucanaccess.jdbc.UcanaccessConnection.flushIO(UcanaccessConnection.java:315)
    ... 6 more

有趣的是,如果我将sql String替换为

,代码运行时没有错误
    String sql = "INSERT INTO Test(FirstName, Age, Photo) VALUES ('TestName',3224,'')";

在执行INSERT INTO包含不需要的附件类型字段的表时,我真的被迫提供类似空字符串的虚拟值(对于其他类型的字段(如数字或文本)不是这种情况)。这可能是UCanAcces中的错误还是我遗漏了什么?

(谢谢 - 我知道我应该将PreparedStatement用于这样的INSERT INTO语句,但为了这篇文章已经对插入进行了硬编码。)

1 个答案:

答案 0 :(得分:0)

此问题已在UCanAccess 4.0.2中修复。