Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.88 KB | None | 0 0
  1. public class FormV1 extends javax.swing.JFrame {
  2.  
  3.     private DefaultComboBoxModel<Pessoa>
  4.             pessoas = new  DefaultComboBoxModel<>();
  5.  
  6.  
  7. private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
  8.         // TODO add your handling code here:
  9.         pessoas.removeAllElements();
  10.         cbPessoas.setModel(pessoas);
  11. }      
  12.  
  13.  
  14.  
  15. private void btnIncluirActionPerformed(java.awt.event.ActionEvent evt) {                                          
  16.  
  17.         Pessoa p = new Pessoa();
  18.         p.setIdade(Integer.parseInt(txt_idade.getText()));
  19.         p.setNome(txt_nome.getText());
  20.        
  21.         String a = txt_sexo.getText();
  22.         char b[] = a.toCharArray();
  23.         p.setSexo(b[0]);
  24.        
  25.         //adicona o produto no model
  26.         pessoas.addElement(p);
  27.        
  28.         //apaga conteudo dos texts
  29.         txt_nome.setText("");
  30.         txt_idade.setText("");
  31.         txt_sexo.setText("");
  32. }                                                          
  33.  
  34.  
  35. private void cbPessoasItemStateChanged(java.awt.event.ItemEvent evt) {                                          
  36.  
  37.         //se for o item selecionado
  38.         if(evt.getStateChange() == ItemEvent.SELECTED) {
  39.             Pessoa p = (Pessoa)cbPessoas.getSelectedItem();
  40.             txt_idade.setText(String.valueOf(p.getIdade()));
  41.             txt_nome.setText(p.getNome());
  42.             txt_sexo.setText(Character.toString(p.getSexo()));
  43.         }
  44. }
  45.  
  46.  
  47. //Gravação no Banco de dados
  48.  
  49.         try {
  50.             int codigo = Integer.parseInt(txt_codigo.getText());
  51.             String nome;
  52.             nome = txt_nome.getText();
  53.             int idade = Integer.parseInt(txt_idade.getText());
  54.             String sexo = txt_sexo.getText();
  55.            
  56.             String sql;
  57.             sql = ("INSERT INTO p (codigo,nome,idade,sexo) VALUES ('" + codigo + "','" + nome + "','" + idade +   "','"  + sexo + "')");
  58.             PreparedStatement st;
  59.             st = DriverManager.getConnection("jdbc:mysql://localhost/pessoa","root","").prepareStatement(sql);
  60.             st.execute();
  61.             System.out.println("Sucesso");
  62.             st.close();
  63.  
  64.         }
  65.         catch (SQLException ex) {
  66.             Logger.getLogger(FormV2BD.class.getName()).log(Level.SEVERE, null, ex);
  67.         }
  68.         catch(NumberFormatException e)
  69.         {
  70.             JOptionPane.showMessageDialog(null,"Dados Inválidos");
  71.         }
  72.  
  73.  
  74. //Criação DAO
  75.  
  76.  
  77. package NovoProva;
  78.  
  79. import java.sql.SQLException;
  80. import java.util.List;
  81.  
  82.  
  83. public interface DAO <T>
  84. {
  85.     public T pesquisar(T obj) throws SQLException;
  86.     public List<T> buscar(String criterio) throws SQLException;
  87.  
  88. }
  89.  
  90.  
  91. //PessoaDAO
  92.  
  93. package NovoProva;
  94.  
  95. import java.sql.*;
  96. import java.util.*;
  97. import prova.BancoFactory;
  98.  
  99.  
  100. public class PessoaDAO implements DAO<PessoaVO>
  101. {
  102.     private PreparedStatement pst;
  103.     private Statement st;
  104.     private ResultSet rs;
  105.  
  106.     @Override
  107.     public PessoaVO pesquisar(PessoaVO obj) throws SQLException
  108.     {
  109.         try
  110.         {
  111.             BancoFactory.Abrir();
  112.             pst = BancoFactory.getConnection().prepareStatement("SELECT * FROM p where codigo = ?");
  113.             pst.setInt(1, obj.getCodigo());
  114.            
  115.             rs = pst.executeQuery();
  116.             if(rs.next())
  117.             {
  118.                 System.out.println("Achou");
  119.                 return obj;
  120.                
  121.             }
  122.             else
  123.             {
  124.                 return null;
  125.             }
  126.         }
  127.         finally
  128.         {
  129.             BancoFactory.fechar();
  130.         }
  131.     }  
  132. }
  133. //FORM
  134.  
  135. public class FormV2BD extends javax.swing.JFrame {
  136.     private PessoaVO pessoaVO;
  137.     private PessoaDAO dao = new PessoaDAO();
  138.     private static FormV2BD pessoa = null;
  139.     public static FormV2BD getInstance(){
  140.         if(pessoa == null)
  141.         {
  142.             pessoa = new FormV2BD();
  143.         }
  144.         return pessoa;
  145.     }
  146.  
  147.  
  148. //Buscar no Form
  149.  
  150.     private void bt_buscarActionPerformed(java.awt.event.ActionEvent evt) {                                          
  151.        pessoaVO = new PessoaVO();
  152.        pessoaVO.setCodigo(Integer.parseInt(txt_codigo.getText()));
  153.        try
  154.         {
  155.            pessoaVO = dao.pesquisar(pessoaVO);
  156.         }
  157.        catch (SQLException ex)
  158.        {
  159.            System.out.println("Erro no Banco");
  160.        }
  161.        
  162.         txt_nome.setText(pessoaVO.getNome());
  163.         txt_idade.setText(String.valueOf(pessoaVO.getIdade()));
  164.         txt_sexo.setText(pessoaVO.getSexo());
  165.        
  166.        if(pessoaVO == null)
  167.        {
  168.            JOptionPane.showMessageDialog(rootPane, "Pessoa não encontrada!");
  169.        }
  170.        else
  171.         JOptionPane.showMessageDialog(rootPane, "Pessoa encontrada!");
  172.         txt_nome.setText(pessoaVO.getNome());
  173.         txt_idade.setText(String.valueOf(pessoaVO.getIdade()));
  174.         txt_sexo.setText(pessoaVO.getSexo());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement