原博文出自于:  http://www.cnblogs.com/xdp-gacl/p/4081848.html      感谢!

一、log4jdbc的简单介绍

  使用log4jdbc在不改变原有代码的情况下,就可以收集执行的SQL文和JDBC执行情况。

  平时开发使用的ibatis,hibernate,spring jdbc的sql日志信息,有一点个缺点是占位符与参数是分开打印的,如果想要拷贝sql至PLSQL Developer客户端直接执行,需要自己拼凑sql。而log4jdbc是在jdbc层的一个日志框架,可以将占位符与参数全部合并在一起显示,方便直接拷贝sql在PLSQL Developer等客户端直接执行,加快调试速度。

二、log4jdbc的使用

  下载log4jdbc的jar包log4jdbc-1.2.jar以及依赖的相关Jar包log4j-1.2.17.jar、slf4j-api-1.6.0.jar、slf4j-log4j12-1.7.7.jar,如下图所示:

  aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAVMAAAFpCAIAAACuyFzoAAAgAElEQVR4nO2cfZAU5Z3H+x8rfx5VSVXiVV1pjKlgUgcVKmVdrhLWS3KKeBc9qzbRRG8p40vuMCoEzyWKu6wuokIkCrho2FmiCyqgeOvq+pZZICxwRckCORg1uju8LssuNwK7M8vu5rk/Wsfufl766e5nuvuZ5/upp6Z6ep6X39PT3/k+M9P9swgAwDws5t5NL6yJOQ4AQJxURPl9fdmGTKYmk23IZPr6slG6AgBUArbyo9DXl63JZuuy2e7u7ra252syWYgfgLShwPP3bV5YLoSQhmy2LpPNZDPZvoZsNlPXkK1ryCiJFQCgiqie37tpoftpfU0m293dnc3WNWRqGjI1NQ01NXWZiKMAANTyufK3bDtQ39JJly3bDtDNVi1+dNXiR1ctXnr+ZOv5k62jfauH9j1+ZOui9/97XkMm05DJNmQaGupq6hrqauoaGhoavKN+RsigqYbMPfZOmYEsN+GiAkAjPj/L61s6mTWY+x+9f3nz73aRT7bRpa8vW5PJ1mSyttvX1HF/5AunMXlxlqvJKN/zGCieQPUBSAMu5U9MjE9OflomJsYnJs4XR8/yPhHmz3uKnOpgFPu3/YZMTV22sbFR8PNeCM0EEqe88kPHE7oVAMniUn6xOFIs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eK+cFS/lQxP1hqqv+XpAMEIG6M8PwH7/1x38miXfpPFvtPFvtOlspPH/yvH1duaADSiRGef/+Ca8Ul6QABiBsjPB8A4AHKB8BEqmG1z0iwAQAQopPn7+yc5ykfHXoLCTYACIFOnr+zc57zBvvz45PnxyeRYAOAEOjk+X/q+HVxbLxcxicmxycmAyXYkLwwTtUldLgOD6QWnU7Nra/eZ99mbxfb89Um2CgD0YLqRifPf/flhWdGxj1FbYKNMlA+qG50Or/f3HR/YWTcWb54yRVfvOQKmQQbhJ9LQ3wrjqchfeNNuOFwZw5IlnR5vvix86UHy7fZ28VWvr393t4dvqPQIg+9IaPY6Df2AlAhdDrntmxYXL7N3i628u3tLRsW+/YQTvm+d+DKD8fsBMoH8ZMWz5dh4/PNx0+POYutfHt74/PNvj1E8XxPq6CeLzMoALGh0zm3ft0jR4fGjg6NldX+xUuuuPWeT3euX/eIuLnHuulv3Z794pqeCrwR6dGZ3QIQMzp5/nOZx/KDpfxgybZ6uzy/6W1753OZx5IO0ItY1dA8SBCdTr7M75f3nSz1nSw9v+ntsuztPR8PlDK/V/A3vhJkzByGD5JFJ89/9pkn/nKiyCvPPvNE0gECoA06eU5Ly+/EJekAAdAGnTwfAKAKKB8AE9FptQ8AUMX/A1JWZYb34NlHAAAAAElFTkSuQmCC" alt="" />

  配置log4j的log4j.properties的配置文件,如下所示:

log4j.logger.jdbc.sqlonly=DEBUG,console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %m%n%n
log4j.logger.jdbc.sqltiming=INFO,console
log4j.logger.jdbc.connection=INFO,console

  修改dbconfig.properties配置文件的url和driverClassName

 1 url:jdbc:log4jdbc:mysql://localhost:3306/xdptest
2 driverClassName:net.sf.log4jdbc.DriverSpy
3 username:root
4 password:root
5 filters:stat
6 maxActive:200
7 initialSize:20
8 maxWait:60000
9 minIdle:10
10 timeBetweenEvictionRunsMillis:60000
11 minEvictableIdleTimeMillis:300000
12 validationQuery:SELECT 'x'
13 testWhileIdle:true
14 testOnBorrow:false
15 testOnReturn:false
16 removeAbandoned:false
17 removeAbandonedTimeout:1800
18 logAbandoned:true

  经过这样的配置之后,就可以使用log4jdbc记录应用系统执行的SQL信息了。

  编写一个测试Servlet进行测试,代码如下:

 1 /**
2 *
3 */
4 package me.gacl.web.controller;
5
6 import java.io.IOException;
7 import java.sql.Connection;
8 import java.sql.PreparedStatement;
9 import java.sql.SQLException;
10 import java.util.UUID;
11 import javax.servlet.ServletException;
12 import javax.servlet.http.HttpServlet;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15 import me.gacl.util.DataSourceUtil;
16
17 /**
18 * <p>ClassName: TestServlet<p>
19 * <p>Description: <p>
20 * <p>Company:广州利迪网络科技有限公司 <p>
21 * @author xudp
22 * @version 1.0 V
23 * @createTime 2014-11-5 下午01:49:49
24 */
25 public class TestServlet extends HttpServlet {
26
27 public void doGet(HttpServletRequest request, HttpServletResponse response)
28 throws ServletException, IOException {
29 String sql = "INSERT INTO LD_USER(ID,USERNAME,USERCODE,PASSWORD) VALUES(?,?,?,?)";
30 Connection connection = DataSourceUtil.getConnection();
31 PreparedStatement pstmt = null;
32 try {
33 pstmt = connection.prepareStatement(sql);
34 pstmt.setString(1, UUID.randomUUID().toString());
35 pstmt.setString(2, "孤傲苍狼");
36 pstmt.setString(3, "gacl");
37 pstmt.setString(4, "xdp");
38 int executeResult = pstmt.executeUpdate();
39 } catch (SQLException e) {
40 e.printStackTrace();
41 }finally{
42 try {
43 pstmt.close();
44 connection.close();
45 } catch (SQLException e) {
46 e.printStackTrace();
47 }
48 }
49 }
50
51 public void doPost(HttpServletRequest request, HttpServletResponse response)
52 throws ServletException, IOException {
53 this.doGet(request, response);
54 }
55 }

  访问TestServlet,执行doGet方法往数据库中插入数据,log4jdbc记录的SQL信息如下图所示:

  aaarticlea/png;base64,iVBORw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3o+nILa0yYKfuZLurerfKF2dGs94KiT1HzufdmFF7zfNu6cczGbPTvYJ7Phn79ZwgBAAAAAH4lb+yY1XdYdnmQvGfo179OmLn2v1OYGR9ae28VYZbUetocz/NhgB0zAAAAAIBbeOszZtt3W7g17afDDhWh6qfff3i+cHcqkOLFu3TT6GlV2E8Uh7P1J/icMEtd7djIEsIscdBEOZrnwgAAAAAAAPwob34rY/baXJzNN2eYL/9wv/0j2U6zX+iRfkDq+KqKmLwZ6X/XvLCfvPEXWx8zK98F3PXQ8b88rj5DSaOdX5Z/tnR8HcrxJmT6LSgdeVB53s9BmAEAAAAA3MJ/7XfMwIffMQMAAAAAuJERYQYAAAAAAAAfAGEGAAAAAABwMwgzAAAAAACAm0GYAQAAAAAA3AzCDAAAAAAA4Gb+D5EyWhWjTXAIAAAAAElFTkSuQmCC" alt="" width="950" height="38" />

  可以看到,log4jdbc记录出了SQL的执行的时间,执行时的参数以及执行耗时。这对于我们在开发中查找SQL错误是非常有帮助的。

转】使用log4jdbc记录SQL信息的更多相关文章

  1. 使用log4jdbc记录SQL信息

    一.log4jdbc的简单介绍 使用log4jdbc在不改变原有代码的情况下,就可以收集执行的SQL文和JDBC执行情况. 平时开发使用的ibatis,hibernate,spring jdbc的sq ...

  2. Servlet开发 中使用 log4jdbc 记录 hibernate 的 SQL信息

    一.前言 使用log4jdbc在不改变原有代码的情况下,就可以收集执行的SQL文和JDBC执行情况. 平时开发使用的ibatis,hibernate,spring jdbc的sql日志信息,有一点个缺 ...

  3. spring-boot记录sql探索

    目标记录每次请求内的http.es.mysql耗时,本篇讨论mysql部分 为什么说要探索,这不是很简单的事么?但是能满足以下几点么? 能记录limit等参数 能将参数和sql写一起,能直接使用 能记 ...

  4. 记录SQL Server2008日志文件损坏的恢复过程

    记录SQL Server2008日志文件损坏的恢复过程: 环境: 系 统:Windows Server2003 数据库:SQL Server2008 故障原因: 通过mstsc链接同一服务器时,用户界 ...

  5. 如何在MySql中记录SQL日志记录

    My SQL可以用下面方法跟踪sql 语句,以下方法以Windows平台为例,linux雷同:   1  配置my.ini文件(在安装目录,linux下文件名为my.cnf     查找到[mysql ...

  6. 如何在MySql中记录SQL日志

    SQL server有一个sql profiler可以实时跟踪服务器执行的SQL语句,这在很多时候调试错误非常有用.例如:别人写的复杂代码.生产系统.无调试环境.无原代码... ...   查了一下资 ...

  7. 使用hibernate更新数据库记录的信息的相关学习记录

    截选代码(可能遗漏标点符号): package name.sql; import java.util.List; import name.session.HibernateSessionFactory ...

  8. RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2->新增记录SQL执行过程

    有时我们需要记录整个系统运行的SQL以作分析,特别是在上线前这对我们做内部测试也非常有帮助,当然记录SQL的方法有很多,也可以使用三方的组件.3.2版本我们在框架底层新增了记录框架运行的所有SQl过程 ...

  9. Atitit mysql 存储过程捕获所有异常,以及日志记录异常信息

    Atitit mysql 存储过程捕获所有异常,以及日志记录异常信息 1.1. 异常的处理模式exit  continue undo模式 1 1.2. 捕获所有异常使用        DECLARE ...

随机推荐

  1. C# 为WebBrowser设置代理,打开网页

    WebBrowser控件是基于IE浏览器的,所以它的内核功能是依赖于IE的,相信做.NET的人都知道. 今天的主题,和上一篇文章应该是差不多的,都是通过代理来实现功能的. 请看下面的代码: //1.定 ...

  2. 【Todo】Kerberos、OpenSSL、OAuth2.0等一系列验证权限安全相关学习

    Kerberos可以先从这一篇开始: http://idior.cnblogs.com/archive/2006/03/20/354027.html

  3. R语言实战读书笔记(五)高级数据管理

    5.2.1 数据函数 abs: sqrt: ceiling:求不小于x的最小整数 floor:求不大于x的最大整数 trunc:向0的方向截取x中的整数部分 round:将x舍入为指定位的小数 sig ...

  4. 解同余式ax ≡ c(mod m)

    将式子变形为 ax-c=my 可以看出原式有解当且仅当线性方程ax-my=c有解 设g = gcd(a, m) 则所有形如ax-my的数都是g的倍数 因此如果g不整除c则原方程无解. 下面假设g整除c ...

  5. HDFS 整体把握

    对于HDFS这样一个分布式文件系统,它的目的是为了实现在多台廉价X86服务器上实现大文件存储.     HDFS 是仿造GFS 设计出来的. 如图所示, 这种实现方案是一种采取有一个中心节点, 多个数 ...

  6. wx处理鼠标事件

    #include "MainFrame.h" BEGIN_EVENT_TABLE(MyFrame,wxFrame) EVT_LEFT_DOWN(MyFrame::OnMouseLe ...

  7. java 构造函数内部的多态方法 完全剖析

    我们先来看一个例子,如果你读过<java编程思想>的话 应该会有印象 package com.test.zj; public class PolyConstructors { public ...

  8. mipmap 目录和drawable 目录有什么区别

    Q :最近使用studio 发现drawle-hdpi 都没有了换成了mipmap-hdpi,这两个目录有什么区别呢,哪个比较好呢??? A: 我简单总结一下: 使用上没有任何区别,你把它当drawa ...

  9. crontab 每月最后一天

    0 8 28-31 * * [ `date -d tomorrow +%e` -eq 1 ] && do-something   我觉得能想到这种方法的,都是经验丰富的人.程序员们,想 ...

  10. Textbox像百度一下实现下拉显示 z

    List<string> Data = new List<string>(); string Randomstr = "功夫撒黑胡椒hcbvf蜂窝qwertyuiop ...