我正在使用带有 MySQL 服务器的 PreparedStatement
。
例子:
String myQuery = "select id from user where name = ?";
PreparedStatement stmt = sqlConnection.prepareStatement(myQuery);
stmt.setString(1, "test");
stmt.executeQUery();
ResultSet rs = stmt.getResultSet();
如何接收即将在 MySQL 服务器
上执行的完整 SQL 查询?
它不是 JDBC 规范强制要求的,但是一些 JDBC 驱动程序让 PreparedStatement
的 toString
返回将要运行的查询的排序,以及 MySQL 的连接器/J 恰好有这种行为(或者至少几年前有)。
String myQuery = "select id from user where name = ?";
PreparedStatement stmt = sqlConnection.prepareStatement(myQuery);
stmt.setString(1, "test");
System.out.println(stmt); // May do what you want!