我学习建立一个在线教育工程(eclipse+tomcat)的日志系统,我用的log4j记录日志,现在在把日志输到MYSQL数据库中出了问题.
我的LOG4J的properties配置如下:
###########################################
log4j.rootCategory=INFO,DatabaseApp
log4j.appender.DatabaseApp=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.DatabaseApp.URL=jdbc:mysql://localhost:3306/test
log4j.appender.DatabaseApp.driver=com.mysql.jdbc.Driver
log4j.appender.DatabaseApp.user=root
log4j.appender.DatabaseApp.password=
log4j.appender.DatabaseApp.layout=org.apache.log4j.PatternLayout
log4j.appender.DatabaseApp.layout.ConversionPattern=INSERT INTO log4j (createDate,thread,priority,category,message) values(getdate(),'%t','%-5p','%c','%m')
#############################################################
我装的是MYSQL5.0,从MYSQL网上下了最新的 mysql-connector-java-5.0.0-beta.zip
因为不知道把它配到哪,我在 CLASSPATH 放了F:\Java\mysql-connector-java-5.0.0-beta\mysql-connector-java-5.0.0-beta-bin.jar;F:\Java\mysql-connector-java-5.0.0-beta\debug\mysql-connector-java-5.0.0-beta-bin-g.jar;
并在eclipse下工程中添加外部的JAR包F:\Java\mysql-connector-java-5.0.0-beta\mysql-connector-java-5.0.0-beta-bin.jar;F:\Java\mysql-connector-java-5.0.0-beta\debug\mysql-connector-java-5.0.0-beta-bin-g.jar;
我在MYSQL也建好了名叫log4j的表
create table log4j
( createDate varchar(20),
thread varchar(20),
priority varchar(20),
caregory varchar(20),
message varchar(40),
primary key(createDate)
)
-----------------------------------------------------------------
运行工程后出现错误
log4j:ERROR Failed to load driver
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.apache.log4j.jdbc.JDBCAppender.setDriver(JDBCAppender.java:351)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.log4j.config.PropertySetter.setProperty(PropertySetter.java:205)
at org.apache.log4j.config.PropertySetter.setProperty(PropertySetter.java:164)
at org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.java:129)
at org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.java:96)
at org.apache.log4j.PropertyConfigurator.parseAppender(PropertyConfigurator.java:654)
at org.apache.log4j.PropertyConfigurator.parseCategory(PropertyConfigurator.java:612)
at org.apache.log4j.PropertyConfigurator.configureRootCategory(PropertyConfigurator.java:509)
at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:415)
at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:441)
at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:468)
at org.apache.log4j.LogManager.<clinit>(LogManager.java:122)
at org.apache.log4j.Logger.getLogger(Logger.java:104)
at org.apache.commons.logging.impl.Log4JLogger.getLogger(Log4JLogger.java:229)
at org.apache.commons.logging.impl.Log4JLogger.<init>(Log4JLogger.java:65)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:529)
at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:235)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:370)
at org.apache.catalina.core.ContainerBase.getLogger(ContainerBase.java:380)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4089)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1013)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:718)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1013)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:442)
at org.apache.catalina.core.StandardService.start(StandardService.java:450)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:709)
at org.apache.catalina.startup.Catalina.start(Catalina.java:551)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:275)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
-------------------------------------------------------------------------------
请问怎么解决这个问题,另说明一下;
1.我用mysql-connector-java-3.1.12.zip也试过,不行.
2.我LOG4J写到普通文本可以成功.
是不是哪里配置错误?
--转自