java.sql.SQLException:没有为jdbc找到合适的驱动程序:mysql:// localhost:3306 / tun_recrut

时间:2014-04-24 04:59:44

标签: java jdbc sqlexception

我是java的新手,我正在尝试制作简单的身份验证界面。

我正在尝试使用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;
    }
}

3 个答案:

答案 0 :(得分:5)

您可能在类路径上缺少mysql-connector-java-x.x.x.jar

答案 1 :(得分:0)

如果你想使用' MySql'使用Java,您需要下载' JConnector'来自MySql的网站。之后,您需要设置' Classpath'。您可以使用以下命令

    set CLASSPATH="PathtOfMySqlConnector.jar"

或者您可以设置它设置Java的bin路径。 希望它有所帮助。

答案 2 :(得分:0)

here下载mysql-connector并将其添加到您的应用程序类路径
可能你会发现这个this tutorial