-1
Hello, I am trying to insert data into mysql by netbeans, but it always returns the "null" error. When I refresh mysql the table continues without the entered data. There is no error in the syntax, it just does not insert the data and returns the null error. The same is true for any other data manipulation command.
public class ClienteDAO {
private Connection conexao = null;
private Statement declaracao = null;
private ResultSet resultado = null;
public void addCliente(Cliente cli){
String script = "INSERT INTO cliente (Nome, Idade, Endereco) VALUES(?, ?, ?);";
try {
PreparedStatement stmt = conexao.prepareStatement(script);
stmt.setString(1, cli.getNome());
stmt.setInt(2, cli.getIdade());
stmt.setString(3, cli.getLocal());
System.out.println(script); //coloquei esse sout para verificar a sintaxe e quando executo o programa ele não é exibido
stmt.executeUpdate();
} catch (Exception e) {
System.out.println("ERROR - Inserir dados: "+e.getMessage());
}
}
public void listarClientes(){
String query = "select * from cliente;";
try {
PreparedStatement declaracao = conexao.prepareStatement(query);
resultado = declaracao.executeQuery();
while (resultado.next()) {
System.out.println("Codigo: "+resultado.getInt("Codigo")+"\nNome: "+resultado.getString("Nome")+
"\nIdade: "+resultado.getInt("Idade")+"Endereço: "+resultado.getString("Endereco"));
}
} catch (Exception e) {
System.out.println("ERROR MYSQL: "+e.getMessage());
}
}
}
Missing to close connection, or manually commit transaction
– nullptr
Still giving null, maybe it’s because I updated jdk
– Lara Silva
post the full stacktrace, but from what I see you didn’t even get to the connection with the bank.
conexao
must always be null– nullptr
Puts a Sqlexception in place of Exception to have a more specific error.
– Luis Alberto Batista