Importei o Projeto desta aula, mesmo assim não consigo Salvar no banco. * Ao Rodar, apreasenta no meu Console
Mon Oct 31 20:15:39 GMT-03:00 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
false
13 gerado
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Can't call rollback when autocommit=true
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.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:917)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
at com.mysql.jdbc.ConnectionImpl.rollback(ConnectionImpl.java:4586)
at br.com.caelum.main.TestaInsercao2.main(TestaInsercao2.java:33)
public class TestaInsercao2 {
public static void main(String[] args) throws SQLException {
try (Connection connection = Database.getConnection()) {
try {
String sql = "insert into produto (nome,descricao) values (?,?)";
// Prepara para ser executado: Passo o SQL e Chave Gerada
PreparedStatement prepareStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
// Setando Valores
adiciona("Notbook' i5", "Eletrônico", prepareStatement);
adiciona("Televisão LDC", "Eletrônico", prepareStatement);
} catch (Exception e) {
connection.rollback(); // Estou voltando atras das minhas modificações.
System.out.println("RollBack Efetuado!"); //imprimindo no console.
}
}
}
private static void adiciona(String nome, String descricao, PreparedStatement prepareStatement)
throws SQLException {
if (nome.equals("Televisão LDC")) {
throw new IllegalArgumentException("Problema ocorrido");
}
prepareStatement.setString(1, nome);
prepareStatement.setString(2, descricao);
// Depois de Preparar os SQL e setar, peço para executar.
boolean resultado = prepareStatement.execute();
System.out.println(resultado);
// Peço para Gerar uma chave.
ResultSet resultSet = prepareStatement.getGeneratedKeys();
// Mostro a chave gerada no ResultSet.
while (resultSet.next()) {
String valor = resultSet.getString(1);
System.out.println(valor + " gerado");
}
resultSet.close();
}
}