idea中的web工程
代码如下:
package com.dos.util;
import java.sql.*;
public class JDBCUtil {
private static String driverName = "com.mysql.cj.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/dormitory";
private static String user = "root";
private static String password = "123456";
static {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public static Connection getConnection(){
Connection connection = null;
try {
connection = DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
throw new RuntimeException(e);
}
return connection;
}
public static void release(Connection connection, Statement statement, ResultSet resultSet){
try {
if (connection != null){
connection.close();
}
if(statement != null){
statement.close();
}
if (resultSet != null){
resultSet.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
System.out.println(JDBCUtil.getConnection());
}
}
运行出现Process finished with exit code 1
有什么解决的办法?或者是因为什么出错?
方法一:URL中加入时区
String url = "jdbc:mysql://localhost:3306/test_db?characterEncoding=utf-8&serverTimezone=Asia/Shanghai"
方法二:修改MySQL的time_zone
需要改变数据库的配置:命令行登陆mysql,修改time_zone变量的值。
set global time_zone ='+8:00';
方法三:调整jdbc驱动的版本
mysql-jdbc 6.0以上的版本在连接数据库时,需要在url后面指定时区。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
url后面加上 ?serverTimezone=UTC
电脑的时区信息是中文,成为了乱码
这里设置下
https://blog.csdn.net/weixin_51485547/article/details/129091557