这句话给我带来以下异常的问题是什么?
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中是否存在表的唯一方法。根据{{3}}
,我看到我的陈述是正确的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
答案 0 :(得分:2)
sql语句中有很多错误!
category
和cat_id
中的ID类型不匹配。PM77-1
注意到空格不受尊重。无论如何,工作代码如下:
s.addBatch("CREATE TABLE category ( id INT NOT NULL GENERATED ALWAYS AS IDENTITY \n" +
"\t(START WITH 0, INCREMENT BY 1), title VARCHAR(100), " +
"\tCONSTRAINT category_pk_id PRIMARY KEY (id))\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," +
"\t CONSTRAINT task_pk_id PRIMARY KEY (id)," +
"\t CONSTRAINT fk_cat_id FOREIGN KEY (cat_id)\n" +
"\t REFERENCES category(id))");
但是代码中仍然会出现错误,这些操作不是原子操作,如果发生了一个错误,而下一个错误则第一个没有回滚。它没有解决。