`
xzcgeorge
  • 浏览: 32046 次
  • 性别: Icon_minigender_1
  • 来自: 珠海
社区版块
存档分类
最新评论

how to use log4jdbc to log SQL

 
阅读更多
1. download it from http://code.google.com/p/log4jdbc/

   log4jdbc4-1.2beta2.jar:  for JDBC 4 (JDK 1.6 and 1.7)   Featured  Jun 2010   62.2 KB  4647   
   log4jdbc3-1.2beta2.jar:  for JDBC 3 (JDK 1.4 and 1.5)   Featured  Jun 2010   50.4 KB  2146  

I uses log4jdbc4-1.2beta2.jar donwload it ot desktop.


2. add to your maven server:
http://localhost:8081/nexus/index.html#view-repositories  log in as admin/admin123
click  Repositories on the left menu.
then 3rd party --> then on the bottom right panel click "Artifact Upload":

  GAV Definition: GAV Parameters.
  Group: net.sf.log4jdbc
  Artifact: log4jdbc4
  version: 1.2beta2
  Packaging: jar
Then click "Select Artifacct(s) to Upload... to find the jar file. 
Click "Add Artifact" button
click "Upload Artifact(s)

The artifact is stored in the nexus now. to verify it: click "Brownse Storage" --> Refresh --> net.sf..... you find it.,


3. to use it in your project:

   3.1 add the pom to your pom file:
           <dependency>
               <groupId>net.sf.log4jdbc</groupId>
               <artifactId>log4jdbc4</artifactId>
               <version>1.2beta2</version>
           </dependency>

 
  3.2 suppose that you have setup slf4j log system already.
 
  3.3 Set your JDBC driver class to
  net.sf.log4jdbc.DriverSpy
 
  in your application's configuration
 
 
  3.4 Prepend log4jdbc to the normal jdbc url that you are using.
 
For example, if your normal jdbc url is

      original: jdbc:derby://localhost:1527//db-derby-10.2.2.0-bin/databases/MyDatabase
      new             jdbc:log4jdbc:derby://localhost:1527//db-derby-10.2.2.0-bin/databases/MyDatabase
     
      then You would change it to:
     
      jdbc:log4jdbc:derby://localhost:1527//db-derby-10.2.2.0-bin/databases/MyDatabase
      to use log4jdbc.  
     
   Here is the example:
  
       <bean id="dataSource"
             class="org.apache.commons.dbcp.BasicDataSource"
             destroy-method="close">
           <property name="driverClassName" value="net.sf.log4jdbc.DriverSpy"/>
           <property name="url" value="jdbc:log4jdbc:hsqldb:mem:test"/>
           <property name="username" value="sa"/>
           <property name="password" value=""/>
       </bean>

Original one:

    <bean id="dataSource"
          class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
        <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
        <property name="url" value="jdbc:hsqldb:mem:test"/>
        <property name="username" value="sa"/>
        <property name="password" value=""/>
    </bean>



// if you can do it like that, you can wrap it  :


  final Connection conn = ds().getConnection();
   Connection connection = new net.sf.log4jdbc.ConnectionSpy(conn);
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics