Skip to main content
se añadieron 5 caracteres en el cuerpo
Origen Enlace
Carlos
  • 302
  • 2
  • 14
public class Conexion {
    
    String url = "jdbc:mysql://rootlocalhost:3306/mydatabase";
    String user = "root";
    String pass = "";
    Connection con;
    
    public Conexion(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(url, user, pass);
        }catch(Exception e){
            System.out.println("Error is ->  " + e.getMessage());
        }
    }
    
    public Connection getConnection(){
        return con;
    }
    
}
public class Conexion {
    
    String url = "jdbc:mysql://root:3306/mydatabase";
    String user = "root";
    String pass = "";
    Connection con;
    
    public Conexion(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(url, user, pass);
        }catch(Exception e){
            System.out.println("Error is ->  " + e.getMessage());
        }
    }
    
    public Connection getConnection(){
        return con;
    }
    
}
public class Conexion {
    
    String url = "jdbc:mysql://localhost:3306/mydatabase";
    String user = "root";
    String pass = "";
    Connection con;
    
    public Conexion(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(url, user, pass);
        }catch(Exception e){
            System.out.println("Error is ->  " + e.getMessage());
        }
    }
    
    public Connection getConnection(){
        return con;
    }
    
}
Origen Enlace
Carlos
  • 302
  • 2
  • 14

Yo te puedo compartir un ejemplo funcionando, espero que te pueda ayudar. // into package Config

public class Conexion {
    
    String url = "jdbc:mysql://root:3306/mydatabase";
    String user = "root";
    String pass = "";
    Connection con;
    
    public Conexion(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(url, user, pass);
        }catch(Exception e){
            System.out.println("Error is ->  " + e.getMessage());
        }
    }
    
    public Connection getConnection(){
        return con;
    }
    
}

//package modeloDAO

public class UsuarioDAO implements UsuarioCRUD {
    Conexion bdC = new Conexion();
    Connection con;
    PreparedStatement ps;
    ResultSet rs;
    
    UsuarioM userM = new UsuarioM();

public boolean create(UsuarioM user) {
        LocalDateTime hoy = LocalDateTime.now();        
         String sql = "INSERT INTO usuarios (email, password, tipo, created) VALUES ('"+user.getEmail()+"', '"+user.getPassword()+"', '"+user.getTipo()+"', '"+hoy+"' )";
        try{
           con = bdC.getConnection();
            ps = con.prepareStatement(sql);
            ps.executeUpdate(sql);
            
        }catch(Exception e){
            return false;
        }
        return true;
    }
}

//serverlet do post

String email = request.getParameter("email");
        String password = request.getParameter("password");
        int tipo = Integer.parseInt(  request.getParameter("tipo"));
        
        UsuarioDAO  query = new UsuarioDAO();
        UsuarioM user = new UsuarioM();
        
        user.setEmail(email);
        user.setPassword(password);
        user.setTipo(tipo);
        
        boolean estado = query.create(user);
        
        sesion = request.getSession();
        
        if(estado){
           sesion.setAttribute("bien", "se guardao correctamente");
           response.sendRedirect("Usuarios");
        }else{
            sesion.setAttribute("error", "No se pudo guardar");
           response.sendRedirect("UsuarioNew");
        }