How to get values of bind parameters from Oracle JDBC PreparedStatement object

后端 未结 2 660
南方客
南方客 2021-02-06 03:26

I want to implement logging of all executed statements with actual bind parameters when using Oracle JDBC. And I would prefer that I could create such logging method only passin

相关标签:
2条回答
  • 2021-02-06 03:59

    You can have a look at p6spy , it's a proxy to your database driver that allows monitoring and logging.

    0 讨论(0)
  • 2021-02-06 04:22

    Most logging framework have the notion for Nested Diagnostic Context. You could save your query and its parameters there when you fill up the prepared statement.

    Or, perhaps, do it in one step:

    PreparedStatement fillAndLog(Connection conn, String query, Object... args) {
        int i = 0;
        PreparedStatement pstmt = conn.prepareStatement(query);
        for (Object o : args) {
           if (o instanceof String) {
               pstmt.setString(i, (String)o);
           } // else...
           i++;
        }
        log.debug(String.format(query.replaceAll("\\?", "%s"), args));
        return pstmt;
    }
    
    0 讨论(0)
提交回复
热议问题