java 找不到适合 jdbc:mysql//localhost:3306/test 的驱动程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15180657/
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-10-31 18:44:01  来源:igfitidea点击:

No suitable driver found for jdbc:mysql//localhost:3306/test

javamysql

提问by S.P

I am trying to write a java program connecting to a MySQL database. But I am getting this error:

我正在尝试编写一个连接到 MySQL 数据库的 java 程序。但我收到此错误:

No suitable driver found for jdbc:mysql//localhost:3306/test

找不到适合 jdbc:mysql//localhost:3306/test 的驱动程序

I have already installed mysql-connector-java.5.1.23-bin.jar. I am still getting this error.

我已经安装了mysql-connector-java.5.1.23-bin.jar。我仍然收到此错误。

Here is my program:

这是我的程序:

package database_practice;

import java.sql.*;

public class CreateConnection {
        private Connection conn = null;

        public static void main(String args[]) {
            CreateConnection conn = new CreateConnection();
                conn.getConnection();
        }


        public Connection getConnection() {
            try {
                String url = "jdbc:mysql//localhost:3306/test";
                String username = "root";
                String password = "admin1234";

            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection(url, username, password);
            System.out.println("Database Created...!!!");
        }

        catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error occured while connecting to database");
        }
            return conn;
    }
}

回答by Jon Skeet

Your connection string is wrong. This:

您的连接字符串错误。这:

"jdbc:mysql//localhost:3306/test"

should be:

应该:

"jdbc:mysql://localhost:3306/test"

Note the colon after "mysql".

注意“ mysql”后面的冒号。