这是项目架构,就是一个简单的控制台测试连接mysql数据库
这是配置文件
jdbcUrl = jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8
user = root
password = 123456
dataSource.cachePrepStmts = true
dataSource.prepStmtCacheSize = 250
dataSource.prepStmtCacheSqlLimit = 2048
dataSource.useServerPrepStmts = true
dataSource.useLocalSessionState = true
dataSource.useLocalTransactionState=true
dataSource.rewriteBatchedStatements = true
dataSource.cacheResultSetMetadata = true
dataSource.cacheServerConfiguration = true
dataSource.elideSetAutoCommits = true
dataSource.maintainTimeStats = false
这是测试连接的代码
import java.sql.Connection;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
public class HikariPoolManager {
static Logger logger = LogManager.getLogger();
static HikariConfig config = new HikariConfig("/hurik.properties");
static HikariDataSource ds = new HikariDataSource(config);
/**
* 取得数据库连接
*
* @return
* @throws Exception
*/
public static Connection getConnection() throws Exception {
Connection connection = null;
try {
connection = ds.getConnection();
} catch (Exception e) {
logger.error("取得数据库连接时发生异常!" + e);
throw e;
}
return connection;
}
/**
* 释放数据库连接
*
* @param connection
* @throws Exception
*/
public static void freeConnection(Connection connection) throws Exception {
if (connection != null) {
try {
connection.close();
} catch (Exception e) {
logger.error("释放数据库连接时发生异常!" + e.getMessage());
}
}
}
public static void main(String[] args) throws Exception {
System.out.println(ds.getConnection());
}
}
运行时出的错误:
ERROR StatusLogger No Log4j 2 configuration file found. Using default configuration (logging only errors to the console), or user programmatically provided configurations. Set system property 'log4j2.debug' to show Log4j 2 internal initialization logging. See https://logging.apache.org/lo... for instructions on how to configure Log4j 2
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.ht... for further details.
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Property user does not exist on target class com.zaxxer.hikari.HikariConfig
at com.zaxxer.hikari.util.PropertyElf.setProperty(PropertyElf.java:131)
at com.zaxxer.hikari.util.PropertyElf.lambda$setTargetFromProperties$0(PropertyElf.java:57)
at java.util.Hashtable.forEach(Unknown Source)
at com.zaxxer.hikari.util.PropertyElf.setTargetFromProperties(PropertyElf.java:52)
at com.zaxxer.hikari.HikariConfig.loadProperties(HikariConfig.java:1067)
at com.zaxxer.hikari.HikariConfig.<init>(HikariConfig.java:148)
at HikariPoolManager.<clinit>(HikariPoolManager.java:11)
没有从网上找到有用的解决方案,希望能得到各位的帮助,或者能发一份简单完整又测试成功的demo案例也行
错误已经很明显了,引入log4j却没有配置Log4j的日志配置文件,配置上就没有问题了。