我有一个使用HSQLDB的JAVAFX项目。当我试图设置一个表的源时,我得到了一个异常,我想我能理解,但由于我不能修复它,我想我不能理解它。我的SQL是:
DROP TABLE temp IF EXISTS;
CREATE TEXT TABLE temp(text_data LONGVARCHAR(10000));
SET TABLE temp SOURCE ?;
INSERT INTO log(typ, json) SELECT SUBSTRING(text_data, 3, LOCATE('"', text_data, 3)-3),text_data FROM temp WHERE text_data <> '';
DROP TABLE temp IF EXISTS;在这里,多个语句不知何故对我不起作用,现在这应该不是问题。我将上面的sql分成一个字符串的ArrayList,每一行都是一个元素。所以我得到了这个Java代码:
s = c.createStatement();
for (String sql : sqls) {
System.out.println("sql: " + sql);
if (sql.contains("?")) {
System.out.println("in ? part");
PreparedStatement ps = c.prepareStatement(sql);
ps.setString(1, path.toUri().toString() + ";encoding=UTF-8;ignore_first=false;fs=\\n\\r");
System.out.println("ps prepared" + ps.toString());
ps.execute();
} else {
s.execute(sql);
}
}我的应用程序在PreparedStatement ps = c.prepareStatement(sql);行失败,出现以下异常:
java.sql.SQLSyntaxErrorException: unexpected token: ? in statement [SET TABLE temp SOURCE ?;]
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.<init>(Unknown Source)
at org.hsqldb.jdbc.JDBCConnection.prepareStatement(Unknown Source)
at myfile in the line I pointed out above
at anotherofmyfiles
at javafx.concurrent.Task$TaskCallable.call(Task.java:1423)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.hsqldb.HsqlException: unexpected token: ?
at org.hsqldb.error.Error.parseError(Unknown Source)
at org.hsqldb.ParserBase.unexpectedToken(Unknown Source)
at org.hsqldb.ParserBase.checkIsValue(Unknown Source)
at org.hsqldb.ParserBase.readQuotedString(Unknown Source)
at org.hsqldb.ParserCommand.compileTableSource(Unknown Source)
at org.hsqldb.ParserCommand.compileSetTable(Unknown Source)
at org.hsqldb.ParserCommand.compileSet(Unknown Source)
at org.hsqldb.ParserCommand.compilePart(Unknown Source)
at org.hsqldb.ParserCommand.compileStatement(Unknown Source)
at org.hsqldb.Session.compileStatement(Unknown Source)
at org.hsqldb.StatementManager.compile(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
... 7 more在此之前的输出中:
sql: DROP TABLE temp IF EXISTS;
sql: CREATE TEXT TABLE temp(text_data LONGVARCHAR(10000));
sql: SET TABLE temp SOURCE ?;
in ? part我知道ps.setString(1, path.toUri().toString() + ";encoding=UTF-8;ignore_first=false;fs=\\n\\r");在语义上可能不是完全正确的,但在语法上它应该可以工作,因为错误是在前面的,所以它不应该是导致这个错误的原因。当我在没有这行的情况下运行应用程序时,同样的错误也发生了。
所以我的问题是:SET TABLE temp SOURCE ?;到底出了什么问题?为什么我不能在Java中使用它作为PreparedStatement?据我从documentation中了解,它的语法是SET TABLE <tablename> SOURCE <quoted_filename_and_options>,其中<quoted_filename_and_options>是一个字符串。我不能用Java编写吗?
发布于 2017-02-14 18:26:44
PreparedStatements被发送到底层SQL引擎进行编译。允许您使用参数的位置取决于驱动程序和引擎。通常,它们只在非常特定的地方被支持,否则就不能真正编译语句。
考虑一个只包含"?“的PreparedStatement,并提供一个参数:
ps.setString(1, "SELECT * FROM myTable");这不能被编译,所以被拒绝了。
因此,大多数SQL数据库仅在通常出现简单值的位置支持INSERT/UPDATE/SELECTS中的参数。它们不能用于字段名、表名等。
https://stackoverflow.com/questions/42222261
复制相似问题