package log4jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement; import org.junit.Test; import com.mchange.v2.c3p0.ComboPooledDataSource; public class DataSourceTest { @Test
public void testC3P0() {
try {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser("root");
dataSource.setPassword("密码");
dataSource.setDriverClass("net.sf.log4jdbc.DriverSpy");
dataSource.setJdbcUrl("jdbc:log4jdbc:mysql://localhost:3306/实例名");
dataSource.setMaxPoolSize(50);
Connection conn = dataSource.getConnection();
String sql = "select * " + "from tb_person where id=?";
PreparedStatement ps = null;
ps = conn.prepareStatement(sql);
ps.setInt(1, 1);
ps.executeQuery();
ps.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <!-- An example log4j configuration xml file for log4jdbc -->
<!-- Logging levels are: -->
<!-- DEBUG < INFO < WARN < ERROR < FATAL --> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="stdout-appender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p %c{1}: %m%n"/>
</layout>
</appender> <appender name="sql-appender" class="org.apache.log4j.FileAppender">
<param name="File" value="./logs/sql.log"/>
<param name="Append" value="false"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="-----&gt; %d{yyyy-MM-dd HH:mm:ss.SSS} &lt;%t&gt; %m%n%n"/>
</layout>
</appender> <appender name="sql-timing-appender" class="org.apache.log4j.FileAppender">
<param name="File" value="./logs/sqltiming.log"/>
<param name="Append" value="false"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="-----&gt; %d{yyyy-MM-dd HH:mm:ss.SSS} %m%n%n"/>
</layout>
</appender> <appender name="jdbc-appender" class="org.apache.log4j.FileAppender">
<param name="File" value="./logs/jdbc.log"/>
<param name="Append" value="false"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %m%n"/>
</layout>
</appender> <appender name="jdbc-connection" class="org.apache.log4j.FileAppender">
<param name="File" value="./logs/connection.log"/>
<param name="Append" value="false"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %m%n"/>
</layout>
</appender> <!--
The Following 5 logs can be turned on and off while the server is running
LIVE in order to trace the SQL and/or all JDBC coming out of the application. To turn a log on, set the level value to INFO or DEBUG (to see class name and
line number information in the log) The DEBUG setting is much more inefficient
but the output is much more useful. To turn off JDBC logging completely, you must set all 5 logs to a level higher
than ERROR (FATAL is suggested.)
--> <!-- log SQL (pre-execution) plus exceptions caused by SQL -->
<logger name="jdbc.sqlonly" additivity="false">
<level value="debug"/>
<appender-ref ref="sql-appender"/>
</logger> <!-- log SQL with timing information, post execution -->
<logger name="jdbc.sqltiming" additivity="false">
<level value="fatal"/>
<appender-ref ref="sql-timing-appender"/>
</logger> <!-- only use the two logs below to trace ALL JDBC information,
NOTE: This can be very voluminous! --> <!-- log all jdbc calls except ResultSet calls -->
<logger name="jdbc.audit" additivity="false">
<level value="fatal"/>
<appender-ref ref="jdbc-appender"/>
</logger> <!-- log the jdbc ResultSet calls -->
<logger name="jdbc.resultset" additivity="false">
<level value="fatal"/>
<appender-ref ref="jdbc-appender"/>
</logger> <!-- log connection open/close events and dump of all open connection numbers -->
<logger name="jdbc.connection" additivity="false">
<level value="fatal"/>
<appender-ref ref="connection-appender"/>
</logger> <!-- this log is for internal debugging of log4jdbc, itself -->
<!-- debug logging for log4jdbc itself -->
<logger name="log4jdbc.debug" additivity="false">
<level value="debug"/>
<appender-ref ref="stdout-appender"/>
</logger> <!-- by default, log everything to the console with a level of WARN or higher -->
<root>
<level value="warn"/>
<appender-ref ref="stdout-appender"/>
</root>
</log4j:configuration>

Console输出:

log4j:ERROR No appender named [connection-appender] could be found.
2015-09-22 22:40:38.933 DEBUG debug: ... log4jdbc initializing ...
2015-09-22 22:40:38.934 DEBUG debug: log4jdbc.properties not found on classpath
2015-09-22 22:40:38.934 DEBUG debug: x log4jdbc.debug.stack.prefix is not defined
2015-09-22 22:40:38.934 DEBUG debug: x log4jdbc.sqltiming.warn.threshold is not defined
2015-09-22 22:40:38.934 DEBUG debug: x log4jdbc.sqltiming.error.threshold is not defined
2015-09-22 22:40:38.934 DEBUG debug: x log4jdbc.dump.booleanastruefalse is not defined (using default value false)
2015-09-22 22:40:38.934 DEBUG debug: x log4jdbc.dump.sql.maxlinelength is not defined (using default of 90)
2015-09-22 22:40:38.934 DEBUG debug: x log4jdbc.dump.fulldebugstacktrace is not defined (using default value false)
2015-09-22 22:40:38.934 DEBUG debug: x log4jdbc.statement.warn is not defined (using default value false)
2015-09-22 22:40:38.934 DEBUG debug: x log4jdbc.dump.sql.select is not defined (using default value true)
2015-09-22 22:40:38.934 DEBUG debug: x log4jdbc.dump.sql.insert is not defined (using default value true)
2015-09-22 22:40:38.935 DEBUG debug: x log4jdbc.dump.sql.update is not defined (using default value true)
2015-09-22 22:40:38.935 DEBUG debug: x log4jdbc.dump.sql.delete is not defined (using default value true)
2015-09-22 22:40:38.935 DEBUG debug: x log4jdbc.dump.sql.create is not defined (using default value true)
2015-09-22 22:40:38.935 DEBUG debug: x log4jdbc.dump.sql.addsemicolon is not defined (using default value false)
2015-09-22 22:40:38.935 DEBUG debug: x log4jdbc.auto.load.popular.drivers is not defined (using default value true)
2015-09-22 22:40:38.935 DEBUG debug: x log4jdbc.trim.sql is not defined (using default value true)
2015-09-22 22:40:38.935 DEBUG debug: x log4jdbc.trim.sql.extrablanklines is not defined (using default value true)
2015-09-22 22:40:38.935 DEBUG debug: x log4jdbc.suppress.generated.keys.exception is not defined (using default value false)
2015-09-22 22:40:38.935 DEBUG debug: x log4jdbc.drivers is not defined
2015-09-22 22:40:38.942 DEBUG debug: FOUND DRIVER com.mysql.jdbc.Driver
2015-09-22 22:40:38.945 DEBUG debug: ... log4jdbc initialized! ...

工程目录下logs/sql.log

-----> 2015-09-22 22:40:39.241 <main>  com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:379)
2. select * from tb_person where id=1

http://blog.csdn.net/alex1314159/article/details/47185365
http://sourceforge.net/projects/c3p0/?source=typ_redirect
https://github.com/arthurblake/log4jdbc

Log4jdbc demo的更多相关文章

  1. 通过一个demo了解Redux

    TodoList小demo 效果展示 项目地址 (单向)数据流 数据流是我们的行为与响应的抽象:使用数据流能帮我们明确了行为对应的响应,这和react的状态可预测的思想是不谋而合的. 常见的数据流框架 ...

  2. 很多人很想知道怎么扫一扫二维码就能打开网站,就能添加联系人,就能链接wifi,今天说下这些格式,明天做个demo

    有些功能部分手机不能使用,网站,通讯录,wifi基本上每个手机都可以使用. 在看之前你可以扫一扫下面几个二维码先看看效果: 1.二维码生成 网址 (URL) 包含网址的 二维码生成 是大家平时最常接触 ...

  3. 在线浏览PDF之PDF.JS (附demo)

    平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html#skill 下载地址:http://mozilla.gith ...

  4. 【微框架】Maven +SpringBoot 集成 阿里大鱼 短信接口详解与Demo

    Maven+springboot+阿里大于短信验证服务 纠结点:Maven库没有sdk,需要解决 Maven打包找不到相关类,需要解决 ps:最近好久没有写点东西了,项目太紧,今天来一篇 一.本文简介 ...

  5. vue双向数据绑定原理探究(附demo)

    昨天被导师叫去研究了一下vue的双向数据绑定原理...本来以为原理的东西都非常高深,没想到vue的双向绑定真的很好理解啊...自己动手写了一个. 传送门 双向绑定的思想 双向数据绑定的思想就是数据层与 ...

  6. Android Studio-—使用OpenCV的配置方法和demo以及开发过程中遇到的问题解决

    前提: 1.安装Android Studio(过程略) 2.官网下载OpenCV for Android 网址:http:opencv.org/downloads.html 我下载的是下图的版本 3. ...

  7. iOS之ProtocolBuffer搭建和示例demo

    这次搭建iOS的ProtocolBuffer编译器和把*.proto源文件编译成*.pbobjc.h 和 *.pbobjc.m文件时,碰到不少问题! 搭建pb编译器到时没有什么问题,只是在把*.pro ...

  8. 钉钉开放平台demo调试异常问题解决:hostname in certificate didn't match

    今天研究钉钉的开放平台,结果一个demo整了半天,这帮助系统写的也很难懂.遇到两个问题: 1.首先是执行demo时报unable to find valid certification path to ...

  9. 无限分级和tree结构数据增删改【提供Demo下载】

    无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...

随机推荐

  1. 源码安装apache及配置转发

    一.    安装Apache a)    解压:tar -xvf httpd-*; b)    ./configure --prefix=/usr/oracle/apache CC="gcc ...

  2. Maven导入eclipse缺少web-resources目录

    导入其它机器的maven项目会出现缺少target/m2e/下的web-resources目录,眼下详细原因不知道, 解决方法是,直接将缺少的web-resources目录复制到项目中,错误解决

  3. Android 4.0新组件:GridLayout详细说明

    于Android 4.0(API 14)它提供了一个新的组件GridLayout,它继承自Linearlayout,用于执行网络格样式布局. 在某些方面,GridLayout与TableLayout和 ...

  4. Redis c/c++, java client连接

    Redis 介绍 redis这个想必大家都了解,关于redis的安装參考这里,redis使用文档參见这里,英文文档. Redis Cclient的用法 Redis的cclient Hiredis使用比 ...

  5. POJ 1236 Network of Schools(强连通分量)

    POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...

  6. xshell联系CentOS6.5 iptables要么ls 乱码输出

    今天Xshell 联系CentOS6.5.当终端>编码设置为:Unicode(UTF-8)时刻,跑service iptables restart输出是乱码命令:当编码被设置为:当默认语言.ls ...

  7. python网络爬虫学习笔记

    python网络爬虫学习笔记 By 钟桓 9月 4 2014 更新日期:9月 4 2014 文章文件夹 1. 介绍: 2. 从简单语句中開始: 3. 传送数据给server 4. HTTP头-描写叙述 ...

  8. 认识Backbone (二)

    Backbone.Model(模型) Models(模型)是任何Javascript应用的核心,包括数据交互及与其相关的大量逻辑: 转换.验证.计算属性和访问控制.Model在Backbone中为数据 ...

  9. NET开发者部署React-Native

    NET开发者部署React-Native 前情摘要 众所周知,有人说.net可以用Xamrian,呵呵,不习惯收费的好么?搞.Net的人设置一次java的环境变量,可能都觉得实在太麻烦了,可能是因为这 ...

  10. Python计算&绘图——曲线拟合问题(转)

    题目来自老师的课后作业,如下所示.很多地方应该可以直接调用函数,但是初学Python,对里面的函数还不是很了解,顺便带着学习的态度,尽量自己动手code. 测试版代码,里面带有很多注释和测试代码: # ...