在Java中创建一个小型模块化流程。但是,当我从我的网站调用SQL语句时,系统返回错误:
java.sql.SQLSyntaxErrorException: Syntax error: Encountered ";" at line 8, column 2.
以下是有问题的SQL文件的前几行以及用于检索数据的系统。
SQL:
CREATE TABLE "chat_logs"
(
"logId" BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
"logDate" INT DEFAULT NULL,
"logUser" BLOB,
"logMessage" BLOB,
PRIMARY KEY ("logId")
);
CREATE TABLE "configs"
(
"configId" BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
"configType" BLOB,
"configSettings" BLOB,
PRIMARY KEY ("configId")
);
...
检索脚本:
InputStream in = new URL("http://db.*******.com/derby/version1.00.sql").openStream();
return org.apache.commons.io.IOUtils.toString(in);
如何从一个分号应该出现的地方生成语法错误是没有意义的。
答案 0 :(得分:3)
是的,您需要将字符串拆分为单独的语句并分别运行:
String[] createTableStatements = allStatements.Split...
for (String createStatement : createTableStatements) {
try (Statement ps = connection.createStatement()) {
ps.executeUpdate(createStatement);
}
}