Maven项目无法加载jdbc.properties(java.sql.SQLException: The server time zone value ‘�й���ʱ��’ is unrecog)
1.JdbcUtils.java
package com.zxm.utils;import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;public class JdbcUtils {private static DruidDataSource dataSource;static {try {Properties properties = new Properties();//读取jdbc.properties属性配置文件InputStream inputStream = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");//从流中加载数据properties.load(inputStream);//创建 数据库连接池dataSource = (DruidDataSource)DruidDataSourceFactory.createDataSource(properties);} catch (Exception e) {e.printStackTrace();}}/*** 获取数据库连接池中的连接* @return 如果返回null,说明获取连接失败*/public static Connection getConnection() {Connection conn = null;try {conn = dataSource.getConnection();} catch (Exception e) {e.printStackTrace();}return conn;}/*** 关闭连接,放回数据库连接池* @param conn*/public static void close(Connection conn) {if(conn != null) {try {conn.close();} catch (Exception e) {e.printStackTrace();}}}}
2.jdbc.properties
username = root
password = root
url = jdbc:mysql://localhost:3306/book
driverClassName = com.mysql.jdbc.Driver
initialSize = 5
maxActive = 10
3.JdbcUtilsTest.java
import com.zxm.utils.JdbcUtils;
import org.junit.Test;import java.sql.Connection;/*** @author MIAO* @date 2022/2/20-20:16*/
public class JdbcUtilsTest {@Testpublic void testJbdcUtils() {for(int i = 0;i < 100 ;i++) {Connection connection = JdbcUtils.getConnection();System.out.println(connection);JdbcUtils.close(connection);}}
}
一开始以为路径写错了:反复找博客,找资料…果然,
出现错误1:
- 错误原因:把jdbc.properties文件放在了src文件夹下(或者是除了resources文件夹以外的地方)
- 解决方法:把jdbc.properties放在resources文件夹下
出现错误2:
-
错误原因(通过查找博客,找到原因:解决java.sql.SQLException: The server time zone value ‘�й���ʱ��’ is unrecognized or)
-
解决办法:
在jdbc.properties文件中的url改为:
url = jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8
最后再插一句:
以上为学习总结bug的内容,觉得有收获就点个赞吧!