从java.sql.PreparedStatement获取查询 [英] Get query from java.sql.PreparedStatement

查看:425
本文介绍了从java.sql.PreparedStatement获取查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

I then execute the setString() method to populate the wildcards of the prepared statement.

有没有办法让我检索(并打印出来) )在调用 executeQuery()方法之前的最终查询并执行查询?我只是想将它用于调试目的。

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.

推荐答案

这在JDBC API合同中没有任何定义,但如果你是幸运,有问题的JDBC驱动程序可以通过调用 PreparedStatement#toString()来返回完整的SQL。即。

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.

System.out.println(preparedStatement);

根据我的经验,这样做的至少是PostgreSQL 8.x和MySQL 5。 x JDBC驱动程序。对于你的JDBC驱动程序不支持它的情况,最好的办法是使用一个语句包装器,它记录所有 setXxx()方法,最后在<$ c上填充一个SQL字符串$ c> toString()根据记录的信息。例如, Log4jdbc P6Spy

To my experience, the ones which do so are at least the PostgreSQL 8.x and MySQL 5.x JDBC drivers. For the case your JDBC driver doesn't support it, your best bet is using a statement wrapper which logs all setXxx() methods and finally populates a SQL string on toString() based on the logged information. For example Log4jdbc or P6Spy.

这篇关于从java.sql.PreparedStatement获取查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆