我正在尝试创建一个类似于我之前创建的现成表(一个模板,如果你愿意的话)的表,其中唯一的变量应该是表名。
这是我到目前为止所尝试的:我将模板表导出到 mysql 代码并将代码复制到preparedStatement 对象,如下所示:
createNewLineTableStatement = constantLink.prepareStatement("CREATE TABLE IF NOT EXISTS ? (" +
" `index` int(5) NOT NULL," +
" `station` int(5) NOT NULL," +
" PRIMARY KEY (`index`)," +
" UNIQUE KEY `station` (`station`)" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;\");");
比我尝试通过调用以下函数来执行代码:
private static boolean createNewLineTable(String tableName) throws SQLException{
createNewLineTableStatement.setString(1, tableName);
if (createNewLineTableStatement.executeUpdate() == Statement.EXECUTE_FAILED)
return false;
return true;
}
但我收到语法错误异常:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''line_37_var_1' ( `index` int(5) NOT NULL, `station` int(5) NOT NULL, PRIMARY' at line 1
如何修复代码?或者是否有更清洁、更好的方法来做同样的事情?也许创建一个带有用户变量的脚本?我想到了这一点,但我以前从未使用过 .sql 脚本。