I'm trying to call a mySQL stored procedure from my Java application. When I call the stored procedure from mySQL workbench, it works and I get the correct number of rows depending of the parameters I send. The problem comes when I try to call it from Java, I don't get any result and I can't find out why. I've been following the oracle documentation.
The stored procedure:
CREATE DEFINER=`root`@`localhost` PROCEDURE `comprobarUsuario`(
IN usu varchar(20),
IN pass varchar(20),
OUT idusuarios int)
BEGIN
SELECT idusuarios
FROM usuarios
WHERE usuarios.nombreUsuario = usu and usuarios.contraseña = pass;
END
The Java Class where I'm trying to call the stored procedure:
public class ConectorSQL {
public static final String URL = "jdbc:mysql://localhost:3306/ProyectoBD?autoReconnect=true&useSSL=false";
public static final String USERNAME = "root";
public static final String PASSWORD = "1627Admin";
public static Connection getConnection() {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = (Connection) DriverManager.getConnection(URL, USERNAME, PASSWORD);
//System.out.println("Conexion exitosa");
} catch (Exception e) {
System.out.println("Error de conexión con la base de datos");
}
return con;
}
public void mostrarDatos() {
try {
Connection con = null;
con = getConnection();
CallableStatement cs;
cs = con.prepareCall("{CALL comprobarUsuario(?,?,?)}");
cs.setString(1,"Jorge" );
cs.setString(2, "1627Jorge");
cs.registerOutParameter(3, Type.INT);
ResultSet rs2 = cs.executeQuery();
if(rs2.next()){
System.out.println(true);
}
int resultado = cs.getInt("idusuarios");
System.out.println(resultado);
con.close();
} catch (Exception e) {
}
}
}