提问者:小点点

尝试执行数据库连接时获取InvalidConntionAt的异常[重复]


当我试图获取数据库连接时,我得到了InvalidConntionAt的异常。

    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

有人能告诉我是什么问题吗?谢谢。


共1个答案

匿名用户

正在使用的服务器时区“Maroc”无效。

要查看设置的值,请使用SELECT@@global.time_zone

尝试在文件中通过default-time设置哪个时区my.cnf例如:default-time='00:00'

要为当前会话设置它,请执行以下操作:SET time_zone = 时区名称;