Guest User

daoUsuario

a guest
Oct 24th, 2016
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. public class DaoUsuario extends DaoGenerico<Usuario, Integer> {
  2.  
  3. @Override
  4. public void grabar(Usuario u) throws BusinessException {
  5. Connection con = ConexionJdbc.getConnection();
  6. ResultSet rs = null;
  7. PreparedStatement pstm = null;
  8.  
  9. try {
  10. String sql = "Insert into usuario ( username, password, tipo, rol, grupo, departamento, nombre, apellido1, apellido2, domicilio, poblacion, codpostal, email, telefono) values ?";
  11. pstm = con.prepareStatement(sql,pstm.RETURN_GENERATED_KEYS);
  12. pstm.setString(1, u.getUserName());
  13. pstm.setString(2, u.getPassword());
  14. pstm.setInt(3, u.getTipo());
  15. pstm.setInt(4, u.getRol());
  16. pstm.setString(5, u.getGrupo());
  17. pstm.setInt(6, u.getDepartamento());
  18. pstm.setString(7, u.getNombre());
  19. pstm.setString(8, u.getApellido1());
  20. pstm.setString(9, u.getApellido2());
  21. pstm.setString(10, u.getDomicilio());
  22. pstm.setString(11, u.getPoblacion());
  23. pstm.setString(12, u.getCodPostal());
  24. pstm.setString(13, u.getEmail());
  25. pstm.setString(14, u.getTelefono());
  26.  
  27. // insertar
  28. pstm.executeUpdate();
  29.  
  30. // obtener clave generada
  31. rs = pstm.getGeneratedKeys();
  32. if (rs.first()) {
  33. Integer id = rs.getInt(1);
  34. u.setIdUsuario(id);
  35. }
  36.  
  37. } catch (SQLException e) {
  38. throw new BusinessException("Error al insertar");
  39. } finally {
  40. ConexionJdbc.cerrar(pstm);
  41. }
  42. }
  43.  
  44. @Override
  45. public void actualizar(Usuario u) throws BusinessException {
  46. Connection con = ConexionJdbc.getConnection();
  47. PreparedStatement pstm = null;
  48.  
  49. try {
  50. String sql = "update usuario set username =?, password=?, tipo=?, rol=?, grupo=?, departamento=?, nombre=?, apellido1=?, apellido2=?, domicilio=?, poblacion=?, codpostal=?, email=?, telefono = ? where idusuario = ?";
  51. pstm = con.prepareStatement(sql);
  52. pstm.setString(1, u.getUserName());
  53. pstm.setString(2, u.getPassword());
  54. pstm.setInt(3, u.getTipo());
  55. pstm.setInt(4, u.getRol());
  56. pstm.setString(5, u.getGrupo());
  57. pstm.setInt(6, u.getDepartamento());
  58. pstm.setString(7, u.getNombre());
  59. pstm.setString(8, u.getApellido1());
  60. pstm.setString(9, u.getApellido2());
  61. pstm.setString(10, u.getDomicilio());
  62. pstm.setString(11, u.getPoblacion());
  63. pstm.setString(12, u.getCodPostal());
  64. pstm.setString(13, u.getEmail());
  65. pstm.setString(14, u.getTelefono());
  66. pstm.setInt(15, u.getIdUsuario());
  67.  
  68. if (pstm.executeUpdate() == 0)
  69. throw new BusinessException("Elemento no encontrado");
  70.  
  71. } catch (SQLException e) {
  72. throw new BusinessException("Error al insertar");
  73. } finally {
  74. ConexionJdbc.cerrar(pstm);
  75. }
  76. }
  77.  
  78. @Override
  79. public void grabarOActualizar(Usuario u) throws BusinessException {
  80. if(buscarPorId(u.getIdUsuario())!=null)actualizar(u);
  81. else grabar(u);
  82. }
  83.  
  84. @Override
  85. public void borrar(Usuario u) throws BusinessException {
  86. borrar(u.getIdUsuario());
  87. }
  88.  
  89. @Override
  90. public void borrar(Integer id) throws BusinessException {
  91. Connection con = ConexionJdbc.getConnection();
  92. PreparedStatement pstm = null;
  93. try {
  94. String sql = " Delete from usuario where idusuario = ?";
  95. pstm = con.prepareStatement(sql);
  96. pstm.setInt(1, id);
  97.  
  98. if (pstm.executeUpdate() == 0)
  99. throw new BusinessException("Elemento no encontrado");
  100.  
  101. } catch (SQLException e) {
  102. throw new BusinessException("Error al eliminar");
  103. } finally {
  104. ConexionJdbc.cerrar(pstm);
  105. }
  106. }
  107.  
  108. @Override
  109. public Usuario buscarPorId(Integer id) throws BusinessException {
  110. Connection con = ConexionJdbc.getConnection();
  111. ResultSet rs = null;
  112. PreparedStatement pstm = null;
  113. Usuario user = null;
  114. try {
  115. String sql = "Select * from usuario where idusuario = ?";
  116. pstm = con.prepareStatement(sql);
  117. pstm.setInt(1, id);
  118. rs = pstm.executeQuery();
  119.  
  120. user = new Usuario(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getInt(4), rs.getInt(5),
  121. rs.getString(6), rs.getInt(7), rs.getString(8), rs.getString(9), rs.getString(10), rs.getString(11),
  122. rs.getString(12), rs.getString(13), rs.getString(14), rs.getString(15));
  123.  
  124. } catch (SQLException e) {
  125. throw new BusinessException("Error al insertar");
  126. } finally {
  127. ConexionJdbc.cerrar(pstm);
  128. }
  129.  
  130. return user;
  131. }
  132.  
  133. @Override
  134. public List<Usuario> buscarTodos() throws BusinessException {
  135. List<Usuario> result = new ArrayList<>();
  136. Connection con = ConexionJdbc.getConnection();
  137. ResultSet rs = null;
  138. PreparedStatement pstm = null;
  139. try {
  140. String sql = "select * from usuario order by idusuario";
  141. pstm = con.prepareStatement(sql);
  142. rs = pstm.executeQuery();
  143. while (rs.next()) {
  144. Usuario user = new Usuario(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getInt(4), rs.getInt(5),
  145. rs.getString(6), rs.getInt(7), rs.getString(8), rs.getString(9), rs.getString(10),
  146. rs.getString(11), rs.getString(12), rs.getString(13), rs.getString(14), rs.getString(15));
  147. result.add(user);
  148. }
  149. } catch (SQLException e) {
  150. throw new BusinessException("Error al consultar");
  151. } finally {
  152. ConexionJdbc.cerrar(pstm);
  153. }
  154. return result;
  155. }
  156.  
  157. }
Add Comment
Please, Sign In to add comment