log4jdbc用于打印于SQL 同时打出绑定参数的值:
log4jdbc: http://code.google.com/p/log4jdbc/
另外一个对log4jdbc的扩展: http://code.google.com/p/log4jdbc-remix/
在官网首页已经将用法讲的很清楚了
JDBC 4 support was added with the JDK 1.6 release and adds many additional features over and above JDBC 3. However, the log4jdbc JDBC 4 driver can wrap a JDBC 3 or older driver and it's recommended that if you use JDK 1.6 or above, that you use the log4jdbc JDBC 4 driver that is compiled with JDK 1.6.
log4jdbc3-1.2.jar for JDBC 3 support in JDK 1.4 , JDK 1.5
log4jdbc4-1.2.jar for JDBC 4 support in JDK 1.6 , JDK 1.7
Set your JDBC driver class to net.sf.log4jdbc.DriverSpy in your application's configuration.
Driver Class Database Type
oracle.jdbc.driver.OracleDriver Older Oracle Driver
oracle.jdbc.OracleDriver Newer Oracle Driver
com.sybase.jdbc2.jdbc.SybDriver Sybase
net.sourceforge.jtds.jdbc.Driver jTDS SQL Server & Sybase driver
com.microsoft.jdbc.sqlserver.SQLServerDriver Microsoft SQL Server 2000 driver
com.microsoft.sqlserver.jdbc.SQLServerDriver Microsoft SQL Server 2005 driver
weblogic.jdbc.sqlserver.SQLServerDriver Weblogic SQL Server driver
com.informix.jdbc.IfxDriver Informix
org.apache.derby.jdbc.ClientDriver Apache Derby client/server driver, aka the Java DB
org.apache.derby.jdbc.EmbeddedDriver Apache Derby embedded driver, aka the Java DB
com.mysql.jdbc.Driver MySQL
org.postgresql.Driver PostgresSQL
org.hsqldb.jdbcDriver HSQLDB pure Java database
org.h2.Driver H2 pure Java database
Prepend jdbc:log4 to the normal jdbc url that you are using.
jdbc:derby://localhost:1527//db-derby-10.2.2.0-bin/databases/MyDatabase改为:
jdbc:log4jdbc:derby://localhost:1527//db-derby-10.2.2.0-bin/databases/MyDatabase
Set up your loggers.
Each of these logs can be set at either DEBUG, INFO or ERROR level.
DEBUG includes the class name and line number (if available) at which the SQL was executed. Use DEBUG level with extra care, as this imposes an additional performance penalty when in use.
INFO includes the SQL (or other information as applicable.)
ERROR will show the stack traces in the log output when SQLExceptions occur.
jdbc.sqlonly Logs only SQL. SQL executed within a prepared statement is automatically shown with it's bind arguments replaced with the data bound at that position, for greatly increased readability. 1.0
jdbc.sqltiming Logs the SQL, post-execution, including timing statistics on how long the SQL took to execute. 1.0
jdbc.audit Logs ALL JDBC calls except for ResultSets. This is a very voluminous output, and is not normally needed unless tracking down a specific JDBC problem. 1.0
jdbc.resultset Even more voluminous, because all calls to ResultSet objects are logged. 1.0
jdbc.connection Logs connection open and close events as well as dumping all open connection numbers. This is very useful for hunting down connection leak problems. 1.2alpha1
使用很简单,将JDBC URL修改,同时修改驱动即可
<property name="connection.url">jdbc:log4jdbc:h2:tcp://localhost/~/dbname</property>
<property name="connection.driver_class">net.sf.log4jdbc.DriverSpy</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
log4j中配置日志的输出
log4j.logger.jdbc.sqlonly=OFF
log4j.logger.jdbc.sqltiming=INFO
log4j.logger.jdbc.audit=OFF
log4j.logger.jdbc.resultset=OFF
log4j.logger.jdbc.connection=OFF