我正在尝试创建下表,但我不断收到错误。对我来说,似乎Java Db试图解析"创建表ORDER" as" ORDER BY"。我也尝试输入" CREATE TABLE"但是,正如预期的那样,没有结果。如何解决这个问题?
public void createTable() throws SQLException {
String createString = "create table ORDER "
+ "(ORDER_ID int NOT NULL, " + "ORDER_NUMBER int NOT NULL, "
+ "PERSON_ID int NOT NULL, " + "PRIMARY KEY (ORDER_ID), "
+ "FOREIGN KEY (PERSON_ID) REFERENCES PERSON (PERSON_ID))";
Statement stmt = null;
System.out.println(createString);
try {
DatabaseMetaData meta = con.getMetaData();
ResultSet tables = meta.getTables(null, null, "ORDER",
new String[] { "TABLE2" });
int size = 0;
while (tables.next()) {
size++;
}
if (size == 0) {
System.out.println("table created");
stmt = con.createStatement();
stmt.executeUpdate(createString);
}
} catch (SQLException e) {
System.out.println("No connection");
e.printStackTrace();
// TODO make exeption
} finally {
if (stmt != null) {
stmt.close();
}
}
}
错误
java.sql.SQLSyntaxErrorException: Syntax error: Encountered "ORDER" at line 1, column 14.
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.executeUpdate(Unknown Source)
at com.tuto.p4.OrderTable.createTable(OrderTable.java:38)
at com.tuto.p4.Main.main(Main.java:18)
Caused by: java.sql.SQLException: Синтаксическая ошибка: Encountered "ORDER" at line 1, column 14.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
... 10 more
Caused by: ERROR 42X01: Syntax error: Encountered "ORDER" at line 1, column 14.
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)
... 4 more
答案 0 :(得分:2)
我相信"订购"是保留字,因此您不应将其用作表名或列名。尝试描述你的功能的简单同义词,比如" POSITION"或者" SORTED"等等。祝你好运!
编辑:如果你真的必须使用那个词,你可以尝试用引号括起来,比如
create table `ORDER`
强制sql引擎尝试将其作为字符串读取。
答案 1 :(得分:0)
或者您可以使用这样的括号来创建表格:
create table `ORDER` ...