Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DaoUsuario extends DaoGenerico<Usuario, Integer> {
- @Override
- public void grabar(Usuario u) throws BusinessException {
- Connection con = ConexionJdbc.getConnection();
- ResultSet rs = null;
- PreparedStatement pstm = null;
- try {
- String sql = "Insert into usuario ( username, password, tipo, rol, grupo, departamento, nombre, apellido1, apellido2, domicilio, poblacion, codpostal, email, telefono) values ?";
- pstm = con.prepareStatement(sql,pstm.RETURN_GENERATED_KEYS);
- pstm.setString(1, u.getUserName());
- pstm.setString(2, u.getPassword());
- pstm.setInt(3, u.getTipo());
- pstm.setInt(4, u.getRol());
- pstm.setString(5, u.getGrupo());
- pstm.setInt(6, u.getDepartamento());
- pstm.setString(7, u.getNombre());
- pstm.setString(8, u.getApellido1());
- pstm.setString(9, u.getApellido2());
- pstm.setString(10, u.getDomicilio());
- pstm.setString(11, u.getPoblacion());
- pstm.setString(12, u.getCodPostal());
- pstm.setString(13, u.getEmail());
- pstm.setString(14, u.getTelefono());
- // insertar
- pstm.executeUpdate();
- // obtener clave generada
- rs = pstm.getGeneratedKeys();
- if (rs.first()) {
- Integer id = rs.getInt(1);
- u.setIdUsuario(id);
- }
- } catch (SQLException e) {
- throw new BusinessException("Error al insertar");
- } finally {
- ConexionJdbc.cerrar(pstm);
- }
- }
- @Override
- public void actualizar(Usuario u) throws BusinessException {
- Connection con = ConexionJdbc.getConnection();
- PreparedStatement pstm = null;
- try {
- String sql = "update usuario set username =?, password=?, tipo=?, rol=?, grupo=?, departamento=?, nombre=?, apellido1=?, apellido2=?, domicilio=?, poblacion=?, codpostal=?, email=?, telefono = ? where idusuario = ?";
- pstm = con.prepareStatement(sql);
- pstm.setString(1, u.getUserName());
- pstm.setString(2, u.getPassword());
- pstm.setInt(3, u.getTipo());
- pstm.setInt(4, u.getRol());
- pstm.setString(5, u.getGrupo());
- pstm.setInt(6, u.getDepartamento());
- pstm.setString(7, u.getNombre());
- pstm.setString(8, u.getApellido1());
- pstm.setString(9, u.getApellido2());
- pstm.setString(10, u.getDomicilio());
- pstm.setString(11, u.getPoblacion());
- pstm.setString(12, u.getCodPostal());
- pstm.setString(13, u.getEmail());
- pstm.setString(14, u.getTelefono());
- pstm.setInt(15, u.getIdUsuario());
- if (pstm.executeUpdate() == 0)
- throw new BusinessException("Elemento no encontrado");
- } catch (SQLException e) {
- throw new BusinessException("Error al insertar");
- } finally {
- ConexionJdbc.cerrar(pstm);
- }
- }
- @Override
- public void grabarOActualizar(Usuario u) throws BusinessException {
- if(buscarPorId(u.getIdUsuario())!=null)actualizar(u);
- else grabar(u);
- }
- @Override
- public void borrar(Usuario u) throws BusinessException {
- borrar(u.getIdUsuario());
- }
- @Override
- public void borrar(Integer id) throws BusinessException {
- Connection con = ConexionJdbc.getConnection();
- PreparedStatement pstm = null;
- try {
- String sql = " Delete from usuario where idusuario = ?";
- pstm = con.prepareStatement(sql);
- pstm.setInt(1, id);
- if (pstm.executeUpdate() == 0)
- throw new BusinessException("Elemento no encontrado");
- } catch (SQLException e) {
- throw new BusinessException("Error al eliminar");
- } finally {
- ConexionJdbc.cerrar(pstm);
- }
- }
- @Override
- public Usuario buscarPorId(Integer id) throws BusinessException {
- Connection con = ConexionJdbc.getConnection();
- ResultSet rs = null;
- PreparedStatement pstm = null;
- Usuario user = null;
- try {
- String sql = "Select * from usuario where idusuario = ?";
- pstm = con.prepareStatement(sql);
- pstm.setInt(1, id);
- rs = pstm.executeQuery();
- user = new Usuario(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getInt(4), rs.getInt(5),
- rs.getString(6), rs.getInt(7), rs.getString(8), rs.getString(9), rs.getString(10), rs.getString(11),
- rs.getString(12), rs.getString(13), rs.getString(14), rs.getString(15));
- } catch (SQLException e) {
- throw new BusinessException("Error al insertar");
- } finally {
- ConexionJdbc.cerrar(pstm);
- }
- return user;
- }
- @Override
- public List<Usuario> buscarTodos() throws BusinessException {
- List<Usuario> result = new ArrayList<>();
- Connection con = ConexionJdbc.getConnection();
- ResultSet rs = null;
- PreparedStatement pstm = null;
- try {
- String sql = "select * from usuario order by idusuario";
- pstm = con.prepareStatement(sql);
- rs = pstm.executeQuery();
- while (rs.next()) {
- Usuario user = new Usuario(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getInt(4), rs.getInt(5),
- rs.getString(6), rs.getInt(7), rs.getString(8), rs.getString(9), rs.getString(10),
- rs.getString(11), rs.getString(12), rs.getString(13), rs.getString(14), rs.getString(15));
- result.add(user);
- }
- } catch (SQLException e) {
- throw new BusinessException("Error al consultar");
- } finally {
- ConexionJdbc.cerrar(pstm);
- }
- return result;
- }
- }
Add Comment
Please, Sign In to add comment