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;
}
}
您可能在类路径中缺少mysql-connector-java-xxxjar
If you want use 'MySql' with Java you need to download 'JConnector' from MySql`s website. After that you need to set the 'Classpath'. You can use the following Commands
set CLASSPATH="PathtOfMySqlConnector.jar"
or you can set it likr you set the bin path of Java. Hope it helps.
download mysql-connector from here and add it to your application class path
probably you may find this this tutorial
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.