/baseDatos/src/main/java/dao/AlumnosDAO.java

https://bitbucket.org/ma-martinez/clon-oscar-marcasdaw · Java · 288 lines · 238 code · 39 blank · 11 comment · 6 complexity · 8ffe2a20c529a38db957c28db2b01623 MD5 · raw file

  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package dao;
  7. import model.Alumno;
  8. import java.math.BigInteger;
  9. import java.sql.Connection;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13. import java.util.ArrayList;
  14. import java.util.Date;
  15. import java.util.List;
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18. import javax.naming.Context;
  19. import javax.naming.InitialContext;
  20. import javax.sql.DataSource;
  21. import org.apache.commons.dbutils.QueryRunner;
  22. import org.apache.commons.dbutils.ResultSetHandler;
  23. import org.apache.commons.dbutils.handlers.BeanHandler;
  24. import org.apache.commons.dbutils.handlers.BeanListHandler;
  25. import org.apache.commons.dbutils.handlers.ScalarHandler;
  26. /**
  27. *
  28. * @author oscar
  29. */
  30. public class AlumnosDAO {
  31. public List<Alumno> getAllAlumnos() {
  32. List<Alumno> lista = null;
  33. DBConnection db = new DBConnection();
  34. Connection con = null;
  35. try {
  36. con = db.getConnection();
  37. QueryRunner qr = new QueryRunner();
  38. ResultSetHandler<List<Alumno>> h
  39. = new BeanListHandler<Alumno>(Alumno.class);
  40. lista = qr.query(con, "select * FROM ALUMNOS", h);
  41. } catch (Exception ex) {
  42. Logger.getLogger(AlumnosDAO.class.getName()).log(Level.SEVERE, null, ex);
  43. } finally {
  44. db.cerrarConexion(con);
  45. }
  46. return lista;
  47. }
  48. public List<Alumno> getAllAlumnosJDBC() {
  49. List<Alumno> lista = new ArrayList<>();
  50. Alumno nuevo = null;
  51. DBConnection db = new DBConnection();
  52. Connection con = null;
  53. Statement stmt = null;
  54. ResultSet rs = null;
  55. try {
  56. con = db.getConnection();
  57. stmt = con.createStatement();
  58. String sql;
  59. sql = "SELECT * FROM ALUMNOS";
  60. rs = stmt.executeQuery(sql);
  61. //STEP 5: Extract data from result set
  62. while (rs.next()) {
  63. //Retrieve by column name
  64. int id = rs.getInt("id");
  65. String nombre = rs.getString("nombre");
  66. Date fn = rs.getDate("fecha_nacimiento");
  67. Boolean mayor = rs.getBoolean("mayor_edad");
  68. nuevo = new Alumno();
  69. nuevo.setFecha_nacimiento(fn);
  70. nuevo.setId(id);
  71. nuevo.setMayor_edad(mayor);
  72. nuevo.setNombre(nombre);
  73. lista.add(nuevo);
  74. }
  75. } catch (Exception ex) {
  76. Logger.getLogger(AlumnosDAO.class.getName()).log(Level.SEVERE, null, ex);
  77. } finally {
  78. try {
  79. if (rs != null) {
  80. rs.close();
  81. }
  82. if (stmt != null) {
  83. stmt.close();
  84. }
  85. } catch (SQLException ex) {
  86. Logger.getLogger(AlumnosDAO.class.getName()).log(Level.SEVERE, null, ex);
  87. }
  88. db.cerrarConexion(con);
  89. }
  90. return lista;
  91. }
  92. public Alumno getUserById(int id) {
  93. Alumno user = null;
  94. DBConnection db = new DBConnection();
  95. Connection con = null;
  96. try {
  97. Context ctx = new InitialContext();
  98. DataSource ds = (DataSource) ctx.lookup("jdbc/db4free");
  99. con = ds.getConnection();
  100. QueryRunner qr = new QueryRunner();
  101. ResultSetHandler<Alumno> h
  102. = new BeanHandler<>(Alumno.class);
  103. user = qr.query(con, "select * FROM ALUMNOS where ID = ?", h, id);
  104. } catch (Exception ex) {
  105. Logger.getLogger(AlumnosDAO.class.getName()).log(Level.SEVERE, null, ex);
  106. } finally {
  107. db.cerrarConexion(con);
  108. }
  109. return user;
  110. }
  111. public Alumno getUser(Alumno userOriginal) {
  112. Alumno user = null;
  113. DBConnection db = new DBConnection();
  114. Connection con = null;
  115. try {
  116. con = db.getConnection();
  117. QueryRunner qr = new QueryRunner();
  118. ResultSetHandler<Alumno> h
  119. = new BeanHandler<>(Alumno.class);
  120. user = qr.query(con, "select * FROM LOGIN where USER = ?", h, userOriginal.getNombre());
  121. } catch (Exception ex) {
  122. Logger.getLogger(AlumnosDAO.class.getName()).log(Level.SEVERE, null, ex);
  123. } finally {
  124. db.cerrarConexion(con);
  125. }
  126. return user;
  127. }
  128. public void delUser(Alumno u) {
  129. DBConnection db = new DBConnection();
  130. Connection con = null;
  131. try {
  132. con = db.getConnection();
  133. QueryRunner qr = new QueryRunner();
  134. int filas = qr.update(con,
  135. "DELETE FROM LOGIN WHERE ID=?",
  136. u.getId());
  137. } catch (Exception ex) {
  138. Logger.getLogger(AlumnosDAO.class.getName()).log(Level.SEVERE, null, ex);
  139. } finally {
  140. db.cerrarConexion(con);
  141. }
  142. }
  143. public void updateUser(Alumno u) {
  144. DBConnection db = new DBConnection();
  145. Connection con = null;
  146. try {
  147. con = db.getConnection();
  148. QueryRunner qr = new QueryRunner();
  149. int filas = qr.update(con,
  150. "UPDATE LOGIN SET MAIL=? WHERE USER=?",
  151. "", "");
  152. } catch (Exception ex) {
  153. Logger.getLogger(AlumnosDAO.class.getName()).log(Level.SEVERE, null, ex);
  154. } finally {
  155. db.cerrarConexion(con);
  156. }
  157. }
  158. public boolean recuperarUser(Alumno u, String activacion) {
  159. DBConnection db = new DBConnection();
  160. Connection con = null;
  161. boolean ok = false;
  162. try {
  163. con = db.getConnection();
  164. QueryRunner qr = new QueryRunner();
  165. int filas = qr.update(con,
  166. "UPDATE LOGIN SET ACTIVACION=?, FECHA_RENOVACION = now() WHERE ID=?",
  167. activacion, u.getId());
  168. if (filas >= 1) {
  169. ok = true;
  170. }
  171. } catch (Exception ex) {
  172. Logger.getLogger(AlumnosDAO.class.getName()).log(Level.SEVERE, null, ex);
  173. } finally {
  174. db.cerrarConexion(con);
  175. }
  176. return ok;
  177. }
  178. public void updateUserPassword(Alumno u) {
  179. DBConnection db = new DBConnection();
  180. Connection con = null;
  181. try {
  182. con = db.getConnection();
  183. QueryRunner qr = new QueryRunner();
  184. int filas = qr.update(con,
  185. "UPDATE LOGIN SET PASSWORD=?,MAIL=? WHERE USER=?",
  186. "", "", "");
  187. } catch (Exception ex) {
  188. Logger.getLogger(AlumnosDAO.class.getName()).log(Level.SEVERE, null, ex);
  189. } finally {
  190. db.cerrarConexion(con);
  191. }
  192. }
  193. public int cambiarPassUser(String codigo, String password) {
  194. DBConnection db = new DBConnection();
  195. Connection con = null;
  196. int filas = 0;
  197. try {
  198. con = db.getConnection();
  199. QueryRunner qr = new QueryRunner();
  200. filas = qr.update(con,
  201. "UPDATE LOGIN SET PASSWORD=? WHERE ACTIVACION=? "
  202. + "AND fecha_renovacion > date_sub(now(),INTERVAL 1 HOUR)",
  203. password, codigo);
  204. } catch (Exception ex) {
  205. Logger.getLogger(AlumnosDAO.class.getName()).log(Level.SEVERE, null, ex);
  206. } finally {
  207. db.cerrarConexion(con);
  208. }
  209. return filas;
  210. }
  211. public int activarUser(String activacion) {
  212. DBConnection db = new DBConnection();
  213. Connection con = null;
  214. int filas = 0;
  215. try {
  216. con = db.getConnection();
  217. QueryRunner qr = new QueryRunner();
  218. filas = qr.update(con,
  219. "UPDATE LOGIN SET ACTIVO=1 WHERE ACTIVACION=? "
  220. + "AND fecha_renovacion > date_sub(now(),INTERVAL 1 HOUR)",
  221. activacion);
  222. } catch (Exception ex) {
  223. Logger.getLogger(AlumnosDAO.class.getName()).log(Level.SEVERE, null, ex);
  224. } finally {
  225. db.cerrarConexion(con);
  226. }
  227. return filas;
  228. }
  229. public Alumno addUser(Alumno u, String activacion) {
  230. DBConnection db = new DBConnection();
  231. Connection con = null;
  232. try {
  233. con = db.getConnection();
  234. con.setAutoCommit(false);
  235. QueryRunner qr = new QueryRunner();
  236. BigInteger id = qr.insert(con,
  237. "INSERT INTO LOGIN (USER,PASSWORD,MAIL,ACTIVACION,ACTIVO,FECHA_RENOVACION) VALUES(?,?,?,?,?,now())",
  238. new ScalarHandler<BigInteger>(),
  239. "", "", "", activacion, 0);
  240. u.setId(id.longValue());
  241. con.commit();
  242. } catch (Exception ex) {
  243. Logger.getLogger(AlumnosDAO.class.getName()).log(Level.SEVERE, null, ex);
  244. } finally {
  245. db.cerrarConexion(con);
  246. }
  247. return u;
  248. }
  249. }