【发布时间】:2014-01-16 06:25:33
【问题描述】:
这个语句有什么问题导致我出现以下异常?
s.addBatch("CREATE TABLE category ( id SMALLINT NOT NULL GENERATED ALWAYS AS IDENTITY \n" +
"\t(START WITH 0, INCREMENT BY 1), title VARCHAR(100))\n" );
s.addBatch("CREATE TABLE task (id SMALLINT NOT NULL GENERATED ALWAYS AS IDENTITY \n" +
"\t(START WITH 0, INCREMENT BY 1), title VARCHAR(100), cat_id INT, visible BOOLEAN, " +
"deprecated BOOLEAN" +
"CONSTRAINT fk_cat_id FOREIGN KEY (cat_id)\n" +
"\tREFERENCES category(id))");
s.executeBatch();
第一个 addBatch 在我的运行代码中被注释,因为我之前创建了表。事实上,如果我不评论第一批,我会收到一条错误消息,指出该表已经存在并看到 this question 我知道这是检查 derby 中是否存在表的唯一方法。根据this documentation看到我的说法是正确的
java.sql.BatchUpdateException: Syntax error: Encountered "fk_cat_id" at line 2, column 119.
at org.apache.derby.impl.jdbc.Util.newBatchUpdateException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedStatement.executeLargeBatch(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedStatement.executeBatch(Unknown Source)
at model.DBConnection.createTables(DBConnection.java:48)
at model.DBConnection.<init>(DBConnection.java:33)
at model.DBConnection.<clinit>(DBConnection.java:10)
at test.DBTest.main(DBTest.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.sql.SQLSyntaxErrorException: Syntax error: Encountered "fk_cat_id" at line 2, column 119.
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedStatement.executeBatchElement(Unknown Source)
... 11 more
Caused by: java.sql.SQLException: Syntax error: Encountered "fk_cat_id" at line 2, column 119.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
... 19 more
Caused by: ERROR 42X01: Syntax error: Encountered "fk_cat_id" at line 2, column 119.
at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
at org.apache.derby.impl.sql.compile.ParserImpl.parseStatement(Unknown Source)
at org.apache.derby.impl.sql.GenericStatement.prepMinion(Unknown Source)
at org.apache.derby.impl.sql.GenericStatement.prepare(Unknown Source)
at org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext.prepareInternalStatement(Unknown Source)
... 13 more
【问题讨论】:
-
"deprecated BOOLEAN" +与"CONSTRAINT fk_cat_id FOREIGN KEY (cat_id)\n"没有任何区别,因此它连接为BOOLEANCONSTRAINT。尝试在BOOLEAN后面加一个空格。 -
啊!你说得对。但是我发现了更多错误,并发布了正确的代码作为答案。@PM77-1
标签: java foreign-keys derby sqlexception