Duvida ao persistir JDBC

4 respostas
jdbcjava
D

estou tentando para persistir objeto usando JDBC e estou com erro.

meu Codigo.

public static void main(String[] args) throws SQLException {
	Connection connection = new ConnectionFactory().getConnection();
	Contato contato = new Contato();
	String sql = "INSERT INTO contato VALUES (?,?,?,?)";
	try {
		PreparedStatement stmt = connection.prepareStatement(sql);

		stmt.setString(1, contato.getNome());
		stmt.setString(2, contato.getEmail());
		stmt.setString(3, contato.getEndereco());
		stmt.setDate(4, new Date(contato.getDataDeNascimento().getTimeInMillis()));

		stmt.execute();
		stmt.close();
	} catch (SQLException e) {
		System.out.println(e);
	}
}

Modelo
public class Contato {

private long id;
	private String nome;
	private String email;
	private String endereco;
	private Calendar dataDeNascimento;
   //getters e Setters

Fabrica de conexão

public class ConnectionFactory {

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

Erro

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.contato.ConnectionFactory.getConnection(ConnectionFactory.java:13)
	at br.com.caelum.contato.TesteCadastro.main(TesteCadastro.java:16)
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:68)
	at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:1683)
	at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:656)
	at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:349)
	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.contato.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:308)
	at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:2474)
	at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:1817)
	at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:1673)
	... 7 more

4 Respostas

F

Dayson_Rodrigues, o erro que vc esta tomando é bem provável que seja por causa da linha

stmt.setDate(4, new Date(contato.getDataDeNascimento().getTimeInMillis()));

tenta passar uma data fixa e testa.

Abraços
Max

D

Resolvido Amigo!

C

Como você resolveu?

M

Esse código deve te ajudar, mas eh bom dar uma olhada no timezone que vc configurou o banco de dados e se o campo eh Date ou DateTime.

public class principal {

public static void main(String[] args) {

java.util.Date dataUtil = Calendar.getInstance().getTime();

java.sql.Date dataSql = new java.sql.Date(dataUtil.getTime());

}

}
Criado 22 de abril de 2017
Ultima resposta 26 de nov. de 2018
Respostas 4
Participantes 4