Estou com dúvida sobre aonde está o erro, e gostaria de saber o porque está com este erro…
Aplicações:
Netbeans IDE 8.2
MySQL Workbench 8.0.20
Connector Java 5.1.23
Mensagem de erro:
Erro de conexão:
Não Conectou
java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
Erro CONSULTA: java.lang.NullPointerException
Erro: java.lang.NullPointerException
Não Conectou
Erro de conexão:
java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
Classe de conexão:
package projeto.p2;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JLabel;
public class dados
{
Connection con; //objeto criado com a classe importada acima
public boolean conecta(String local,String banco, String usuario, String senha) //metodo para conectar no banco de dados
{
boolean retorno = false;
try
{
Class.forName(“com.mysql.jdbc.Driver”);
con = DriverManager.getConnection
(“jdbc:mysql://” + local + “/” + banco, usuario, senha); retorno = true;
}
catch (ClassNotFoundException e)
{
System.err.println(“Erro de conexão:\n” + e);
}
catch (SQLException e)
{
System.err.println(“Erro de conexão:\n” + e);
}
return retorno;
}
//metodo que insere dados no banco
public boolean insere(String tabela, String nome, String cpf, String data, String sexo, String telefone, String celular,
String email, String pw, String cidade, String bairro, String endereco,
String estado, int cep, int numero, JLabel imagem, String local ) {
boolean retorno = false;
try {
PreparedStatement stmt = (PreparedStatement) this.con
.prepareStatement("INSERT INTO cadastro (nome, cpf, data_nasc, sexo, telefone, celular, email, presenca_na_web, cidade, bairro,"
+ "endereco, estado, cep, numero, imagem, local) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
stmt.setString(1, nome);
stmt.setString(2, cpf);
stmt.setString(3, data);
stmt.setString(4, sexo);
stmt.setString(5, telefone);
stmt.setString(6, celular);
stmt.setString(7, email);
stmt.setString(8, pw);
stmt.setString(9, cidade);
stmt.setString(10, bairro);
stmt.setString(11, endereco);
stmt.setString(12, estado);
stmt.setInt(13, cep);
stmt.setInt(14, numero);
stmt.setString(16, local);
File arq = new File(local);
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(arq);
} catch (FileNotFoundException ex) {
System.err.println("Erro - " + ex);
}
stmt.setBinaryStream(15,inputStream, (int) arq.length());
stmt.execute(); stmt.close();
retorno = true;
} catch (SQLException ex) {
retorno = false;
System.err.println("Erro INSERT: " + ex);
}
return retorno;
}
public boolean insereOb(String tabela, String nome, String cpf, String data, String sexo, String telefone, String celular,
String email, String pw, String cidade, String bairro, String endereco,
String estado, int cep, int numero, String Observacao, JLabel imagem, String local ) {
boolean retorno = false;
try {
PreparedStatement stmt = (PreparedStatement) this.con
.prepareStatement("INSERT INTO cadastro (nome, cpf, data_nasc, sexo, telefone, celular, email, presenca_na_web, cidade, bairro,"
+ "endereco, estado, cep, numero, observacao, imagem, local) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
stmt.setString(1, nome);
stmt.setString(2, cpf);
stmt.setString(3, data);
stmt.setString(4, sexo);
stmt.setString(5, telefone);
stmt.setString(6, celular);
stmt.setString(7, email);
stmt.setString(8, pw);
stmt.setString(9, cidade);
stmt.setString(10, bairro);
stmt.setString(11, endereco);
stmt.setString(12, estado);
stmt.setInt(13, cep);
stmt.setInt(14, numero);
stmt.setString(15, Observacao);
stmt.setString(17, local);
File arq = new File(local);
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(arq);
} catch (FileNotFoundException ex) {
System.err.println("Erro - " + ex);
}
stmt.setBinaryStream(16,inputStream, (int) arq.length());
stmt.execute(); stmt.close();
retorno = true;
} catch (SQLException ex) {
retorno = false;
System.err.println("Erro INSERT: " + ex);
}
return retorno;
}
//metodo que insereCidades dados no banco
public boolean insereCidades(String tabela, String descricao, int cep, String uf) {
boolean retorno = false;
try {
PreparedStatement stmt = (PreparedStatement) this.con
.prepareStatement("insert into " + tabela + " (descricao, cep, uf) values ('" + descricao + "'," + cep + ",'" + uf + "');");
stmt.execute();
stmt.close();
retorno = true;
} catch (SQLException ex) {
retorno = false;
System.err.println("Erro INSERT: " + ex);
}
return retorno;
}
public boolean insereBairros(String tabela, String descricao, String uf, int cidades_idcidade){
boolean retorno = false;
try {
PreparedStatement stmt = (PreparedStatement) this.con
.prepareStatement("insert into bairros (descricao, uf, cidades_idcidade) values (?,?,?)");
stmt.setString(1, descricao);
stmt.setString(2, uf);
stmt.setInt(3, cidades_idcidade);
stmt.execute(); stmt.close();
retorno = true;
} catch (SQLException ex) {
retorno = false;
System.err.println("Erro INSERT: " + ex);
}
return retorno;
}
public boolean insereLogra(String tabela, String uf, int idcidade, String descricao,int bairros_idbairros, int cep ){
boolean retorno = false;
try {
PreparedStatement stmt = (PreparedStatement) this.con
.prepareStatement("insert into logradouros (uf, idcidade, descricao, bairros_idbairros, cep) values (?,?,?,?,?)");
stmt.setString(1, uf);
stmt.setInt(2, idcidade);
stmt.setString(3, descricao);
stmt.setInt(4, bairros_idbairros);
stmt.setInt(5, cep);
stmt.execute(); stmt.close();
retorno = true;
} catch (SQLException ex) {
retorno = false;
System.err.println("Erro INSERT: " + ex);
}
return retorno;
}
public ResultSet consulta(String consulta) {
ResultSet rs = null;
try {
PreparedStatement stmt = (PreparedStatement) this.con.prepareStatement(consulta);
rs = stmt.executeQuery();
} catch (Exception e) {
System.err.println("Erro CONSULTA: " + e);
}
return rs;
}
public boolean atualizaBairros(String tabela, String descricao, String uf, int cidades_idcidade, String condicao){
boolean retorno = false;
try {
PreparedStatement stmt = (PreparedStatement) this.con
.prepareStatement("update bairros set descricao=?, uf=?, cidades_idcidade=? where descricao=?");
stmt.setString(1, descricao);
stmt.setString(2, uf);
stmt.setInt(3, cidades_idcidade);
stmt.setString(4, condicao);
stmt.execute(); stmt.close();
retorno = true;
} catch (SQLException ex) {
retorno = false;
System.err.println("Erro UPDATE: " + ex);
}
return retorno;
}
public boolean atualizaLogra(String tabela, String uf, int idcidade, String descricao,int bairros_idbairros, int cep, int condicao){
boolean retorno = false;
try {
PreparedStatement stmt = (PreparedStatement) this.con
.prepareStatement("update logradouros set uf=?, idcidade=?, descricao=?, bairros_idbairros=?, cep=? where idlogradouros=?;");
stmt.setString(1, uf);
stmt.setInt(2, idcidade);
stmt.setString(3, descricao);
stmt.setInt(4, bairros_idbairros);
stmt.setInt(5, cep);
stmt.setInt(6, condicao);
stmt.execute(); stmt.close();
retorno = true;
} catch (SQLException ex) {
retorno = false;
System.err.println("Erro UPDATE: " + ex);
}
return retorno;
}
public boolean atualiza(String tabela, String nome, String cpf, String data, String sexo, String telefone, String celular,
String email, String pw, String cidade, String bairro, String endereco,
String estado, int cep, int numero, JLabel imagem, String local, String condicao)
{
boolean retorno = false;
try {
PreparedStatement stmt = (PreparedStatement) this.con
.prepareStatement("update cadastro set nome=?, cpf=?, data_nasc=?, sexo=?, telefone=?, celular=?, email=?, presenca_na_web=?, cidade=?, bairro=?, endereco=?, estado=?, cep=?, numero=?, imagem=?, local=? where cpf=?;");
stmt.setString(1, nome);
stmt.setString(2, cpf);
stmt.setString(3, data);
stmt.setString(4, sexo);
stmt.setString(5, telefone);
stmt.setString(6, celular);
stmt.setString(7, email);
stmt.setString(8, pw);
stmt.setString(9, cidade);
stmt.setString(10, bairro);
stmt.setString(11, endereco);
stmt.setString(12, estado);
stmt.setInt(13, cep);
stmt.setInt(14, numero);
stmt.setString(16, local);
stmt.setString(17, condicao);
File arq = new File(local);
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(arq);
} catch (FileNotFoundException ex) {
System.err.println("Erro - " + ex);
}
stmt.setBinaryStream(15,inputStream, (int) arq.length());
stmt.execute(); stmt.close();
retorno = true;
} catch (SQLException ex) {
retorno = false;
System.err.println("Erro UPDATE: " + ex);
}
return retorno;
}
public boolean atualizaOb(String tabela, String nome, String cpf, String data, String sexo, String telefone, String celular,
String email, String pw, String cidade, String bairro, String endereco,
String estado, int cep, int numero, String observacao, JLabel imagem, String local, String condicao)
{
boolean retorno = false;
try {
PreparedStatement stmt = (PreparedStatement) this.con
.prepareStatement("update cadastro set nome=?, cpf=?, data_nasc=?, sexo=?, telefone=?, celular=?, email=?, presenca_na_web=?, cidade=?, bairro=?, endereco=?, estado=?, cep=?, numero=?, observacao=?, imagem=?, local=? where cpf=?;");
stmt.setString(1, nome);
stmt.setString(2, cpf);
stmt.setString(3, data);
stmt.setString(4, sexo);
stmt.setString(5, telefone);
stmt.setString(6, celular);
stmt.setString(7, email);
stmt.setString(8, pw);
stmt.setString(9, cidade);
stmt.setString(10, bairro);
stmt.setString(11, endereco);
stmt.setString(12, estado);
stmt.setInt(13, cep);
stmt.setInt(14, numero);
stmt.setString(15, observacao);
stmt.setString(17, local);
stmt.setString(18, condicao);
File arq = new File(local);
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(arq);
} catch (FileNotFoundException ex) {
System.err.println("Erro - " + ex);
}
stmt.setBinaryStream(16,inputStream, (int) arq.length());
stmt.execute(); stmt.close();
retorno = true;
} catch (SQLException ex) {
retorno = false;
System.err.println("Erro UPDATE: " + ex);
}
return retorno;
}
public boolean atualizaCidade(String tabela, String descricao, int cep, String uf, String condicao){
boolean retorno = false;
try {
PreparedStatement stmt = (PreparedStatement) this.con
.prepareStatement("update cidades set descricao=?, cep=?, uf=? where idcidade=?;");
stmt.setString(1, descricao);
stmt.setInt(2, cep);
stmt.setString(3, uf);
stmt.setString(4, condicao);
stmt.execute(); stmt.close();
retorno = true;
} catch (SQLException ex) {
retorno = false;
System.err.println("Erro UPDATE: " + ex);
}
return retorno;
}
//Excluir Cadastros
public boolean excluir(String tabela, String condicao)
{
boolean retorno = false;
try {
PreparedStatement stmt = (PreparedStatement) this.con.prepareStatement("delete from " + tabela + " where " + condicao);
stmt.execute();
stmt.close();
retorno = true;
} catch (SQLException ex) {
retorno = false;
System.err.println("Erro DELETE: " + ex);
}
return retorno;
}
}