尝试获取数据库连接时遇到 InvalidConnectionAttributeException。
import java.sql.*;
public class JdbcTest {
public static void main (String[] args) throws SQLException {
Connection con= null;
Statement st= null;
ResultSet rs= null;
try {
// get connection to database
con= DriverManager.getConnection("jdbc:mysql://localhost:3306/demo", "st", "st");
System.out.println("Connection has created successfuly !");
// create a statement
st= con.createStatement();
// Execute sql query
rs= st.executeQuery("SELECT * FROM demo.employees;");
// process the result set
while (rs.next()) {
System.out.println(rs.getString("last_name")+", "+rs.getString("first_name"));
}
con.close();
}
catch (Exception exc){
exc.printStackTrace();
}
}
}
这是我的程序的输出:
at java.sql.DriverManager.getConnection(Unknown Source)
at JdbcTest.main(JdbcTest.java:15)
Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value 'Maroc' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
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 com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85)
at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:132)
at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2243)
at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2267)
at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1319)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:966)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
... 6 more
有人可以告诉我问题是什么吗?谢谢。
最佳答案
正在使用的服务器时区“Maroc”无效。
要查看设置为什么值,请使用SELECT @@global.time_zone;
尝试在 my.cnf 文件中通过 default-time-zone
设置时区
例如:
default-time-zone='+00:00'
要将其设置为当前 session ,请执行以下操作:
SET time_zone = timezonename;
关于java - 尝试进行数据库连接时出现 InvalidConnectionAttributeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55994808/