从 java.sql.PreparedStatement 获取查询
在我的代码中,我使用java.sql.PreparedStatement
。
然后,我执行 setString()
方法来填充准备好的语句的通配符。
有没有办法让我在调用 executeQuery()
方法并执行查询之前检索(并打印出)最终查询?我只是想要这个用于调试目的。
In my code I am using java.sql.PreparedStatement
.
I then execute the setString()
method to populate the wildcards of the prepared statement.
Is there a way for me to retrieve (and print out) the final query before the executeQuery()
method is called and the query is executed? I Just want this for debugging purposes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
JDBC API 契约中没有对此进行定义,但如果您幸运,相关 JDBC 驱动程序可能只需调用
PreparedStatement#toString()
即可返回完整的 SQL。即根据我的经验,目前这样做的至少是 PostgreSQL 8.x 和 MySQL 5.x JDBC 驱动程序。
如果您的 JDBC 驱动程序不支持它,最好的选择是使用语句包装器,它记录对
setXxx()
方法的所有调用,并最终在toString() 上填充 SQL 字符串
基于记录的信息。执行此操作的现有库是 P6Spy。同时,向 JDBC 驱动程序的开发团队发布增强请求,并希望他们也实现所需的toString()
行为。This is nowhere definied in the JDBC API contract, but if you're lucky, the JDBC driver in question may return the complete SQL by just calling
PreparedStatement#toString()
. I.e.To my experience, the ones which currently do so are at least the PostgreSQL 8.x and MySQL 5.x JDBC drivers.
In the case that your JDBC driver doesn't support it, your best bet is using a statement wrapper which records all calls to
setXxx()
methods and finally populates a SQL string ontoString()
based on the recorded information. An existing library which does that is P6Spy. In the meanwhile, post an enhancement request to the development team of your JDBC driver and hope that they'll implement the desiredtoString()
behavior as well.设置绑定值后,您可以尝试在准备好的语句上调用
toString()
。当您使用 JDBC MySQL 驱动程序时,这有效,但我不确定在其他情况下是否有效。您可能必须跟踪您制作的所有装订,然后将其打印出来。
上述代码的示例输出。
You could try calling
toString()
on the prepared statement after you've set the bind values.This works when you use the JDBC MySQL driver, but I'm not sure if it will in other cases. You may have to keep track of all the bindings you make and then print those out.
Sample output from above code.
对于那些正在寻找 Oracle 解决方案的人,我从 Log4Jdbc 的代码中创建了一个方法。您将需要提供查询和传递给 preparedStatement 的参数,因为从中检索它们有点痛苦:
For those of you looking for a solution for Oracle, I made a method from the code of Log4Jdbc. You will need to provide the query and the parameters passed to the preparedStatement since retrieving them from it is a bit of a pain:
您可以将 log4jdbc 添加到您的项目中。这增加了 sql 命令执行时的日志记录以及许多其他信息。
http://code.google.com/p/log4jdbc/wiki/FAQ
You can add log4jdbc to your project. This adds logging of sql commands as they execute + a lot of other information.
http://code.google.com/p/log4jdbc/wiki/FAQ
如果您只想记录查询,则将 'logger' 和 'profileSQL' 添加到 jdbc url:
然后您将得到以下 SQL 语句:
默认记录器为:
Mysql jdbc 属性列表: https://dev.mysql.com/doc/connector-j/en /connector-j-reference-configuration-properties.html
If you only want to log the query, then add 'logger' and 'profileSQL' to the jdbc url:
Then you will get the SQL statement below:
The default logger is:
Mysql jdbc property list: https://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html
我已经制定了解决方法来解决这个问题。
请访问以下链接了解更多详情
http://code-outofbox.blogspot。 com/2015/07/java-prepared-statement-print-values.html
解决方案:
I have made a workaround to solve this problem.
Visit the below link for more details
http://code-outofbox.blogspot.com/2015/07/java-prepared-statement-print-values.html
Solution:
有点黑客,但对我来说效果很好:
A bit of a hack, but it works fine for me:
我认为可以在数据库和您的应用程序之间放置代理,然后观察通信。我不知道你会用什么软件来做到这一点。
I would assume it's possible to place a proxy between the DB and your app then observe the communication. I'm not familiar with what software you would use to do this.