I'm new in java and i'm trying to make simple Authentication interface.
I am trying to execute simple query using DriverManager . Can you please help me understand what is the issue here ?
1/ MyDBConnection.java
package com.esprit.tunRecrut.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MyDBConnection {
/**
* Déclaration des variables pour la connexion
*/
private String url = "jdbc:mysql://localhost:3306/tun_recrut";
private String login = "root";
private String pwd = "";
private static MyDBConnection instance;
public static Connection connection;
private MyDBConnection() {
try {
connection = DriverManager.getConnection(url,login,pwd);
} catch (SQLException ex) {
Logger.getLogger(MyDBConnection.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static Connection getConnection() {
return connection;
}
public static MyDBConnection getInstance() {
if(instance==null)
instance = new MyDBConnection();
return instance;
}
}
2/Crud.java
package com.esprit.tunRecrut.util;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Crud {
MyDBConnection mc = MyDBConnection.getInstance();
public boolean execute(String sql){
try {
Statement statement = mc.getConnection().createStatement();
statement.executeUpdate(sql);
return true;
} catch (SQLException ex) {
Logger.getLogger(Crud.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
}
public ResultSet exeRead(String sql){
try {
Statement statement = mc.getConnection().createStatement();
ResultSet rs;
rs = statement.executeQuery(sql);
return rs;
} catch (SQLException ex) {
Logger.getLogger(Crud.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
}
in my userDao.java
public User findUserByEmailAndPassword(String email, String password) {
User user=null;
try {
String sql = "SELECT * FROM user WHERE email_address = '" + email + "' AND password = '" + password + "'";
ResultSet rs = crud.exeRead(sql);
while (rs.next()) {
user = new User(rs.getInt("id"), rs.getString("type"), rs.getString("email_address"));
}
return user;
} catch (SQLException ex) {
Logger.getLogger("Client controller").log(Level.SEVERE, " fail");
// Logger.getLogger("Client DAO").log(Level.SEVERE, null, ex);
return null;
}
}
You are probably missing mysql-connector-java-x.x.x.jar on the class path