Advertisement
Irlan

AutorDAO()

Apr 4th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.23 KB | None | 0 0
  1. package Connection;
  2.  
  3. import Factory.FactoryCon;
  4. import Model.Autor;
  5. import java.sql.Connection;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.ArrayList;
  11. import java.util.Date;
  12. import java.util.List;
  13. import javax.swing.JOptionPane;
  14.  
  15. /**
  16.  *
  17.  * @author IrlanJR
  18.  */
  19. public class AutorDAO {
  20.    
  21.     private Connection con;
  22.     private PreparedStatement pstmtInsert;
  23.     private PreparedStatement pstmtFind;
  24.     private PreparedStatement pstmtFindAll;
  25.     private PreparedStatement pstmtDelete;
  26.     private PreparedStatement pstmtAux;
  27.     private Statement stmtRecords; // Pensei que usaria
  28.    
  29.     private ResultSet rsRecords;
  30.    
  31.    
  32.     public AutorDAO(){
  33.  
  34.         try {
  35.            
  36.             con = FactoryCon.getConection(); // Conexão com o banco
  37.            
  38.             pstmtInsert =  con.prepareStatement("INSERT INTO autor(nome,dataNasc) VALUES ( ?,? ); ");
  39.             pstmtDelete = con.prepareStatement("DELETE FROM autor WHERE cod = ?; ");
  40.             pstmtFind = con.prepareStatement("SELECT * FROM autor WHERE cod = ?; ");
  41.             pstmtFindAll = con.prepareStatement("SELECT cod,nome,dataNasc FROM autor;");
  42.             stmtRecords = con.createStatement();
  43.            
  44.            
  45.            
  46.         }
  47. //        catch( Exception e){
  48. //            System.out.println("Erro no carregamento dos statements: "+ e.getMessage());
  49. //        }
  50.         catch( SQLException e){
  51.             System.out.println("Erro no carregamento dos statements: "+ e.getMessage());
  52.         }
  53.        
  54.        
  55.        
  56.     };
  57.  
  58.    
  59.     public int inserir(Autor autor) {
  60.        
  61.         //Retorna valor do código;
  62.         int registro;
  63.        
  64.         try{
  65.              // Casting que é sempre utilizado em java
  66.            
  67.            
  68.             pstmtInsert.setString(1, autor.getNome());
  69.             pstmtInsert.setDate(2, convertDate(autor.getDataNasc()));
  70.            
  71.             registro = pstmtInsert.executeUpdate();
  72.            
  73.             JOptionPane.showMessageDialog(null,"Cadastro concluido com sucesso!!");
  74.            
  75.        
  76.        
  77.             pstmtAux = con.prepareStatement("SELECT @@IDENTITY");
  78.             rsRecords = pstmtAux.executeQuery();
  79.             int codigo = 0;
  80.            
  81.             if((registro == 1) && (rsRecords != null )){
  82.                
  83.                 rsRecords.next();
  84.                 codigo = rsRecords.getInt(1);
  85.                 return codigo;  // Esse código é mostrado na tela.
  86.                
  87.             }else{
  88.                
  89.                 return codigo;  // Esse código é mostrado na tela.
  90.             }
  91.            
  92.         }
  93.         catch( SQLException e ){
  94.            
  95.             System.out.println("Erro em AutorDAO.inserir(): "+ e.getMessage());
  96.             return 0;
  97.         }
  98.        
  99.     }
  100.  
  101.  
  102.     public void deletar(int cod) {
  103.        
  104.         try{
  105.             pstmtDelete.setInt(1, cod);
  106.            
  107.             pstmtDelete.executeUpdate();
  108.            
  109.             JOptionPane.showMessageDialog(null,"Usuario excluido com sucesso!!");
  110.         }
  111.         catch( SQLException e ){
  112.            
  113.            System.out.println("Erro na deleção: "+ e.getMessage());
  114.         }
  115.        
  116.        
  117.     }
  118.  
  119.    
  120.     public List<Autor> acharTodos() {
  121.        
  122.         List<Autor> listaAutor = new ArrayList<>();
  123.        
  124.         ResultSet rs = null;
  125.        
  126.         try {
  127.          rs = pstmtFindAll.executeQuery();
  128.          
  129.             while(rs.next()){
  130.                
  131.                 Autor autor = new Autor();
  132.                
  133.                 autor.setCod(rs.getInt("Cod"));
  134.                 autor.setNome(rs.getString("Nome"));
  135.                 autor.setDataNasc(rs.getDate("dataNasc"));
  136.                
  137.                 listaAutor.add(autor);
  138.                
  139.             }
  140.            
  141.             return listaAutor;
  142.            
  143.         } catch (SQLException ex) {
  144.            
  145.             JOptionPane.showMessageDialog(null," Erro no carregamento: "+ ex.getMessage());
  146.             return null;
  147.         }
  148.        
  149.        
  150.        
  151.     }
  152.      
  153.     public Autor achar(int cod){
  154.        
  155.         try{
  156.             pstmtFind.setInt(1, cod);
  157.            
  158.             ResultSet rs = pstmtFind.executeQuery();
  159.            
  160.             if(rs.next()){
  161.                
  162.                 Autor autor = new Autor();
  163.                
  164.                 autor.setCod(rs.getInt("COD"));
  165.                 autor.setNome(rs.getString("NOME"));
  166.                 autor.setDataNasc(rs.getDate("DATANASC"));
  167.  
  168.                 return autor;
  169.                
  170.             }else{
  171.                
  172.                 throw new SQLException("Record Not Found");
  173.             }
  174.  
  175.         }
  176.         catch(SQLException e){
  177.            
  178.             System.out.println("Erro na busca: "+ e.getMessage());
  179.             return null;
  180.             }
  181.        
  182.         }
  183.    
  184.     public java.sql.Date convertDate(Date data) { // Método encontrado na GUJ que ajuda a converter em sql.date
  185.             if(data == null){
  186.                  return null;
  187.             }else{
  188.             return new java.sql.Date(data.getTime());
  189.         }
  190.     }
  191.    
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement