Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.curso.modelo.persistencia;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import com.curso.modelo.entidad.Usuario;
- import com.curso.modelo.persistencia.util.JDBCUtil;
- public class UsuarioDaoJdbcMyslImplementation {
- //Tablas de usuarios
- //---------------
- //id
- //INSERT INTO TABLA (COLUMNAS) VALUES (VALORES)
- //UPDATE FROM TABLA SET COLUMNA = VALOR WHERE CONDICION
- //DELETE FROM TABLA WHERE
- //SELECT COLUMNAS FROM TABLA WHERE TABLA
- private void insertar(Usuario usuario) {
- Connection conex = null;
- try {
- conex = JDBCUtil.getConnection();
- //conex = DriverManager.getConnection("jdbc:mysql://localhost:3306/gestflot3000", "root", "rootroot");
- PreparedStatement pst = conex.prepareStatement(" INSERT INTO USUARIO (NOMBRE, CORREOE_E, PW, DIRECCION, TELEFONO) VALUES (?,?,?,?,?)");
- //PreparedStatement pst = conex.prepareStatement(" INSERT INTO COCHE (MARCA,MODELO,MATRICULA) VALUES ("+ coche.getMarca()+"' , '"+ coche.getModelo()+ "' ,' "+ coche.getMatricula()+"')");
- pst.setString(1, usuario.getNombre());
- pst.setString(2, usuario.getCorreoE());
- pst.setString(3, usuario.getPw());
- pst.setString(4, usuario.getDireccion());
- pst.setString(5, usuario.getTelefono());
- //pst.executeQuery(); PARA OBTENER INFORMACION
- pst.executeUpdate(); //Cuando es update es para insertar
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- // Obligatorio cerrar las conexiones cuando terminados de usarlas..
- conex.close();
- } catch (SQLException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public Usuario buscarPorCredenciales(String correoE, String pw) {
- Usuario user = null;
- Connection conex = null;
- try {
- conex = JDBCUtil.getConnection();
- PreparedStatement pst = conex.prepareStatement(" SELECT * FROM USUARIO WHERE CORREO_E =? AND PW=?");
- //PreparedStatement pst = conex.prepareStatement(" INSERT INTO COCHE (MARCA,MODELO,MATRICULA) VALUES ("+ coche.getMarca()+"' , '"+ coche.getModelo()+ "' ,' "+ coche.getMatricula()+"')");
- pst.setString(1, correoE );
- pst.setString(2, pw);
- //pst.executeQuery(); PARA OBTENER INFORMACION
- ResultSet rs = pst.executeQuery(); //Cuando es executeQuery() es para consultar
- // Cuando es update es para insertar
- // ResultSet es bidimensional para guardar las consultas..
- //si hay mas filas despues del incide y devuelve true..
- while ( rs.next() ) {
- user = new Usuario( rs.getInt ("ID"),
- rs.getString ("NOMBRE"),
- rs.getString ("CORREO_E"),
- rs.getString ("PW"),
- rs.getString ("DIRECCION"),
- rs.getString ("TELEFONO"));
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- // Obligatorio cerrar las conexiones cuando terminados de usarlas..
- conex.close();
- } catch (SQLException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return user;
- }
- public Usuario buscarPorCorreoE(String correoE) {
- Usuario user = null;
- Connection conex = null;
- try {
- conex = JDBCUtil.getConnection();
- PreparedStatement pst = conex.prepareStatement(" SELECT * FROM USUARIO WHERE CORREO_E =? ");
- //PreparedStatement pst = conex.prepareStatement(" INSERT INTO COCHE (MARCA,MODELO,MATRICULA) VALUES ("+ coche.getMarca()+"' , '"+ coche.getModelo()+ "' ,' "+ coche.getMatricula()+"')");
- pst.setString(1, correoE );
- //pst.executeQuery(); PARA OBTENER INFORMACION
- ResultSet rs = pst.executeQuery(); //Cuando es executeQuery() es para consultar
- // Cuando es update es para insertar
- // ResultSet es bidimensional para guardar las consultas..
- //si hay mas filas despues del incide y devuelve true..
- while ( rs.next() ) {
- user = new Usuario( rs.getInt ("ID"),
- rs.getString ("NOMBRE"),
- rs.getString ("CORREO_E"),
- rs.getString ("PW"),
- rs.getString ("DIRECCION"),
- rs.getString ("TELEFONO"));
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- // Obligatorio cerrar las conexiones cuando terminados de usarlas..
- conex.close();
- } catch (SQLException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return user;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement