Bom Dia
Estou com um problema para inserir dados em um banco ao click do botão.
poderiam me ajudar por favor.
Já procurei em varios topicos porem sem sucesso.
Minhas classes.
Classe principal.
public class CadastroDeEmpresa extends JFrame {
JLabel rotuloCEP, rotuloTel, rotuloEndereco, rotuloEmpresa, rotuloCidade,
rotuloBairro, rotuloEmail, rotuloCNPJ, rotuloInscricao;
JFormattedTextField cep, tel, cnpj;
MaskFormatter maskCEP, maskTel, maskCNPJ;
JTextField empresa, enderceco, cidade, bairro, email, inscricao, endereco;
JButton cancela, limpar, insere;
//EmpresaDAO dao = new EmpresaDAO();
public CadastroDeEmpresa() {
super("Cadastro de Clientes");
Container telaEmpresa = getContentPane();
setLayout(null);
//Empresa cadastra = new Empresa();
// Rotulos
rotuloEmpresa = new JLabel("Empresa: ");
rotuloEndereco = new JLabel("Endereco: ");
rotuloCidade = new JLabel("Cidade: ");
rotuloBairro = new JLabel("Bairro: ");
rotuloCEP = new JLabel("CEP: ");
rotuloTel = new JLabel("Telefone: ");
rotuloEmail = new JLabel("Email: ");
rotuloCNPJ = new JLabel("CNPJ: ");
rotuloInscricao = new JLabel("Inscrição: ");
// Posicionamento dos rotulos
rotuloEmpresa.setBounds(10, 0, 100, 120);
rotuloEndereco.setBounds(10, 30, 100, 120);
rotuloCidade.setBounds(10, 60, 100, 120);
rotuloBairro.setBounds(10, 90, 100, 120);
rotuloCEP.setBounds(300, 90, 100, 120);
rotuloTel.setBounds(10, 150, 100, 120);
rotuloEmail.setBounds(10, 180, 100, 120);
rotuloCNPJ.setBounds(10, 210, 100, 120);
rotuloInscricao.setBounds(10, 240, 100, 120);
// TextField.
empresa = new JTextField(30);
endereco = new JTextField(50);
cidade = new JTextField(50);
bairro = new JTextField(30);
email = new JTextField(50);
inscricao = new JTextField(20);
// Posicionamento dos TextField
empresa.setBounds(70, 50, 220, 20);
endereco.setBounds(70, 80, 220, 20);
cidade.setBounds(70, 110, 220, 20);
bairro.setBounds(70, 140, 220, 20);
email.setBounds(70, 232, 220, 20);
inscricao.setBounds(70, 290, 180, 20);
// Botões
cancela = new JButton("Cancela");
limpar = new JButton("Limpar");
insere = new JButton("Inserir");
cancela.setBounds(250, 340, 100, 20);
limpar.setBounds(130, 340, 100, 20);
insere.setBounds(10, 340, 100, 20);
try {
maskCEP = new MaskFormatter("#####-###");
maskCNPJ = new MaskFormatter("###.###.###/####-##");
maskTel = new MaskFormatter("(##)-####-####");
maskTel.setPlaceholderCharacter('_');
maskCEP.setPlaceholderCharacter('_');
maskCNPJ.setPlaceholderCharacter('_');
} catch (ParseException exp) {
}
cep = new JFormattedTextField(maskCEP);
cnpj = new JFormattedTextField(maskCNPJ);
tel = new JFormattedTextField(maskTel);
cep.setBounds(333, 140, 100, 20);
cnpj.setBounds(70, 260, 150, 20);
tel.setBounds(70, 200, 100, 20);
telaEmpresa.add(rotuloEmpresa);
telaEmpresa.add(rotuloEndereco);
telaEmpresa.add(rotuloCidade);
telaEmpresa.add(rotuloBairro);
telaEmpresa.add(rotuloCEP);
telaEmpresa.add(rotuloCNPJ);
telaEmpresa.add(rotuloEmail);
telaEmpresa.add(rotuloInscricao);
telaEmpresa.add(rotuloTel);
telaEmpresa.add(empresa);
telaEmpresa.add(endereco);
telaEmpresa.add(cidade);
telaEmpresa.add(bairro);
telaEmpresa.add(email);
telaEmpresa.add(inscricao);
telaEmpresa.add(tel);
telaEmpresa.add(cep);
telaEmpresa.add(cnpj);
telaEmpresa.add(cancela);
telaEmpresa.add(limpar);
telaEmpresa.add(insere);
setSize(450, 400);
setVisible(true);
insere.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
//nao sei como fazer para este botao funcionar o que devo adicionar aqui?
}
});
limpar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
empresa.setText(null);
endereco.setText(null);
cidade.setText(null);
bairro.setText(null);
email.setText(null);
inscricao.setText(null);
tel.setText(null);
cep.setText(null);
cnpj.setText(null);
}
});
cancela.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int opcao;
Object[] sair = {"Sim", "Não"};
opcao = JOptionPane.showOptionDialog(null,
"Deseja mesmo Cancelar a operação?", "Fechar",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, sair, sair[0]);
if (opcao == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
});
}
public static void main(String args[]) {
CadastroDeEmpresa app = new CadastroDeEmpresa();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Classe de Conexao (Já testada).
public class ConnectionFactory {
public static Connection getConnection() throws SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Conectando ao banco");
return DriverManager.getConnection("jdbc:mysql://localhost/clientes",
"root", "root");
} catch (ClassNotFoundException e) {
throw new SQLException(e.getMessage());
}
}
}
Classe DAO
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JTextField;
public class EmpresaDAO {
private Connection connection;
public EmpresaDAO() throws SQLException {
this.connection = ConnectionFactory.getConnection();
}
public void adiciona(Empresa empresa) throws SQLException {
// prepared statement para inserção
String sql = "insert into (nome,email,endereco) values (?,?,?)";
PreparedStatement stmt = (PreparedStatement) connection.prepareStatement(sql);
// seta os valores
stmt.setString(1, empresa.getEmpresa());
stmt.setString(2, empresa.getEndereco());
stmt.setString(3, empresa.getCidade());
stmt.setString(4, empresa.getBairro());
stmt.setString(5, empresa.getEmail());
stmt.setString(6, empresa.getInscricao());
stmt.setString(7, empresa.getCep());
stmt.setString(8, empresa.getTel());
stmt.setString(9, empresa.getCnpj());
// executa
stmt.execute();
stmt.close();
}
void adiciona(JTextField empresa) {
throw new UnsupportedOperationException("Not yet implemented");
}
}
public class Empresa {
String empresa, enderceco, cidade, bairro, email, inscricao, endereco, cep,
tel, cnpj;
public String getEmpresa() {
return empresa;
}
public void setEmpresa(String empresa) {
this.empresa = empresa;
}
public String getEnderceco() {
return enderceco;
}
public void setEnderceco(String enderceco) {
this.enderceco = enderceco;
}
public String getCidade() {
return cidade;
}
public void setCidade(String cidade) {
this.cidade = cidade;
}
public String getBairro() {
return bairro;
}
public void setBairro(String bairro) {
this.bairro = bairro;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getInscricao() {
return inscricao;
}
public void setInscricao(String inscricao) {
this.inscricao = inscricao;
}
public String getEndereco() {
return endereco;
}
public void setEndereco(String endereco) {
this.endereco = endereco;
}
public String getCep() {
return cep;
}
public void setCep(String cep) {
this.cep = cep;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getCnpj() {
return cnpj;
}
public void setCnpj(String cnpj) {
this.cnpj = cnpj;
}
}

