Apostila Java WEB Página 12- Exercício C

Não consigo resolver de jeito nenhum.

Exception in thread “main” java.lang.RuntimeException: java.sql.SQLException: The server time zone value ‘Hora oficial do Brasil’ 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 br.com.caelum.jdbc.ConnectionFactory.getConnection(ConnectionFactory.java:13)
at br.com.caelum.jdbc.teste.TestaConexao.main(TestaConexao.java:11)
Caused by: java.sql.SQLException: The server time zone value ‘Hora oficial do Brasil’ 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 com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:695)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:663)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:653)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:638)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:606)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:624)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:620)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:69)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:1663)
at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:662)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:352)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:221)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at br.com.caelum.jdbc.ConnectionFactory.getConnection(ConnectionFactory.java:11)
… 1 more
Caused by: com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: The server time zone value ‘Hora oficial do Brasil’ 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.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:54)
at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:73)
at com.mysql.cj.jdbc.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:118)
at com.mysql.cj.mysqla.MysqlaSession.configureTimezone(MysqlaSession.java:315)
at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:2446)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:1797)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:1653)
… 7 more

Falta informação: aonde executa isso, cade seu código!?

package br.com.caelum.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionFactory {
	public Connection getConnection() {
		try {
			
			return DriverManager.getConnection("jdbc:mysql://localhost/fj21", "root", "");
		} catch(SQLException e) {
			throw new RuntimeException(e);
		}
	}
}

package br.com.caelum.jdbc.teste;

import java.sql.Connection;
import java.sql.SQLException;

import br.com.caelum.jdbc.ConnectionFactory;

public class TestaConexao {
	public static void main(String[] args) throws SQLException, ClassNotFoundException {
		
		Connection connection = new ConnectionFactory().getConnection();
		System.out.println("Conexão aberta!");
		connection.close();
	}
}

Eu mudei a versão do Driver JDBC e mudou o erro, mudei da versão 6 para a 5. Interessante que dentro do driver jdbc vejo varios endereços com.mysql… e acredito que deveria ter java.sql porém baixei do site oficial.

Exception in thread “main” java.lang.RuntimeException: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/fj21
at br.com.caelum.jdbc.ConnectionFactory.getConnection(ConnectionFactory.java:13)
at br.com.caelum.jdbc.teste.TestaConexao.main(TestaConexao.java:11)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/fj21
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at br.com.caelum.jdbc.ConnectionFactory.getConnection(ConnectionFactory.java:11)
… 1 more

1 curtida

Consegui, inseri o Class.forName("com.mysql.jdbc.Driver"); no início do try e mudei a versão do Driver do JDBC para a versão 5.

1 curtida

e FUNCIONOU?

Funcionou sim, muito obrigado!

1 curtida