A situação é a seguinte:
quando eu clico no meu botão salvar ele salva um registro no banco de dados em branco.
Abaixo está o meu código do banco:
import javax.swing.JOptionPane;
public class ConexaoBD extends ClicadoCadUsu {
java.sql.Connection con;
public void conectar(){
//fazendo a conexão com o banco
try{
Class.forName("org.gjt.mm.mysql.Driver");
con = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/bd_loja","root","root");
}
catch(Exception e){
e.printStackTrace();
throw new java.lang.RuntimeException("erro ao conectar");
}
}
public void gravar(){
ClicadoCadUsu cadusu = new ClicadoCadUsu();
//inserindo dados
String nome =cadusu.textonome.getText();
String end = cadusu.textoend.getText();
String cpf = cadusu.textocpf.getText();
String login = cadusu.textonlog.getText();
String senha = cadusu.textosen.getText();
String comando = "insert into tab_usuarios(Nome_Usuario,End_Usuario,Cpf_Usuario,Login_Usuario,Senha_Usuario)"+
"values" + "('"+nome+"','"+end+"','"+cpf+"','"+login+"','"+senha+"')";
try{
java.sql.Statement stmt = con.createStatement();
stmt.executeUpdate(comando);
JOptionPane.showMessageDialog(null,"Dados Gravados com Sucesso");
stmt.close();
con.close();
}
catch(java.sql.SQLException e){
throw new java.lang.RuntimeException(e.getMessage());
}
//fechando a conexão com o banco
try{
con.close();
}
catch(Exception e){
e.printStackTrace();
throw new java.lang.RuntimeException("erro fechar");
}
}
}
E este é o código do meu formulário onde uso os métodos da classe do banco:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class ClicadoCadUsu extends JFrame implements ActionListener {
//Criação de objetos
JPrincipal janela = new JPrincipal();
JLabel lblnome = new JLabel("Nome do Usuario:::>");
JLabel lblend = new JLabel("<:::Endereço do Usuario");
JLabel lblcpf = new JLabel("CPF:::>");
JLabel lbldpessoais = new JLabel("|DADOS PESSOAIS|:");
JLabel lbldlogin = new JLabel("|DADOS DE LOGIN|");
JLabel lblnlogin = new JLabel("Login:::>");
JLabel lblsen = new JLabel("<:::Senha");
JLabel lbltip = new JLabel("|TIPO DE USUÁRIO|");
JButton botaosal = new JButton("Salvar");
JButton botaosai = new JButton("Sair");
JTextField textonome = new JTextField();
JTextField textonlog = new JTextField();
JTextField textocpf = new JTextField();
JTextField textoend = new JTextField();
JPasswordField textosen = new JPasswordField();
JPanel painel = new JPanel();
JPanel painel2 = new JPanel();
JRadioButton usu_master = new JRadioButton("Master",false);
JRadioButton usu_normal = new JRadioButton("Comum",false);
ClicadoCadUsu cadusu = new ClicadoCadUsu();
public void actionPerformed(ActionEvent evento){
JLabel titulotela = new JLabel("|||||||||||||||||" +
"||||||||||||||||||||||||||||||||||" +
"||||||||" +
"||||||||||||||||||||||||||||||||||||||||" +
"|||||||||||||||||||||||||||||||||" +
"|||||||||" +
">CADASTRO DE USUARIOS DO SISTEMA<|||||||||||||||||||" +
"||||||||||||||||||||||||||||||||||||||||||||||||||" +
"|||||||||||||||||||||||||||||||||||||||||||||||||||||||" +
"||||||||||||||||||||||||||||");
titulotela.setForeground(Color.yellow);
painel.setSize(1268,62);
painel2.setSize(1000,685);
painel.setBackground(Color.blue.darker());
painel2.setBackground(Color.black);
titulotela.setBounds(0, -200,1500 , 500);
lbldpessoais.setForeground(Color.blue.darker());
lbldpessoais.setBounds(250, -120, 800, 500);
lbltip.setForeground(Color.blue.darker());
lbltip.setBounds(600, 134, 800, 500);
lbldlogin.setForeground(Color.blue.darker());
lbldlogin.setBounds(250,365,200,30);
textonome.setBounds(250, 150,500, 20);
lblnome.setBounds(136,143,200,30);
textoend.setBounds(250,200,500,20);
lblend.setBounds(752,193,200,30);
textocpf.setBounds(250,250,200,20);
lblcpf.setBounds(210,243,200,30);
textonlog.setBounds(250, 400,200,20);
lblnlogin.setBounds(200,400,200,20);
textosen.setBounds(250,450,200,20);
lblsen.setBounds(454,450,200,20);
botaosal.setBounds(250,600,100,30);
botaosal.addActionListener(Salvar);
botaosai.setBounds(400,600,100,30);
botaosai.addActionListener(Sair);
usu_master.setBounds(600,400,200,20);
usu_normal.setBounds(600,450,200,20);
janela.add(titulotela);
janela.add(lbldpessoais);
janela.add(textonome);
janela.add(lblnome);
janela.add(textoend);
janela.add(lblend);
janela.add(textocpf);
janela.add(lblcpf);
janela.add(textonlog);
janela.add(lblnlogin);
janela.add(lbldlogin);
janela.add(textosen);
janela.add(lblsen);
janela.add(botaosal);
janela.add( botaosai);
janela.add(usu_master);
janela.add(usu_normal);
janela.add(lbltip);
janela.add(painel);
janela.add(painel2);
janela.getContentPane().setLayout(null);//Permite se organizar objetos no frame
janela.setExtendedState(MAXIMIZED_BOTH);
janela.setVisible(true);
}
ActionListener Sair = new ActionListener(){
public void actionPerformed(ActionEvent actionEvent){
Sai();
}
};
ActionListener Salvar = new ActionListener(){
public void actionPerformed(ActionEvent actionEvent){
Salva();
}
};
public void Sai(){
janela.dispose();
}
public void Salva(){
ConexaoBD grava = new ConexaoBD();
grava.conectar();
grava.gravar();
}
}