日期:2014-05-20  浏览次数:20737 次

log4j连接数据库不能插入数据
log4j.properties:
Java code
log4j.rootLogger=database
log4j.appender.database=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.database.URL=jdbc:mysql://localhost:3306/logtest
log4j.appender.database.driver=com.mysql.jdbc.Driver
log4j.appender.database.user=root
log4j.appender.database.password=zhangyilin
log4j.appender.database.sql=insert into t_log(level,location,message,proTime) values('%p','%c{1}','%m','%d')
log4j.appender.database.layout=org.apache.log4j.PatternLayout


java程序:
Java code
import org.apache.log4j.Logger;

public class Log4jDbTest {

    public static void main(String args[]) {
        System.out.println("----------------------------");
        Logger log = Logger.getLogger(Log4jDbTest.class);
        log.info("hello");
        System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
    }
    
}


------解决方案--------------------
DB
=============
CREATE TABLE `t_log` (
`level` VARCHAR(100) COLLATE utf8_general_ci DEFAULT NULL,
`location` VARCHAR(500) COLLATE utf8_general_ci DEFAULT NULL,
`message` VARCHAR(500) COLLATE utf8_general_ci DEFAULT NULL,
`proTime` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00'
)ENGINE=InnoDB
CHARACTER SET 'utf8' COLLATE 'utf8_general_ci'
COMMENT='InnoDB free: 8192 kB';
=====================
log4j.properties:
===================
log4j.rootLogger=INFO,database
log4j.appender.database=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.database.URL=jdbc:mysql://localhost:3306/test
log4j.appender.database.driver=com.mysql.jdbc.Driver
log4j.appender.database.user=root
log4j.appender.database.password=123456
log4j.appender.database.sql=insert into t_log(level,location,message,proTime) values('%p','%c{1}','%m','%d{yyyy-MM-dd HH:mm:ss}')
log4j.appender.database.layout=org.apache.log4j.PatternLayout

====================================

public static void main(String args[]) {
System.out.println("----------------------------");
Logger log = Logger.getLogger(Log4jDbTest.class);
log.info("hello");
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
}