I'm running this code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.caelum.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestaInsercao {
public static void main(String[] args) throws SQLException {
Connection connection = DataBase.getConnection();
String nome = "Notebook";
String descricao = "Notebook core i5";
String sql = "insert into produto (nome, descricao) values (?,?)";
PreparedStatement comando = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
comando.setString(1, nome);
comando.setString(2, descricao);
boolean resultado = comando.execute();
System.out.println(resultado);
ResultSet resultSet = comando.getGeneratedKeys();
while (resultSet.next()) {
String id = resultSet.getString("id");
System.out.println("Chave gerado: " + id);
}
resultSet.close();
comando.close();
connection.close();
}
}
And I'm getting the following SQLException at line comando.getString("id")
:
run: false Exception in thread "main" java.sql.SQLException: Column 'id' not found. at com.mysql.jdbc.SQLError.createSQLException (SQLError.java:1074) at com.mysql.jdbc.SQLError.createSQLException (SQLError.java:988) at com.mysql.jdbc.SQLError.createSQLException (SQLError.java:974) at com.mysql.jdbc.SQLError.createSQLException (SQLError.java:919) at com.mysql.jdbc.ResultSetImpl.findColumn (ResultSetImpl.java:1167) at com.mysql.jdbc.ResultSetImpl.getString (ResultSetImpl.java:5733) at br.caelum.jdbc.TestaInsercao.main (TestaInsercao.java:33) C: \ Users \ bahia \ AppData \ Local \ NetBeans \ Cache \ 8.2 \ executor-snippets \ run.xml: 53: Java returned: 1 CONSTRUCTION FAIL (total time: 0 seconds)
I know that elá is saying that it is not finding the id column, but it exists, and when I go to the Mysql Workbench and make a select in my table, it shows that the product was added, even showing false and giving exception in java