在我的代码我正在使用java.sql.PreparedStatement.
然后我执行该setString()方法来填充预准备语句的通配符.
在executeQuery()调用方法并执行查询之前,有没有办法检索(并打印出)最终查询?我只想将其用于调试目的.
如果没有,是否有比字符串连接更安全的方法?
试过这个..
final String CREATE_SCHEMA_SQL = "CREATE SCHEMA IF NOT EXISTS ?";
List<String> schemas = // ["001", "002", "003"];
for (String schema : schemas) {
try (Connection connection = dataSource.getConnection();
PreparedStatement createSchemaStatement =
connection.prepareStatement(CREATE_SCHEMA_SQL)) {
createSchemaStatement.setString(1, schema);
createSchemaStatement.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException("Could not create tenant schema");
}
}
Run Code Online (Sandbox Code Playgroud)
产生这个错误:
org.postgresql.util.PSQLException: ERROR: syntax error at or near "$1"
Position: 29
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2674)
Run Code Online (Sandbox Code Playgroud)