Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. public class FabricanteDAO {
  2.  
  3. ConexaoFactory cf;
  4. PreparedStatement pst;
  5. ArrayList<Fabricante> lista = new ArrayList<Fabricante>();
  6. ArrayList<Fabricante> lista2 = new ArrayList<Fabricante>();;
  7. Fabricante retorno = null;
  8.  
  9. public void salvar(Fabricante f){
  10. StringBuilder sql = new StringBuilder();
  11. sql.append("INSERT INTO fabricante");
  12. sql.append("(descricao) ");
  13. sql.append("VALUES (?)");
  14. try {
  15. Connection conexao = cf.conectar();
  16. PreparedStatement pst = conexao.prepareStatement(sql.toString());
  17. pst.setString(1,f.getDescricao());
  18. pst.executeUpdate();
  19. JOptionPane.showMessageDialog(null,"Salvo com Sucesso !");
  20. }catch(SQLException ex) {
  21. JOptionPane.showMessageDialog(null,"Erro ao Salvar: "+ex.getMessage());
  22. }
  23. }
  24.  
  25. public void excluir(Fabricante f){
  26. StringBuilder sql = new StringBuilder();
  27. sql.append("DELETE FROM fabricante ");
  28. sql.append("WHERE codigo = ? ");
  29. try {
  30. Connection conexao = cf.conectar();
  31. PreparedStatement pst = conexao.prepareStatement(sql.toString());
  32. pst.setLong(1,f.getCodigo());
  33. pst.executeUpdate();
  34. JOptionPane.showMessageDialog(null,"Salvo com Sucesso !");
  35. }catch(SQLException ex) {
  36. JOptionPane.showMessageDialog(null,"Erro ao Salvar: "+ex.getMessage());
  37. }
  38. }
  39.  
  40. public void editar(Fabricante f){
  41. StringBuilder sql = new StringBuilder();
  42. sql.append("UPDATE fabricante ");
  43. sql.append("SET descricao = ?");
  44. try {
  45. Connection conexao = cf.conectar();
  46. PreparedStatement pst = conexao.prepareStatement(sql.toString());
  47. pst.setString(1,f.getDescricao());
  48. pst.setLong(2,f.getCodigo());
  49. pst.executeUpdate();
  50. JOptionPane.showMessageDialog(null,"Salvo com Sucesso !");
  51. }catch(SQLException ex) {
  52. JOptionPane.showMessageDialog(null,"Erro ao Salvar: "+ex.getMessage());
  53. }
  54. }
  55.  
  56. public Fabricante consulta(Fabricante f){
  57. StringBuilder sql = new StringBuilder();
  58. sql.append("SELECT codigo, descricao ");
  59. sql.append("FROM fabricante ");
  60. sql.append("WHERE codigo = ?");
  61. try {
  62. Connection conexao = cf.conectar();
  63. PreparedStatement pst = conexao.prepareStatement(sql.toString());
  64. pst.setLong(1,f.getCodigo());
  65. ResultSet rs = pst.executeQuery();
  66. if(rs.next()){
  67. retorno = new Fabricante();
  68. retorno.setCodigo(rs.getLong("codigo"));
  69. retorno.setDescricao(rs.getString("descricao"));
  70. }
  71. }catch(SQLException ex) {
  72. JOptionPane.showMessageDialog(null,"Erro ao Salvar: "+ex.getMessage());
  73. }
  74. return retorno;
  75. }
  76.  
  77. public ArrayList<Fabricante> listar() throws SQLException{
  78. StringBuilder sql = new StringBuilder();
  79. sql.append("SELECT codigo, descricao ");
  80. sql.append("FROM fabricante ");
  81. sql.append("WHERE BY descricao ASC ");
  82. Connection conexao = cf.conectar();
  83. PreparedStatement pst = conexao.prepareStatement(sql.toString());
  84. ResultSet rs = pst.executeQuery();
  85.  
  86. while(rs.next()){
  87. Fabricante f = new Fabricante();
  88. f.setCodigo(rs.getLong("codigo"));
  89. f.setDescricao(rs.getString("descricao"));
  90. lista.add(f);
  91. }
  92. return lista;
  93. }
  94.  
  95. public ArrayList<Fabricante> listarDesc(Fabricante f){
  96. StringBuilder sql = new StringBuilder();
  97. sql.append("SELECT codigo, descricao ");
  98. sql.append("FROM fabricante ");
  99. sql.append("WHERE descricao LIKE = ? ");
  100. sql.append("ORDER BY descricao ASC ");
  101. try {
  102. cf.conn = cf.conectar();
  103. PreparedStatement pst = cf.conn.prepareStatement(sql.toString());
  104. pst.setString(1, "%"+f.getDescricao()+"%");
  105. ResultSet rs = pst.executeQuery();
  106.  
  107. while(rs.next()){
  108. Fabricante item = new Fabricante();
  109. item.setCodigo(rs.getLong("codigo"));
  110. item.setDescricao(rs.getString("descricao"));
  111. lista2.add(item);
  112. }
  113. }catch(SQLException ex) {
  114. JOptionPane.showMessageDialog(null,"Erro ao Salvar: "+ex.getMessage());
  115. }
  116. return lista2;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement