gpt4 book ai didi

java.sql.SQLException : No suitable driver found for jdbc:mysql://localhost:3306/tun_recrut

转载 作者:行者123 更新时间:2023-12-01 17:12:09 26 4
gpt4 key购买 nike

我是java新手,我正在尝试制作简单的身份验证接口(interface)。

我正在尝试使用 DriverManager 执行简单查询。您能帮我理解这里的问题是什么吗?

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;
}

}


}

在我的 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-x.x.x.jar

关于java.sql.SQLException : No suitable driver found for jdbc:mysql://localhost:3306/tun_recrut,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23260202/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com