【问题标题】:Getting InvalidConnectionAttributeException when trying to do a Database Connection [duplicate]尝试进行数据库连接时出现 InvalidConnectionAttributeException [重复]
【发布时间】:2019-09-23 11:25:29
【问题描述】:

我在尝试获取数据库连接时收到 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

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

【问题讨论】:

    标签: java mysql jdbc


    【解决方案1】:

    正在使用的服务器时区“摩洛哥”无效。

    查看设置为使用什么值SELECT @@global.time_zone;

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

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

    【讨论】:

    • 我使用了那个连接然后它工作了 --> jdbc:mysql://localhost:3306/db?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
    猜你喜欢
    • 2019-02-13
    • 2015-05-28
    • 1970-01-01
    • 2014-06-09
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    相关资源
    最近更新 更多