0

I am getting InvalidConnectionAttributeException when trying to obtain a database connection.

    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();

            }

        }

    }

That's the output of my program:

 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

Could someone tells me what the problem is, please? Thanks.

Patrick W
  • 1,485
  • 4
  • 19
  • 27

1 Answers1

0

The server time zone 'Maroc' that is being used is invalid.

To see what value it is set to use SELECT @@global.time_zone;

Try to set the whichever Time Zone by default-time-zone in the file my.cnf For eg: default-time-zone='+00:00'

To set it for current session do: SET time_zone = timezonename;

  • i used that connection then it's work --> jdbc:mysql://localhost:3306/db?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC – Mohammed Lakhdari May 05 '19 at 20:15