I am learning programming and I am struggling with a problem in my web application. I have made the same application using SQLServer and I have not had problems but changing the connection string to make it work with MySql stops working.
I am trying to validate a login using a user query and password in the database through a Select statement. The database created in Mysql is identical to the one I made in SQLServer (in fact I have tried the query and there are no problems).
The only error that I launched was with the connector that sent me this message: "Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class is
com.mysql.cj.jdbc.Driver '" and that was solved by adding that "cj". The rest is completely the same.
I leave my classes implemented and I thank you in advance for any help you can give me. by the way I use Netbeans IDE 8.2, Mysql 8.0 and this connector "mysql-connector-java-8.0.12.jar"
connection class:
package conexion;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Conexion {
private static Conexion instancia=null;
private static Connection con=null;
private static final String url="jdbc:mysql://localhost:3306/mantenimiento?useTimezone=true&serverTimezone=UTC";
private static final String driver="com.mysql.cj.jdbc.Driver";
private static final String usuario="sa";
private static final String clave="123456";
public Conexion() {
try {
Class.forName(driver);
con=DriverManager.getConnection(url, usuario, clave);
System.out.println("Conectado");
} catch (ClassNotFoundException | SQLException e) {
System.out.println(e.getMessage());
}
}
public synchronized static Conexion conectar(){
if(instancia==null){
instancia = new Conexion();
}
return instancia;
}
public Connection getCon() {
return con;
}
public void cerraConexion(){
instancia=null;
}
User class:
package dtos;
public class Usuario {
private String correo;
private String clave;
public Usuario(String correo, String clave) {
this.correo = correo;
this.clave = clave;
}
public String getCorreo() {
return correo;
}
public void setCorreo(String correo) {
this.correo = correo;
}
public String getClave() {
return clave;
}
public void setClave(String clave) {
this.clave = clave;
}
}
LoginDAO class;
package daos;
import conexion.Conexion;
import dtos.Usuario;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class LoginDAO {
private static final String sql_select="select * from usuario where correo=? and clave=?";
private static PreparedStatement pstm=null;
private static ResultSet res = null;
private static Conexion con = Conexion.conectar();
public boolean validarLogin(Usuario usuario){
boolean resultado=false;
try {
pstm = con.getCon().prepareStatement(sql_select);
pstm.setString(1,usuario.getCorreo());
pstm.setString(2,usuario.getClave());
res =pstm.executeQuery();
if(res.next()){
resultado=true;
}
} catch (Exception e) {
System.out.println("Error"+e.getMessage());
}
finally{
try {
if(res!=null)res.close();
if(pstm!=null)pstm.close();
if(con!=null)con.cerraConexion();
} catch (Exception e) {
System.out.println("Error :"+e.getMessage());
}
}
return resultado;
}
}
Servlet controller:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String correo = request.getParameter("txtCorreo");
String clave = request.getParameter("txtClave");
Usuario usuario = new Usuario(correo, clave);
LoginDAO login = new LoginDAO();
if (login.validarLogin(usuario)) {
request.getSession().setAttribute("correo",usuario.getCorreo());
request.getRequestDispatcher("sistema.jsp").forward(request, response);
} else {
String error = "Usuario y/o contraseña incorrecto";
request.getSession().setAttribute("error",error);
request.getRequestDispatcher("error.jsp").forward(request, response);
}
}