Logging PreparedStatements in Java

前端 未结 4 1595
终归单人心
终归单人心 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:24

    I tried log4jdbc and it did the job for me.

    SECURITY NOTE: As of today August 2011, the logged results of a log4jdbc prepared statement are NOT SAFE to execute. They can be used for analysis, but should NEVER be fed back into a DBMS.

    Example of log generated by logjdbc:

    2010/08/12 16:30:56 jdbc.sqlonly org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105) 8. INSERT INTO A_TABLE (ID_FILE,CODE1,ID_G,ID_SEQUENCE,REF,NAME,BAR,DRINK_ID,AMOUNT,DESCRIPTION,STATUS,CODE2,REJECT_DESCR,ID_CUST_REJ) VALUES (2,'123',1,'2','aa','awe',null,'0123',4317.95,'Rccc','0',null,null,null)

    The library is very easy to setup:


    My configuration with HSQLDB :

    jdbc.url=jdbc:log4jdbc:hsqldb:mem:sample
    

    With Oracle :

    jdbc.url=jdbc:log4jdbc:oracle:thin:@mybdd:1521:smt
    jdbc.driverClass=net.sf.log4jdbc.DriverSpy
    

    logback.xml :

    
    

    Too bad it wasn't on a maven repository, but still useful.
    From what I tried, if you set

    You will only get the statements in error, however, I don't know if this library has an impact on performance.

提交回复
热议问题