From be8ece40d66868f875e650432f5b5a6957f16c03 Mon Sep 17 00:00:00 2001 From: NataliaCancinoV Date: Mon, 29 Apr 2024 00:36:41 -0600 Subject: [PATCH] Commit DAO y conexion --- backend/src/main/java/mx/uv/Conexion.java | 26 ++ backend/src/main/java/mx/uv/DAO.java | 320 ++++++++++++++++++++ backend/src/main/java/mx/uv/Usuario.java | 2 +- backend/target/classes/mx/uv/Conexion.class | Bin 0 -> 1531 bytes backend/target/classes/mx/uv/DAO.class | Bin 0 -> 9710 bytes backend/target/classes/mx/uv/Usuario.class | Bin 0 -> 1612 bytes 6 files changed, 347 insertions(+), 1 deletion(-) create mode 100644 backend/src/main/java/mx/uv/Conexion.java create mode 100644 backend/src/main/java/mx/uv/DAO.java create mode 100644 backend/target/classes/mx/uv/Conexion.class create mode 100644 backend/target/classes/mx/uv/DAO.class create mode 100644 backend/target/classes/mx/uv/Usuario.class diff --git a/backend/src/main/java/mx/uv/Conexion.java b/backend/src/main/java/mx/uv/Conexion.java new file mode 100644 index 0000000..d465c86 --- /dev/null +++ b/backend/src/main/java/mx/uv/Conexion.java @@ -0,0 +1,26 @@ +package mx.uv; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class Conexion { + private static String url = "jdbc:mysql://127.0.0.1:3306/?user=pastel"; + private static String driverName = "com.mysql.cj.jdbc.Driver"; + private static String username = "pastel"; + private static String password = "pastel"; + // variable de conexion + private static Connection connection = null; + + public static Connection getConnection(){ + try { + Class.forName(driverName); + connection = DriverManager.getConnection(url, username, password); + } catch (SQLException e) { + System.out.println(" SQL:" + e); + } catch (ClassNotFoundException e){ + System.out.println("Driver:" + e); + } + return connection; + } +} \ No newline at end of file diff --git a/backend/src/main/java/mx/uv/DAO.java b/backend/src/main/java/mx/uv/DAO.java new file mode 100644 index 0000000..aee3d70 --- /dev/null +++ b/backend/src/main/java/mx/uv/DAO.java @@ -0,0 +1,320 @@ +package mx.uv; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +// Data Access Object +public class DAO { + // en el dao se establece la conexion a la BD + private static Conexion c = new Conexion(); + // este metodo regresa un conjunto de usuarios que cumpla un criterio + public static List dameUsuarios() { + Statement stm = null; + ResultSet rs = null; + Connection conn = null; + + List resultado = new ArrayList<>(); + + conn = Conexion.getConnection(); + + try { + String sql = "SELECT * from usuarios"; + stm = (Statement) conn.createStatement(); + rs = stm.executeQuery(sql); + while (rs.next()) { + Usuario u = new Usuario(rs.getString("id"), rs.getString("correo"), rs.getString("password"), rs.getString("nombre")); + resultado.add(u); + } + } catch (Exception e) { + System.out.println(e); + } finally { + if (rs != null) + try { + rs.close(); + } catch (SQLException e) { + System.out.println(e); + } + rs = null; + if (stm != null) { + try { + stm.close(); + } catch (Exception e) { + System.out.println(e); + } + stm = null; + } + try { + conn.close(); + } catch (Exception e) { + System.out.println(e); + } + } + + return resultado; + } + + public static String crearUsuario(Usuario u) { + PreparedStatement stm = null; + Connection conn = null; + String msj = ""; + + conn = Conexion.getConnection(); + try { + String sql = "INSERT INTO usuarios (id, correo, password,nombre) values (?,?,?,?)"; + stm = (PreparedStatement) conn.prepareStatement(sql); + stm.setString(1, u.getId()); + stm.setString(2, u.getCorreo()); + stm.setString(3, u.getPassword()); + stm.setString(4, u.getNombre()); + if (stm.executeUpdate() > 0) + msj = "usuario agregado"; + else + msj = "usuario no agregado"; + + } catch (Exception e) { + System.out.println(e); + } finally { + if (stm != null) { + try { + stm.close(); + } catch (Exception e) { + System.out.println(e); + } + stm = null; + } + try { + conn.close(); + } catch (Exception e) { + System.out.println(e); + } + } + return msj; + } + + public static boolean validarUsuario(String correo, String password) { + PreparedStatement stm = null; + Connection conn = null; + + conn = Conexion.getConnection(); + try { + String sql = "SELECT * FROM usuarios WHERE correo = ? AND password = ?"; + stm = conn.prepareStatement(sql); + stm.setString(1, correo); + stm.setString(2, password); + + ResultSet rs = stm.executeQuery(); + + return rs.next(); // Devuelve true si hay una coincidencia, false si no hay coincidencia + + } catch (Exception e) { + System.out.println(e); + return false; // Manejar adecuadamente las excepciones en tu aplicación real + } finally { + // Cerrar recursos + if (stm != null) { + try { + stm.close(); + } catch (Exception e) { + System.out.println(e); + } + } + try { + conn.close(); + } catch (Exception e) { + System.out.println(e); + } + } + } + + public static boolean correoExistente(String correo) { + PreparedStatement stm = null; + Connection conn = null; + + conn = Conexion.getConnection(); + try { + String sql = "SELECT * FROM usuarios WHERE correo = ?"; + stm = conn.prepareStatement(sql); + stm.setString(1, correo); + + ResultSet rs = stm.executeQuery(); + + return rs.next(); // Devuelve true si hay una coincidencia (correo existente), false si no hay coincidencia + + } catch (Exception e) { + System.out.println(e); + return false; // Manejar adecuadamente las excepciones en tu aplicación real + } finally { + // Cerrar recursos + if (stm != null) { + try { + stm.close(); + } catch (Exception e) { + System.out.println(e); + } + } + try { + conn.close(); + } catch (Exception e) { + System.out.println(e); + } + } + } + + public static boolean existeUsuarioPorCorreo(String correo) { + PreparedStatement stm = null; + Connection conn = null; + + conn = Conexion.getConnection(); + try { + String sql = "SELECT COUNT(*) as num_usuarios FROM usuarios WHERE correo = ?"; + stm = conn.prepareStatement(sql); + stm.setString(1, correo); + + ResultSet rs = stm.executeQuery(); + + if (rs.next()) { + return rs.getInt("num_usuarios") > 0; // Devuelve true si existe al menos un usuario con ese correo + } else { + return false; // Devuelve false si no hay usuarios con ese correo + } + + } catch (Exception e) { + System.out.println(e); + return false; // Manejar adecuadamente las excepciones en tu aplicación real + } finally { + // Cerrar recursos + if (stm != null) { + try { + stm.close(); + } catch (Exception e) { + System.out.println(e); + } + } + try { + conn.close(); + } catch (Exception e) { + System.out.println(e); + } + } + } + + public static String cambiarContrasena(String correo, String nuevaContrasena) { + String mensaje = ""; + + try (Connection conn = Conexion.getConnection(); + PreparedStatement stm = conn.prepareStatement("UPDATE usuarios SET password = ? WHERE correo = ?")) { + + // Verificar si el usuario existe + if (existeUsuarioPorCorreo(correo)) { + stm.setString(1, nuevaContrasena); + stm.setString(2, correo); + + if (stm.executeUpdate() > 0) { + mensaje = "Contraseña actualizada correctamente"; + } else { + mensaje = "No se pudo actualizar la contraseña"; + } + } else { + mensaje = "Usuario no encontrado"; + } + + } catch (SQLException e) { + // Manejo de excepciones (puedes personalizar este manejo según tus necesidades) + mensaje = "Error al actualizar la contraseña"; + e.printStackTrace(); // O registra la excepción en un sistema de registro + } + + return mensaje; + } + + public static String obtenerIdUsuario(String correo, String password) { + PreparedStatement stm = null; + Connection conn = null; + + conn = Conexion.getConnection(); + try { + String sql = "SELECT id FROM usuarios WHERE correo = ? AND password = ?"; + stm = conn.prepareStatement(sql); + stm.setString(1, correo); + stm.setString(2, password); + + ResultSet rs = stm.executeQuery(); + + if (rs.next()) { + return rs.getString("id"); // Devuelve el ID si hay una coincidencia + } else { + return "falso"; // Devuelve -1 si no hay coincidencia + } + + } catch (Exception e) { + System.out.println(e); + return "falso"; // Manejar adecuadamente las excepciones en tu aplicación real + } finally { + // Cerrar recursos + if (stm != null) { + try { + stm.close(); + } catch (Exception e) { + System.out.println(e); + } + } + try { + conn.close(); + } catch (Exception e) { + System.out.println(e); + } + } + } + + public static Usuario obtenerDatosUsuario(String id) { + PreparedStatement stm = null; + Connection conn = null; + + conn = Conexion.getConnection(); + try { + String sql = "SELECT * FROM usuarios WHERE id = ?"; + stm = conn.prepareStatement(sql); + stm.setString(1, id); + + ResultSet rs = stm.executeQuery(); + + if (rs.next()) { + // Crear un objeto Usuario con los datos del usuario + Usuario usuario = new Usuario(); + usuario.setId(rs.getString("id")); + usuario.setCorreo(rs.getString("correo")); + usuario.setPassword(rs.getString("password")); + usuario.setNombre(rs.getString("nombre")); + // Agregar más campos según tu estructura de base de datos + + return usuario; + } else { + return null; // Devuelve null si no hay coincidencia + } + + } catch (Exception e) { + System.out.println(e); + return null; // Manejar adecuadamente las excepciones en tu aplicación real + } finally { + // Cerrar recursos + if (stm != null) { + try { + stm.close(); + } catch (Exception e) { + System.out.println(e); + } + } + try { + conn.close(); + } catch (Exception e) { + System.out.println(e); + } + } + } + +} \ No newline at end of file diff --git a/backend/src/main/java/mx/uv/Usuario.java b/backend/src/main/java/mx/uv/Usuario.java index 3d9d740..0f56621 100644 --- a/backend/src/main/java/mx/uv/Usuario.java +++ b/backend/src/main/java/mx/uv/Usuario.java @@ -1,4 +1,4 @@ -package main.java.mx.uv; +package mx.uv; public class Usuario { String id; diff --git a/backend/target/classes/mx/uv/Conexion.class b/backend/target/classes/mx/uv/Conexion.class new file mode 100644 index 0000000000000000000000000000000000000000..90fa0575d1bcea3bba1174512803424472ff6a51 GIT binary patch literal 1531 zcmZuxYg5}s6g>;rGKvVnfO+Ia)6yE6SPyt**q{;0^d1w17E{{ zz=eJ5z%o6{-!s>D_Z>SD=x>IeK)Te?RHD%J_lg2~HFOW0ux!;FfmE~MgnqoOTaCuw zL0F}U9r!+Rc!9up4AOY#$y?iE;-r>s&-LBtK5xs;Z3_&n1XU-63j*2w>aJa=y{AvX zG#78)%`Na*EadYGcgzR!%6q&la=bw>NCOEZQy3GNw1Zksx#jHroCL`|R>4ZUi~&vB zO$sDC-qQwBNJ{-B1B3DIiUA$x1cpnl@06RhT_@bKc0Hz>E(NybZCjx$&uw9HS*4+) zA3qxS0YgkBdgC@oY^j@VW<9j$L>(FgCbDxq`Oz_lUp34#icSqyJW_1nI&KJPuY-0C zX0vC@)CN^GRbE9MCKfd;kO&1R>k*T)X8G2h6B_s#HwD&upq<4(E9sraAa3D~hTC0D zR(a3b)ePLlZ>)XLWWTkW#0|_1wpqkxbF5lXLxBu+@{fD)QPcIRPME^7Kw_q{St{sQ z!hK2o10wZ^e{Ne^{BGb8R#<(jUUz)9>5bE-apxCL6W4Ve30%Sx1FLw--i?A-DKPfE zfQsu_O55jnso{mdsEWQjF9RhRsaBV%dVUIJ9^zUQG;A=3lL|4rCx^CEm!qWN4}qBu z8>+75AX*KYe)YskK(J9KI!$#Nf(>}>ROxyn$S$o!mi>0ks<&HQuLRA|c2-?E1Vi6H z9yu8cS!WUXg-LK|IEdWoa?sSu(NZf1ORXF$uKgT214HyX&(m|R@;8+pKEgGzC%$T{`wz#a%8~( literal 0 HcmV?d00001 diff --git a/backend/target/classes/mx/uv/DAO.class b/backend/target/classes/mx/uv/DAO.class new file mode 100644 index 0000000000000000000000000000000000000000..648d8fc3cf60243d321c5f5e21f9ac1568b4c3bc GIT binary patch literal 9710 zcmchd4SbZvoyUKZY_gkV5e?qsJ;p6D~I9OWAIuW4$H)uvjO#1eJUCCxFT!kTzPw0U7P(PY1!!hB_`A7iv% z+>|sb@Z7;`JP9{OTVe~6DeU7^7LF>eab}TfZ)z^DX-c+ZlQ+LidtymbNW754RFKI6C zuBZ&@M596Tt81#O>LMkPixTmcNXm1%K*Kd6sFTt|_G}u~NR48h8WOQ+du)C?e_CR# z?98Cdq5Sg70+pyVsL?EiGXdM1UmI4L#;|6w6|sg?d+h8~EV0sP)F^k!oQhSHqgCv$2Y&upQ`n*c30H%YN8pi$z;1^EF=!f~reHrDt_m3w4fBu+6q; z293RX#;nF`QCR0$M&>j$$CELOnMD?ci-P)xbl=W4RIg}=wOMEfbs_zABkJrLzY3R= z?JY(}yDs+t9OTPNK+B9e9~IOE8sLsU*`C0+yiGXJi#Eos2=&|osk+qiv3m1;y58 zsI1F|pqBSo^ty`^@k?DX3|rbKJg!;sI%G~BO*1DS(kdf`bh%O0%-Z?Y^XeiqYwPCt zauXTV)L0sEDODOt)2Y;DQgLK?v^f<^Mn;`p>in;U7;)Sye$!HizYHv=;TcB zAFds9-3E=m0s-N2eIu-^bq(l45N5)lqtjBBIgbKet8WE$ozV~r*FHD*9zWG@Tf%e>DE=^mp~{F7(;yg9SGp>WQO>Uq^379yucPLE8jo#sQr7FZYm z*`6Br9dh7gvbetsrs@?aBS(`ZSuf5NO>-* zh(wz|d)$02*50u8*~6*7bC5X#T9=46fUEwkso3&pda8EWgUl-#T}IuJ@g<-ymYCV- zc^%RZj3#+NX=?leNE`R<^=eqJ>2=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