相关疑难解决方法(0)

从java.sql.PreparedStatement获取查询

在我的代码我正在使用java.sql.PreparedStatement.

然后我执行该setString()方法来填充预准备语句的通配符.

executeQuery()调用方法并执行查询之前,有没有办法检索(并打印出)最终查询?我只想将其用于调试目的.

java jdbc prepared-statement

154
推荐指数
6
解决办法
28万
查看次数

创建数据库模式时是否可以使用Java中的Prepared Statement?

如果没有,是否有比字符串连接更安全的方法?

试过这个..

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)

java sql database prepared-statement

5
推荐指数
1
解决办法
920
查看次数

标签 统计

java ×2

prepared-statement ×2

database ×1

jdbc ×1

sql ×1