Advertisement
Guest User

Untitled

a guest
Feb 12th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. package com.curso.modelo.persistencia;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8.  
  9. import com.curso.modelo.entidad.Usuario;
  10. import com.curso.modelo.persistencia.util.JDBCUtil;
  11.  
  12. public class UsuarioDaoJdbcMyslImplementation {
  13.  
  14. //Tablas de usuarios
  15. //---------------
  16. //id
  17.  
  18. //INSERT INTO TABLA (COLUMNAS) VALUES (VALORES)
  19. //UPDATE FROM TABLA SET COLUMNA = VALOR WHERE CONDICION
  20. //DELETE FROM TABLA WHERE
  21. //SELECT COLUMNAS FROM TABLA WHERE TABLA
  22.  
  23.  
  24. private void insertar(Usuario usuario) {
  25.  
  26. Connection conex = null;
  27.  
  28. try {
  29.  
  30. conex = JDBCUtil.getConnection();
  31. //conex = DriverManager.getConnection("jdbc:mysql://localhost:3306/gestflot3000", "root", "rootroot");
  32.  
  33. PreparedStatement pst = conex.prepareStatement(" INSERT INTO USUARIO (NOMBRE, CORREOE_E, PW, DIRECCION, TELEFONO) VALUES (?,?,?,?,?)");
  34.  
  35. //PreparedStatement pst = conex.prepareStatement(" INSERT INTO COCHE (MARCA,MODELO,MATRICULA) VALUES ("+ coche.getMarca()+"' , '"+ coche.getModelo()+ "' ,' "+ coche.getMatricula()+"')");
  36.  
  37. pst.setString(1, usuario.getNombre());
  38. pst.setString(2, usuario.getCorreoE());
  39. pst.setString(3, usuario.getPw());
  40. pst.setString(4, usuario.getDireccion());
  41. pst.setString(5, usuario.getTelefono());
  42.  
  43. //pst.executeQuery(); PARA OBTENER INFORMACION
  44. pst.executeUpdate(); //Cuando es update es para insertar
  45.  
  46.  
  47. } catch (SQLException e) {
  48. e.printStackTrace();
  49. } finally {
  50. try {
  51. // Obligatorio cerrar las conexiones cuando terminados de usarlas..
  52. conex.close();
  53. } catch (SQLException e)
  54. {
  55. // TODO Auto-generated catch block
  56. e.printStackTrace();
  57. }
  58. }
  59.  
  60.  
  61. }
  62.  
  63.  
  64.  
  65. public Usuario buscarPorCredenciales(String correoE, String pw) {
  66.  
  67. Usuario user = null;
  68. Connection conex = null;
  69. try {
  70. conex = JDBCUtil.getConnection();
  71.  
  72. PreparedStatement pst = conex.prepareStatement(" SELECT * FROM USUARIO WHERE CORREO_E =? AND PW=?");
  73. //PreparedStatement pst = conex.prepareStatement(" INSERT INTO COCHE (MARCA,MODELO,MATRICULA) VALUES ("+ coche.getMarca()+"' , '"+ coche.getModelo()+ "' ,' "+ coche.getMatricula()+"')");
  74.  
  75. pst.setString(1, correoE );
  76. pst.setString(2, pw);
  77.  
  78. //pst.executeQuery(); PARA OBTENER INFORMACION
  79. ResultSet rs = pst.executeQuery(); //Cuando es executeQuery() es para consultar
  80. // Cuando es update es para insertar
  81. // ResultSet es bidimensional para guardar las consultas..
  82.  
  83. //si hay mas filas despues del incide y devuelve true..
  84. while ( rs.next() ) {
  85.  
  86. user = new Usuario( rs.getInt ("ID"),
  87. rs.getString ("NOMBRE"),
  88. rs.getString ("CORREO_E"),
  89. rs.getString ("PW"),
  90. rs.getString ("DIRECCION"),
  91. rs.getString ("TELEFONO"));
  92.  
  93. }
  94.  
  95.  
  96. } catch (SQLException e) {
  97. e.printStackTrace();
  98. } finally {
  99. try {
  100. // Obligatorio cerrar las conexiones cuando terminados de usarlas..
  101. conex.close();
  102. } catch (SQLException e)
  103. {
  104. // TODO Auto-generated catch block
  105. e.printStackTrace();
  106. }
  107. }
  108.  
  109. return user;
  110.  
  111. }
  112.  
  113.  
  114. public Usuario buscarPorCorreoE(String correoE) {
  115.  
  116. Usuario user = null;
  117. Connection conex = null;
  118. try {
  119. conex = JDBCUtil.getConnection();
  120.  
  121. PreparedStatement pst = conex.prepareStatement(" SELECT * FROM USUARIO WHERE CORREO_E =? ");
  122. //PreparedStatement pst = conex.prepareStatement(" INSERT INTO COCHE (MARCA,MODELO,MATRICULA) VALUES ("+ coche.getMarca()+"' , '"+ coche.getModelo()+ "' ,' "+ coche.getMatricula()+"')");
  123.  
  124. pst.setString(1, correoE );
  125.  
  126. //pst.executeQuery(); PARA OBTENER INFORMACION
  127. ResultSet rs = pst.executeQuery(); //Cuando es executeQuery() es para consultar
  128. // Cuando es update es para insertar
  129. // ResultSet es bidimensional para guardar las consultas..
  130.  
  131. //si hay mas filas despues del incide y devuelve true..
  132. while ( rs.next() ) {
  133.  
  134. user = new Usuario( rs.getInt ("ID"),
  135. rs.getString ("NOMBRE"),
  136. rs.getString ("CORREO_E"),
  137. rs.getString ("PW"),
  138. rs.getString ("DIRECCION"),
  139. rs.getString ("TELEFONO"));
  140.  
  141. }
  142.  
  143.  
  144. } catch (SQLException e) {
  145. e.printStackTrace();
  146. } finally {
  147. try {
  148. // Obligatorio cerrar las conexiones cuando terminados de usarlas..
  149. conex.close();
  150. } catch (SQLException e)
  151.  
  152. {
  153. // TODO Auto-generated catch block
  154. e.printStackTrace();
  155. }
  156. }
  157.  
  158. return user;
  159.  
  160. }
  161.  
  162.  
  163.  
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement