我正在尝试使用MySQL中的预准备语句创建数据库.在这里,我传递的参数如图所示.
PreparedStatement createDbStatement = connection.prepareStatement("CREATE DATABASE ?");
createDbStatement.setString(1, "first_database");
createDbStatement.execute();
connection.commit();
Run Code Online (Sandbox Code Playgroud)
但我收到语法错误.是否可以使用预准备语句创建表和数据库?如果没有,请建议另一种方法.
in a PreparedStatement
can只能用于绑定值(例如,在where
in values
子句中的条件中),而不是对象名称.因此,您无法使用它来绑定数据库的名称.
您可以使用字符串操作来添加数据库名称,但在这种情况下,使用PreparedStatement
s 确实没有任何好处,您应该只使用一个Statement
:
String dbName = "first_database";
Statement createDbStatement = connection.createStatement();
createDbStatement.execute("CREATE DATABASE " + dbName);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1681 次 |
最近记录: |