jdbc log4jdbc mysql_log4jdbc 使用入门

log4jdbc 可以将占位符与参数全部合并在一起显示,方便直接拷贝

sql

PLSQL Developer

等客户端直接执行,加快调试速度. 记录执行的sql及其耗时等.

1.测试代码

package com.mtea.demo.test;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

public class Demo {

private static Logger logger = LoggerFactory.getLogger(Demo.class);

public static void main(String args[]) {

test_without_log4jdbc_effect();

test_with_log4jdbc_effect();

}

/**

* no log4jdbc

*/

public static void test_without_log4jdbc_effect(){

//mysql-connector-java-5.1.10-bin.jar提供的类

String driverNameOfMysql = "com.mysql.jdbc.Driver";

String connStringOfMysql = "jdbc:mysql://localhost:3306/db_hibernate";

Connection conn = null;

ResultSet rs = null;

PreparedStatement pstmt = null;

try {

Class.forName(driverNameOfMysql);

conn = DriverManager.getConnection(connStringOfMysql, "root", "root");

pstmt = conn.prepareStatement("select name from tb_product where id=?");

pstmt.setInt(1, 1);

rs=pstmt.executeQuery();

while (rs.next()) {

System.out.println(rs.getString("name"));

}

rs.close();

} catch (Exception e) {

logger.error("test_without_log4jdbc_effect 出错", e);

} finally {

logger.info("test_without_log4jdbc_effect finished");

}

}

/**

* with log4jdbc_effect

*/

public static void test_with_log4jdbc_effect(){

//log4jdbc4-1.2beta2.jar提供的类

String driverName = "net.sf.log4jdbc.DriverSpy";

String connString = "jdbc:log4jdbc:mysql://localhost:3306/db_hibernate";

Connection conn = null;

ResultSet rs = null;

PreparedStatement pstmt = null;

try {

Class.forName(driverName);

conn = DriverManager.getConnection(connString, "root", "root");

pstmt = conn.prepareStatement("select name from tb_product where id=?");

pstmt.setInt(1, 1);

rs=pstmt.executeQuery();

while (rs.next()) {

System.out.println(rs.getString("name"));

}

rs.close();

} catch (Exception e) {

logger.error("test_with_log4jdbc_effect 出错", e);

} finally {

logger.info("test_with_log4jdbc_effect finished");

}

}

}

2.运行结果:

d31db3e5a0fb379be9413353809bbb5f.png

3.项目配置:

dbaebb58aea4d1de673180ec1a42ba29.png

4.过滤日志:

5. over

d6cea115f4898fc400123215944cda0e.png

大小: 68.3 KB

d48bc10c73f59e27c29226c8a12e2f8c.png

大小: 27.7 KB

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2011-10-21 13:30

浏览 2234

论坛回复 / 浏览 (0 / 2372)

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值