java.sql.SQLException:找不到适合 jdbc 的驱动程序:mysql://localhost:3306/dbname [重复]

Posted

技术标签:

【中文标题】java.sql.SQLException:找不到适合 jdbc 的驱动程序:mysql://localhost:3306/dbname [重复]【英文标题】:java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/dbname [duplicate] 【发布时间】:2014-04-18 13:07:09 【问题描述】:

我有这个 Java 程序:mysqlConnectExample.java

import java.sql.*;
import java.util.Properties;

public class MySQLConnectExample 
    public static void main(String[] args) 
        Connection conn1 = null;
        Connection conn2 = null;
        Connection conn3 = null;

        try 
            String url1 = "jdbc:mysql://localhost:3306/aavikme";
            String user = "root";
            String password = "aa";

            conn1 = DriverManager.getConnection(url1, user, password);
            if (conn1 != null)
                System.out.println("Connected to the database test1");

            String url2 = "jdbc:mysql://localhost:3306/aavikme?user=root&password=aa";
            conn2 = DriverManager.getConnection(url2);
            if (conn2 != null) 
                System.out.println("Connected to the database test2");
            

            String url3 = "jdbc:mysql://localhost:3306/aavikme";
            Properties info = new Properties();
            info.put("user", "root");
            info.put("password", "aa");

            conn3 = DriverManager.getConnection(url3, info);
            if (conn3 != null) 
                System.out.println("Connected to the database test3");
            
         catch (SQLException ex) 
            System.out.println("An error occurred. Maybe user/password is invalid");
            ex.printStackTrace();
        
    

我是这样编译的:

E:\java mysql code driver>javac MySQLConnectExample.java

E:\java mysql code driver>java -cp mysql-connector-java-3.0.11-stable-bin.jar;.
MySQLConnectExample

我收到此错误:

An error occurred. Maybe user/password is invalid
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/
aavikme
        at java.sql.DriverManager.getConnection(DriverManager.java:596)
        at java.sql.DriverManager.getConnection(DriverManager.java:215)
        at MySQLConnectExample.main(MySQLConnectExample.java:20)

我做错了什么?

【问题讨论】:

你的电脑上运行了mysql吗?复制数据文件夹将无济于事,必须运行 mysql 服务器。 是的,我的系统上正在运行 mysql 相关问题:***.com/q/5556664/632951 【参考方案1】:

确保你先运行这个:

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

这会强制驱动程序自行注册,以便 Java 知道如何处理这些数据库连接字符串。

有关详细信息,请参阅MySQL Connector reference。

【讨论】:

它的旧代码,但它很可能会解决这个问题,因为这是一个非常旧的驱动程序。下载更新的驱动程序版本也可能会在不更改代码的情况下解决问题。 编译时错误类未找到异常!!! 编译 E:\java mysql 代码驱动程序时出现错误>javac MySQLConnectExample.java MySQLConnectExample.java:16: 错误:未报告的异常 ClassNotFoundException;必须被捕获或声明被抛出 Class.forName("com.mysql.jdbc.Driver"); ^ 1 个错误 只是一个建议。命令行上的 -cp 参数必须指向驱动程序 jar 的完整路径。像 -cp c:\jars\driverjar.jar;. 尽管该行不是必需的,但它对了解驱动程序是否正确加载非常有帮助。【参考方案2】:

你必须加载jdbc driver。考虑下面的代码。

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

            // connect way #1
            String url1 = "jdbc:mysql://localhost:3306/aavikme";
            String user = "root";
            String password = "aa";

            conn1 = DriverManager.getConnection(url1, user, password);
            if (conn1 != null) 
                System.out.println("Connected to the database test1");
            

            // connect way #2
            String url2 = "jdbc:mysql://localhost:3306/aavikme?user=root&password=aa";
            conn2 = DriverManager.getConnection(url2);
            if (conn2 != null) 
                System.out.println("Connected to the database test2");
            

            // connect way #3
            String url3 = "jdbc:mysql://localhost:3306/aavikme";
            Properties info = new Properties();
            info.put("user", "root");
            info.put("password", "aa");

            conn3 = DriverManager.getConnection(url3, info);
            if (conn3 != null) 
                System.out.println("Connected to the database test3");
            
    catch (SQLException ex) 
            System.out.println("An error occurred. Maybe user/password is invalid");
            ex.printStackTrace();
    catch (ClassNotFoundException e) 
    // TODO Auto-generated catch block
    e.printStackTrace();
   

【讨论】:

编译时错误类未找到异常!!! 查看我修改后的答案。 你修改了什么我没有看到代码导入有任何变化 编译 E:\java mysql 代码驱动程序时出现错误>javac MySQLConnectExample.java MySQLConnectExample.java:16: 错误:未报告的异常 ClassNotFoundException;必须被捕获或声明被抛出 Class.forName("com.mysql.jdbc.Driver"); ^ 1 个错误 上线Class.forName("com.mysql.jdbc.Driver");:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver【参考方案3】:

我遇到了同样的问题,我的代码如下:

private Connection conn = DriverManager.getConnection(Constant.MYSQL_URL, Constant.MYSQL_USER, Constant.MYSQL_PASSWORD);
private Statement stmt = conn.createStatement();

我还没有加载驱动类,但是在本地可以运行,可以从MySQL查询结果,但是部署到Tomcat上就不行了,出现如下错误:

No suitable driver found for jdbc:mysql://172.16.41.54:3306/eduCloud

所以当我看到发布的其他答案时,我加载了驱动程序类,如下所示:

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

现在可以了!我不知道为什么它在本地运行良好,我需要你的帮助,非常感谢!

【讨论】:

我在建立连接之前添加了Class.forName ... 行。【参考方案4】:

从具有列 column1、column2、column3、column4、cloumn1 和 2 的表中检索数据的示例保存 int 值,列 3 和 4 保存varchar(10)

import java.sql.*; 
// need to import this as the STEP 1. Has the classes that you mentioned  
public class JDBCexample 
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
    static final String DB_URL = "jdbc:mysql://LocalHost:3306/databaseNameHere"; 
    // DON'T PUT ANY SPACES IN BETWEEN and give the name of the database (case insensitive) 

    // database credentials
    static final String USER = "root";
    // usually when you install MySQL, it logs in as root 
    static final String PASS = "";
    // and the default password is blank

    public static void main(String[] args) 
        Connection conn = null;
        Statement stmt = null;

        try 
    // registering the driver__STEP 2
            Class.forName("com.mysql.jdbc.Driver"); 
    // returns a Class object of com.mysql.jdbc.Driver
    // (forName(""); initializes the class passed to it as String) i.e initializing the
    // "suitable" driver
            System.out.println("connecting to the database");
    // opening a connection__STEP 3
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
    // executing a query__STEP 4 
            System.out.println("creating a statement..");
            stmt = conn.createStatement();
    // creating an object to create statements in SQL
            String sql;
            sql = "SELECT column1, cloumn2, column3, column4 from jdbcTest;";
    // this is what you would have typed in CLI for MySQL
            ResultSet rs = stmt.executeQuery(sql);
    // executing the query__STEP 5 (and retrieving the results in an object of ResultSet)
    // extracting data from result set
            while(rs.next())
    // retrieve by column name
                int value1 = rs.getInt("column1");
                int value2 = rs.getInt("column2");
                String value3 = rs.getString("column3");
                String value4 = rs.getString("columnm4");
    // displaying values:
                System.out.println("column1 "+ value1);
                System.out.println("column2 "+ value2);
                System.out.println("column3 "+ value3);
                System.out.println("column4 "+ value4);

            
    // cleaning up__STEP 6
            rs.close();
            stmt.close();
            conn.close();
         catch (SQLException e) 
    //  handle sql exception
            e.printStackTrace();
        catch (Exception e) 
    // TODO: handle exception for class.forName
            e.printStackTrace();
        finally  
    //closing the resources..STEP 7
            try 
                if (stmt != null)
                    stmt.close();
             catch (SQLException e2) 
                e2.printStackTrace();
            try 
                if (conn != null) 
                    conn.close();
                
             catch (SQLException e2) 
                e2.printStackTrace();
            
        
        System.out.println("good bye");
    

【讨论】:

上线Class.forName("com.mysql.jdbc.Driver");:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver【参考方案5】:

您可能没有将MySQL connector/J jar 文件复制到 lib 文件夹中,然后该文件必须在类路径中。

如果您还没有这样做,请告诉我,我会详细说明答案

【讨论】:

【参考方案6】:

您的代码中缺少Class.forName("com.mysql.jdbc.Driver");

这是让一切正常工作所缺少的。

【讨论】:

【参考方案7】:

这里的所有答案都使用Class.forName("my.vandor.Driver"); 行来加载驱动程序。

作为一个(更好的)替代方案,您可以使用 DriverManager 辅助类,它为您提供了一些方法来处理您的 JDBC 驱动程序。

你可能想要

    使用DriverManager.registerDriver(driverObject); 将您的驱动程序注册到它的驱动程序列表中

向 DriverManager 注册给定的驱动程序。新加载的驱动程序类应该调用方法 registerDriver 以使 DriverManager 知道自己。如果驱动程序当前已注册,则不采取任何措施

    使用DriverManager.deregisterDriver(driverObject); 将其删除。

从 DriverManager 的已注册驱动程序列表中删除指定的驱动程序。

例子:

Driver driver = new oracle.jdbc.OracleDriver();
DriverManager.registerDriver(driver);
Connection conn = DriverManager.getConnection(url, user, password);
// ... 
// and when you don't need anything else from the driver
DriverManager.deregisterDriver(driver);

或者更好的是,使用DataSource

【讨论】:

【参考方案8】:

试试这个

String url = "jdbc:mysql://localhost:3306/<dbname>";
String user = "<username>";
String password = "<password>";
conn = DriverManager.getConnection(url, user, password); 

【讨论】:

【参考方案9】:

我也遇到过类似的问题,只要验证一下你的Mysql服务器运行的端口,就可以解决问题了

例如,我的代码是:

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:8080/bddventas","root","");

我把字符串改成

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bddventas","root","");

瞧!!,这很有效,因为我的服务器在那个端口上运行

希望有帮助

【讨论】:

mysql默认监听3306端口

以上是关于java.sql.SQLException:找不到适合 jdbc 的驱动程序:mysql://localhost:3306/dbname [重复]的主要内容,如果未能解决你的问题,请参考以下文章

H2 java.sql.SQLException:找不到合适的驱动程序

java.sql.SQLException:找不到适合 jdbc:microsoft:sqlserver 的驱动程序

java.sql.SQLException:找不到适合 jdbc 的驱动程序:mysql://localhost:3306/dbname [重复]

java.sql.SQLException:将 Spark 数据帧保存到 Sybase 时找不到类型“TIMESTAMP”

Spring Boot + Flyway + AWS:原因:java.sql.SQLException:找不到合适的驱动程序

java.sql.SQLException: 找不到适合 jdbc:sqlserver 的驱动程序... (intellij, maven)