Inserir dados no BD mysql-eclipse

10/02/2013

1

olá pessoal, estava acompanha uma video aula, mas no final acabou não dando certo.

MainFrame.java

package br.edu.devmedia.jdbc.gui;  
import java.awt.Component;  
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import java.text.DateFormat;  
import java.text.ParseException;  
  
import javax.swing.BorderFactory;  
import javax.swing.ButtonGroup;  
import javax.swing.GroupLayout;  
import javax.swing.JButton;  
import javax.swing.JComponent;  
import javax.swing.JLabel;  
import javax.swing.JPanel;  
import javax.swing.JRadioButton;  
import javax.swing.JTabbedPane;  
import javax.swing.JTextField;  
import javax.swing.LayoutStyle;  
import javax.swing.SwingConstants;  
  
import javax.swing.WindowConstants;  
import javax.swing.border.BevelBorder;  
import javax.swing.border.TitledBorder;  
import javax.swing.SwingUtilities;  
  
import br.edu.devmedia.jdbc.bo.PessoaBO;  
import br.edu.devmedia.jdbc.dto.PessoaDTO;  
import br.edu.devmedia.jdbc.util.MensagensUtil;  
  
  
/** 
* This code was edited or generated using CloudGarden's Jigloo 
* SWT/Swing GUI Builder, which is free for non-commercial 
* use. If Jigloo is being used commercially (ie, by a corporation, 
* company or business for any purpose whatever) then you 
* should purchase a license for each developer using Jigloo. 
* Please visit[url="http://www.cloudgarden.com"]www.cloudgarden.com[/url] for details. 
* Use of Jigloo implies acceptance of these licensing terms. 
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR 
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED 
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE. 
*/  
public class MainFrame extends javax.swing.JFrame {  
  
   /** 
    *  
    */  
     
   private static final long serialVersionUID = 7973973234717597809L;  
   private JTabbedPane mainTabbedPane;  
   private JLabel lblSexo;  
   private JRadioButton rbtFeminino;  
   private JRadioButton rbtMasculino;  
   private JLabel lblEndereco;  
   private JTextField txtEndereco;  
   private JLabel lblCpf;  
   private JTextField txtCpf;  
   private JTextField txtNome;  
   private JLabel lblNome;  
   private JPanel panelListagem;  
   private JPanel panelCadastro;  
   private ButtonGroup grpSexo;  
   private JButton btnLimpar;  
   private JButton btnCadastrar;  
   private JLabel lblNasc;  
   private JTextField txtNasc;  
     
   //Format de dates  
   private DateFormat dateFormat = SimpleDateFormat("dd/mm/yyyy");  
  
   /** 
   * Auto-generated main method to display this JFrame 
   */  
   public static void main(String[] args) {  
      SwingUtilities.invokeLater(new Runnable() {  
         public void run() {  
            MainFrame inst = new MainFrame();  
            inst.setLocationRelativeTo(null);  
            inst.setVisible(true);  
         }  
      });  
   }  
     
   private DateFormat SimpleDateFormat(String string) {  
      // TODO Auto-generated method stub  
      return null;  
   }  
  
   public MainFrame() {  
      super();  
      initGUI();  
   }  
     
   private void initGUI() {  
      try {  
         GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());  
         getContentPane().setLayout(thisLayout);  
         setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);  
         this.setTitle("Principal");  
         {  
            mainTabbedPane = new JTabbedPane();  
            {  
               panelCadastro = new JPanel();  
               GroupLayout panelCadastroLayout = new GroupLayout((JComponent)panelCadastro);  
               panelCadastro.setLayout(panelCadastroLayout);  
               mainTabbedPane.addTab("Cadastro", null, panelCadastro, null);  
               panelCadastro.setPreferredSize(new java.awt.Dimension(355, 235));  
               {  
                  lblNome = new JLabel();  
                  lblNome.setText("Nome:");  
               }  
               {  
                  txtNome = new JTextField();  
               }  
               {  
                  txtEndereco = new JTextField();  
               }  
               {  
                  rbtMasculino = new JRadioButton();  
                  rbtMasculino.setText("Masculino");  
                  rbtMasculino.setSelected(true);  
                  getGrpSexo().add(rbtMasculino);  
               }  
               {  
                  rbtFeminino = new JRadioButton();  
                  rbtFeminino.setText("Feminino");  
                  getGrpSexo().add(rbtFeminino);  
               }  
               {  
                  lblSexo = new JLabel();  
                  lblSexo.setText("Sexo:");  
               }  
               {  
                  lblEndereco = new JLabel();  
                  lblEndereco.setText("Endereço:");  
               }  
               {  
                  txtCpf = new JTextField();  
               }  
               {  
                  lblCpf = new JLabel();  
                  lblCpf.setText("CPF:");  
               }  
               panelCadastroLayout.setVerticalGroup(panelCadastroLayout.createSequentialGroup()  
                  .addContainerGap()  
                  .addGroup(panelCadastroLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)  
                      .addComponent(txtNome, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)  
                      .addComponent(lblNome, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))  
                  .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)  
                  .addGroup(panelCadastroLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)  
                      .addComponent(txtCpf, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)  
                      .addComponent(lblCpf, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))  
                  .addGap(17)  
                  .addGroup(panelCadastroLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)  
                      .addComponent(txtEndereco, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)  
                      .addComponent(lblEndereco, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))  
                  .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)  
                  .addGroup(panelCadastroLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)  
                      .addComponent(rbtMasculino, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)  
                      .addComponent(rbtFeminino, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)  
                      .addComponent(lblSexo, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))  
                  .addGap(16)  
                  .addGroup(panelCadastroLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)  
                      .addComponent(getJTextField1(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)  
                      .addComponent(getJLabel1(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))  
                  .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)  
                  .addGroup(panelCadastroLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)  
                      .addComponent(getBtnCadastrar(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)  
                      .addComponent(getBtnLimpar(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))  
                  .addContainerGap(15, 15));  
               panelCadastroLayout.setHorizontalGroup(panelCadastroLayout.createSequentialGroup()  
                  .addContainerGap()  
                  .addGroup(panelCadastroLayout.createParallelGroup()  
                      .addComponent(getJLabel1(), GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 57, GroupLayout.PREFERRED_SIZE)  
                      .addComponent(lblSexo, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 57, GroupLayout.PREFERRED_SIZE)  
                      .addComponent(lblEndereco, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 57, GroupLayout.PREFERRED_SIZE)  
                      .addComponent(lblCpf, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 57, GroupLayout.PREFERRED_SIZE)  
                      .addComponent(lblNome, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 57, GroupLayout.PREFERRED_SIZE))  
                  .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)  
                  .addGroup(panelCadastroLayout.createParallelGroup()  
                      .addGroup(GroupLayout.Alignment.LEADING, panelCadastroLayout.createSequentialGroup()  
                          .addComponent(getJTextField1(), GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)  
                          .addGap(0, 12, Short.MAX_VALUE))  
                      .addGroup(GroupLayout.Alignment.LEADING, panelCadastroLayout.createSequentialGroup()  
                          .addComponent(getBtnCadastrar(), GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)  
                          .addGap(21)  
                          .addComponent(getBtnLimpar(), GroupLayout.PREFERRED_SIZE, 59, GroupLayout.PREFERRED_SIZE)  
                          .addGap(0, 12, Short.MAX_VALUE))  
                      .addGroup(panelCadastroLayout.createSequentialGroup()  
                          .addPreferredGap(getJTextField1(), rbtMasculino, LayoutStyle.ComponentPlacement.INDENT)  
                          .addGroup(panelCadastroLayout.createParallelGroup()  
                              .addGroup(GroupLayout.Alignment.LEADING, panelCadastroLayout.createSequentialGroup()  
                                  .addComponent(rbtMasculino, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE)  
                                  .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)  
                                  .addComponent(rbtFeminino, GroupLayout.PREFERRED_SIZE, 82, GroupLayout.PREFERRED_SIZE))  
                              .addComponent(txtEndereco, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)  
                              .addComponent(txtCpf, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)  
                              .addComponent(txtNome, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE))  
                          .addGap(0, 0, Short.MAX_VALUE)))  
                  .addContainerGap(82, 82));  
               panelCadastroLayout.linkSize(SwingConstants.HORIZONTAL, new Component[] {getBtnCadastrar(), getBtnLimpar()});  
               panelCadastroLayout.linkSize(SwingConstants.HORIZONTAL, new Component[] {lblEndereco, lblCpf, lblNome});  
            }  
            {  
               panelListagem = new JPanel();  
               mainTabbedPane.addTab("Listagem", null, panelListagem, null);  
               GroupLayout panelListagemLayout = new GroupLayout((JComponent)panelListagem);  
               panelListagem.setLayout(panelListagemLayout);  
               panelListagem.setPreferredSize(new java.awt.Dimension(252, 185));  
               panelListagemLayout.setVerticalGroup(panelListagemLayout.createSequentialGroup());  
               panelListagemLayout.setHorizontalGroup(panelListagemLayout.createSequentialGroup());  
            }  
         }  
         thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()  
            .addContainerGap()  
            .addComponent(mainTabbedPane, 0, 259, Short.MAX_VALUE)  
            .addContainerGap());  
         thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()  
            .addContainerGap(32, 32)  
            .addComponent(mainTabbedPane, GroupLayout.PREFERRED_SIZE, 360, GroupLayout.PREFERRED_SIZE)  
            .addContainerGap(54, Short.MAX_VALUE));  
         pack();  
         this.setSize(462, 321);  
      } catch (Exception e) {  
          //add your error handling code here  
         e.printStackTrace();  
      }  
   }  
     
   private ButtonGroup getGrpSexo() {  
      if(grpSexo == null) {  
         grpSexo = new ButtonGroup();  
      }  
      return grpSexo;  
   }  
     
   private JTextField getJTextField1() {  
      if(txtNasc == null) {  
         txtNasc = new JTextField();  
      }  
      return txtNasc;  
   }  
     
   private JLabel getJLabel1() {  
      if(lblNasc == null) {  
         lblNasc = new JLabel();  
         lblNasc.setText("Dt. Nasc:");  
      }  
      return lblNasc;  
   }  
     
   private JButton getBtnCadastrar() {  
      if(btnCadastrar == null) {  
         btnCadastrar = new JButton();  
         btnCadastrar.setText("Cadastrar");  
         btnCadastrar.addActionListener(new ActionListener() {  
            public void actionPerformed(ActionEvent evt) {  
               PessoaDTO pessoaDTO = new PessoaDTO();  
               try {  
                  pessoaDTO.setNome(txtNome.getText());  
                  pessoaDTO.setEndereco(txtEndereco.getText());  
                  pessoaDTO.setCpf(Long.parseLong(txtCpf.getText()));     
  
                  String nasc = txtNasc.getText();  
                  pessoaDTO.setDtNascimento(dateFormat.parse(nasc));  
                  char sexo = rbtMasculino.isSelected() ? 'M' : 'F';  
                  pessoaDTO.setSexo(sexo);  
  
                  PessoaBO pessoaBO = new PessoaBO();  
                  pessoaBO.cadastrar(pessoaDTO);  
                  MensagensUtil.addMsg(MainFrame.this, "Cadastro efetuado com sucesso!");     
               } catch(Exception e) {  
                  e.printStackTrace();  
                  MensagensUtil.addMsg(MainFrame.this, e.getMessage());              
               }  
            }  
         });  
      }  
      return btnCadastrar;  
   }  
     
   private JButton getBtnLimpar() {  
      if(btnLimpar == null) {  
         btnLimpar = new JButton();  
         btnLimpar.setText("Limpar");  
         btnLimpar.addActionListener(new ActionListener() {  
            public void actionPerformed(ActionEvent evt) {  
               txtNome.setText("");  
               txtCpf.setText("");  
               txtEndereco.setText("");  
               txtNasc.setText("");  
               txtNome.setText("");  
               rbtMasculino.setSelected(true);  
            }  
         });  
      }  
      return btnLimpar;  
   }  
  
}  


Read more: http://javafree.uol.com.br/topic-889086-Eclipse-Jigloo-e-MySql-nao-insere-dados.html#ixzz2KWNA2etE



PessoaBO.java


package br.edu.devmedia.jdbc.bo;  
  
import br.edu.devmedia.jdbc.dao.PessoaDAO;  
import br.edu.devmedia.jdbc.dto.PessoaDTO;  
import br.edu.devmedia.jdbc.exception.NegocioException;  
  
public class PessoaBO {  
     
   public void cadastrar(PessoaDTO pessoaDTO) throws NegocioException {  
      try {  
         PessoaDAO pessoaDAO = new PessoaDAO();  
         pessoaDAO.inserir(pessoaDTO);  
      }   catch(Exception exception)   {  
         throw new NegocioException(exception.getMessage());  
      }  
   }  
  
}  


Read more: http://javafree.uol.com.br/topic-889086-Eclipse-Jigloo-e-MySql-nao-insere-dados.html#ixzz2KWNGfUzr



erros:

java.lang.NullPointerException
at br.edu.devmedia.jdbc.gui.MainFrame$2.actionPerformed(MainFrame.java:260)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Read more: http://javafree.uol.com.br/topic-889086-Eclipse-Jigloo-e-MySql-nao-insere-dados.html#ixzz2KWNLCtN6
Responder

Posts

14/02/2013

Vanessa Fortes

Posta seu método inserir da DAO?
Responder

15/02/2013

Davi Costa

posta essa linha:
at br.edu.devmedia.jdbc.gui.MainFrame$2.actionPerformed(MainFrame.java:260)

foi ai que quebrou

att Davi
Responder