java 没有找到适合 jdbc 的驱动程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17432813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:58:00  来源:igfitidea点击:

No suitable driver found for jdbc?

javajdbc

提问by david blaine

I took this example from Oracle's website and I am not able to implement it. It keeps on showing an error. I have already added the mysql 5.1 driver jar to my projects lib folder and also to my build path. How do I fix my code ?

我从 Oracle 的网站上获取了这个示例,但我无法实现它。它一直显示错误。我已经将 mysql 5.1 驱动程序 jar 添加到我的项目 lib 文件夹以及我的构建路径中。如何修复我的代码?

link - http://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html

链接 - http://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html

error -

错误 -

java.sql.SQLException: No suitable driver found for jdbc:JavaEE://localHost:3306/
    at java.sql.DriverManager.getConnection(DriverManager.java:602)
    at java.sql.DriverManager.getConnection(DriverManager.java:154)
    at com.beans.us.dao.Data.getConnection(Data.java:25)
    at com.beans.us.dao.Data.main(Data.java:43)
Cannot connect to database

code -

代码 -

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class Data {

    public static Connection getConnection() {

        Connection conn = null;
        Properties connectionProps = new Properties();
        String userName = "root";
        String password = "root";
        String dbms = "JavaEE";
        String serverName = "localHost";
        String portNumber = "3306";

        connectionProps.put("user", userName);
        connectionProps.put("password", password);

        try {

            conn = DriverManager.getConnection("jdbc:" + dbms + "://"
                    + serverName + ":" + portNumber + "/", connectionProps);
        } catch (SQLException sQLException) {
            sQLException.printStackTrace();
        }

        if (conn != null) {
            System.out.println("Connected to database");
        } else {
            System.out.println("Cannot connect to database");
        }

        return conn;
    }

    public static void main(String[] args) {

        getConnection();

    }

}

回答by jlordo

Your connection string is:

您的连接字符串是:

"jdbc:JavaEE://..."

but with MySQL it should be

但是对于 MySQL,它应该是

"jdbc:mysql://..."

Also, your forgot to load the driver:

另外,您忘记加载驱动程序:

Class.forName("com.mysql.jdbc.Driver");

It needs to be done before calling

需要在调用之前完成

conn = DriverManager.getConnection(...);

回答by NINCOMPOOP

It seems you have forgotten to load the driver .

看来您忘记加载驱动程序了。

Class.forName("com.mysql.jdbc.Driver");

The Class.forName()causes the ClassLoader to load the class into memory. JDBC driver classes contain a static initializer block that registers the driver with DriverManagerfor later reference.

Class.forName()会导致 ClassLoader 将类加载到内存中。JDBC 驱动程序类包含一个静态初始化程序块,用于注册驱动程序以DriverManager供以后参考。

Didn't notice this earlier , your connection string should be "jdbc:mysql://localhost:3306/dbname"

之前没有注意到这一点,您的连接字符串应该是 "jdbc:mysql://localhost:3306/dbname"

回答by David Hofmann

Your jdbc string is not properly formatted, take a look at the answers of this question:

你的jdbc字符串格式不正确,看看这个问题的答案:

What is the MySQL JDBC driver connection string?

什么是 MySQL JDBC 驱动程序连接字符串?