Logging PreparedStatements in Java

前端 未结 4 1555
终归单人心
终归单人心 2020-12-24 03:22

One thing that always been a pain is to log SQL (JDBC) errors when you have a PreparedStatement instead of the query itself.

You always end up with messages like:

4条回答
  •  醉梦人生
    2020-12-24 04:12

    1. If you are using MySQL, MySQL Connector's PreparedStatement.toString() does include the bound parameters. Though third-party connection pools may break this.

    2. Sub-class PreparedStatement to build up the query string as parameters are added. There's no way to extract the SQL from a PreparedStatement, as it uses a compiled binary form.

    LoggedPreparedStatement looks promising, though I haven't tried it.

    One advantage of these over a proxy driver that logs all queries is that you can modify the query string before logging it. For example in a PCI environment you might want to mask card numbers.

提交回复
热议问题