Java程序和mySQL连接问题:找不到合适的驱动程序 [英] Java program and mySQL connectivity issue: No suitable driver found

查看:403
本文介绍了Java程序和mySQL连接问题:找不到合适的驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的MySQL数据库连接一直存在问题.我遇到错误:

找不到适用于jdbc的驱动程序:mysql://127.0.0.1/sakila.

我已经安装了mySQL工作台,并从这里安装了驱动程序 http://dev.mysql.com/downloads/connector/j/

我已经保存了mysql-connector-java-5.1.18-bin并将类路径设置为

C:\ Program Files \ Java \ jre7 \ lib \ mysql-connector-java-5.1.18-bin;

并启动了找到数据库的mysql工作台.

我正在使用的代码如下:我确信它可以正常工作,因为我已经邀请一位朋友对其进行测试.不幸的是,我们在不同的平台上进行开发,无法指导我如何解决此错误.有谁知道我该如何解决?

public class Version {

public static void main(String[] args) {

    Connection con = null;
    Statement st = null;
    ResultSet rs = null;

    String url = "jdbc:mysql://127.0.0.1/sakila";
    //String url = "jdbc:mysql://localhost:3306/sakila";
    String user = "root";
    String password = "root";


    try {


        con = DriverManager.getConnection(url, user, password);
        st = con.createStatement();
        rs = st.executeQuery("select * from actor;");

        System.out.println("test");

        if (rs.next()) {
            System.out.println(rs.getString(1));
        }

    } catch (Exception ex) {
        System.out.println(ex);
}
}

}

问题已解决.没有将.jar附加到bin文件的末尾,这是必需的.

解决方案

您需要在调用getConnection之前实例化驱动程序:

String pdriver = "com.mysql.jdbc.Driver";
Class.forName(pdriver).newInstance();

I've been having a problem with mySQL database connectivity. I'm getting an error:

No suitable driver found for jdbc:mysql://127.0.0.1/sakila.

I have installed mySQL workbench, and have the driver from here http://dev.mysql.com/downloads/connector/j/

I have saved mysql-connector-java-5.1.18-bin and set the classpath to

C:\Program Files\Java\jre7\lib\mysql-connector-java-5.1.18-bin;

and started the mysql workbench where the database is found.

The code I am using is as follows: Which I am sure works, as I've asked a friend to test it form me. Unfortunately, we are developing on different platforms and could not instruct me as to how to fix this error. Has anyone an idea on how I can fix this?

public class Version {

public static void main(String[] args) {

    Connection con = null;
    Statement st = null;
    ResultSet rs = null;

    String url = "jdbc:mysql://127.0.0.1/sakila";
    //String url = "jdbc:mysql://localhost:3306/sakila";
    String user = "root";
    String password = "root";


    try {


        con = DriverManager.getConnection(url, user, password);
        st = con.createStatement();
        rs = st.executeQuery("select * from actor;");

        System.out.println("test");

        if (rs.next()) {
            System.out.println(rs.getString(1));
        }

    } catch (Exception ex) {
        System.out.println(ex);
}
}

}

EDIT: Problem sovled. Did not have .jar appended to the end of the bin file, which is necessary.

解决方案

You need to instantiate the driver before calling the getConnection :

String pdriver = "com.mysql.jdbc.Driver";
Class.forName(pdriver).newInstance();

这篇关于Java程序和mySQL连接问题:找不到合适的驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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