Posted on 2006-07-04 14:04
天空苍茫 阅读(667)
评论(0) 编辑 收藏 所属分类:
jsp学习
log4j.properties 配置文件代码:
# Attach appender A1 to root. Set root level to Level.DEBUG.
log4j.rootLogger=INFO, A1,A4
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.Threshold=ERROR
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%p [%t] %c{2} (%M:%L) - %m%n
log4j.appender.A4=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A4.file=D:\\Java\\logs\\yeming.log
log4j.appender.A4.Encoding=GBK
log4j.appender.A4.DatePattern='.'yyyy-MM-dd
log4j.appender.A4.layout=org.apache.log4j.PatternLayout
log4j.appender.A4.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n
使用代码:
package com.javawoker.yeming.jiemie.database;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
/**
*@author 叶明 ---guming123416@gmail.com
*@version $Id: v 1.01 2006/06/38 16:09:14 teodord Exp $
*/
public class Pubconn {
/*
* 创建私有变量conn为数据库连接对象中Connection
* 创建私有变量dsye为数据库连接池的DataSource
*/
private Connection conn;
private DataSource dsye;
private static Logger log=Logger.getLogger(Pubconn.class);
/*
* 创建构造函数Pubconn,在建立class中加载数据源的InitialContext
*/
public Pubconn()
{
try{
Context initCtx=new InitialContext();
if(initCtx==null)
{
throw new Exception("不能加载文件Context");
}
dsye=(javax.sql.DataSource)initCtx.lookup("jdbc/yeming");
}catch(Exception ex)
{
ex.printStackTrace();
log.error("在加栽数据库连接池时间发生错误"+ex.getMessage());
}
}
/*
* 从连接池中取出一条连接变量
*
*/
public Connection getConn()
{
try{
conn=dsye.getConnection();
}catch(SQLException ex)
{
ex.printStackTrace();
log.error("获得连接对象CONN时间发生错误"+ex.getMessage());
}
return conn;
}
/*
* 关闭数据库连接,释放资源
*/
public void closeConn(Connection conn)
{
try{
if(conn!=null)
{
conn.close();
conn=null;
}
}catch(SQLException ex)
{
ex.printStackTrace();
log.error("关闭CONN时间发生错误"+ex.getMessage());
}
}
}