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

标签 java jdbc sqlexception

我是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/

相关文章:

java - 没有这样的方法错误:

sql - 使用 jdbc 配置 SQL 连接 - vagrant

java - 在 Airflow 中设置 DB2

c# - RAISERROR―如何与SqlException区分?

java - 删除 xsi :type after marshalling abstract class with hierarchy

java - 在需要很长时间的函数之前和之后显示来自 ActionListener 的消息

java - Java获取ArrayList中的第一个和最后一个元素

java - 在 ResultSet 中搜索特定值方法?

Java 与 MongoDB 连接问题 - 出现 SQL 异常

java - Spring JdbcTemplate 可以等待 oracle 存储过程完成多长时间