java.sql.SQLException:没有为jdbc找到合适的驱动程序:mysql:// localhost:3306 / dbname [英] java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/dbname

查看:2046
本文介绍了java.sql.SQLException:没有为jdbc找到合适的驱动程序:mysql:// localhost:3306 / dbname的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个Java程序: MySQLConnectExample.java

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

public class MySQLConnectExample {
public static void main(String [] args){
Connection conn1 = null;
连接conn2 = null;
连接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(连接到数据库test1);

String url2 =jdbc:mysql:// localhost:3306 / aavikme?user = root& password = aa;
conn2 = DriverManager.getConnection(url2);
if(conn2!= null){
System.out.println(连接到数据库test2);
}

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

conn3 = DriverManager.getConnection(url3,info);
if(conn3!= null){
System.out.println(连接到数据库test3);
}
} catch(SQLException ex){
System.out.println(发生错误。可能用户/密码无效);
ex.printStackTrace();
}
}
}

我编译它像这样:

  E:\ java mysql代码驱动程序> javac MySQLConnectExample.java 

E:\ java mysql代码驱动程序> java -cp mysql-connector-java-3.0.11-stable-bin.jar;。
MySQLConnectExample

我收到此错误:

 发生错误。也许用户/密码无效
java.sql.SQLException:没有为jdbc找到合适的驱动程序:mysql:// localhost:3306 /
aavikme
java.sql.DriverManager.getConnection(DriverManager) .java:596)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at MySQLConnectExample.main(MySQLConnectExample.java:20)

我做错了什么?

解决方案

制作确定你先运行它:

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

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

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


I have this Java program: 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();
        }
    }
}

I compile it like this:

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

I get this error:

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)

What am I doing wrong?

解决方案

Make sure you run this first:

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

This forces the driver to register itself, so that Java knows how to handle those database connection strings.

For more information, see the MySQL Connector reference.

这篇关于java.sql.SQLException:没有为jdbc找到合适的驱动程序:mysql:// localhost:3306 / dbname的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆