gpt4 book ai didi

java - log4j 未初始化 JDBC 驱动程序

转载 作者:行者123 更新时间:2023-12-02 11:03:13 24 4
gpt4 key购买 nike

尝试使用 log4j 登录到 MS SQL 服务器:

import org.apache.log4j.Logger;

public class MailTest {
private static Logger lg = Logger.getLogger(MailTest.class);
public static void main(String[] args) {
MailTest test = new MailTest();
try {

lg.info("hello");

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

遇到异常:

log4j:ERROR Failed to excute sql
java.sql.SQLException: No suitable driver found for "jdbc:sqlserver://localhost:1433;databaseName=ERP_IDMS"
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at org.apache.log4j.jdbc.JDBCAppender.getConnection(JDBCAppender.java:251)
at org.apache.log4j.jdbc.JDBCAppender.execute(JDBCAppender.java:215)
at org.apache.log4j.jdbc.JDBCAppender.flushBuffer(JDBCAppender.java:289)
at org.apache.log4j.jdbc.JDBCAppender.append(JDBCAppender.java:186)
at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:251)
at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:66)
at org.apache.log4j.Category.callAppenders(Category.java:206)
at org.apache.log4j.Category.forcedLog(Category.java:391)
at org.apache.log4j.Category.info(Category.java:666)
at tst.MailTest.main(MailTest.java:35)
d{yyyy-MM-dd HH:mm:ss} INFO MailTest:35 - hello

log4j.属性:

# Define the root logger with file appender
log4j.rootLogger = debug, sql, stdout
#log4j.rootLogger = error, sql, stdout

# Log for the Hibernate
log4j.logger.org.hibernate=ERROR
#log4j.logger.org.hibernate=INFO
#log4j.logger.org.hibernate.type=trace
#log4j.logger.org.hibernate.type=ALL

# ================ Added as part of logging into DB ====================
# Define the jdbc appender
log4j.appender.sql=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.sql.URL="jdbc:sqlserver://localhost:1433;databaseName=ERP_IDMS"


# Set Database Driver
log4j.appender.sql.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver

# Set database user name and password
log4j.appender.sql.user=sa
log4j.appender.sql.password=sa

# Set the SQL statement to be executed.
#short
log4j.appender.sql.layout.ConversionPattern=INSERT INTO LOGS1 VALUES ('%d{yyyy-MM-dd HH:mm:ss.SSS}' ,'%C','%p','%m', '%throwable{short}', '%throwable{800}')
# Define the xml layout for file appender
log4j.appender.sql.layout=org.apache.log4j.EnhancedPatternLayout
#=======================================================================

# Direct to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# =============================================================================

我在互联网上看到很多关于 JDBC 未初始化的报告。但是如何在 log4j 中做到这一点呢?如何修复这个错误?

最佳答案

您的类路径中是否有 SQL Server JDBC 驱动程序?如果没有,请参阅文章获取指导:Using the JDBC Driver

关于java - log4j 未初始化 JDBC 驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51176039/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com