找不到适合 jdbc mysql 的驱动程序? [英] No suitable driver found for jdbc mysql?

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

问题描述

我正在尝试编写一个程序以在 Eclipse 中连接到 MySQL 数据库,但出现错误java.sql.SQLException:找不到合适的驱动程序".

I am trying to write a program to connect to a MySQL database in eclipse, but I get the error "java.sql.SQLException: No suitable driver found".

java代码为:

import java.sql.*;

public class FirstExample {

//static final String S_JDBC_DRIVER = "com.mysql.jdbc.Driver";  
static final String S_DB_URL = "jdbc:mysql://localhost:3306/emp";
static final String S_USER = "root";
static final String S_PASS = "root";

public static void main(String[] args) {

    try {

        System.out.println("Connecting to database...");
        //Class.forName(S_JDBC_DRIVER);
        Connection connection = DriverManager.getConnection(S_DB_URL,
                S_USER, S_PASS);

        System.out.println("Creating statement...");
        Statement statement = connection.createStatement();
        String sql = "SELECT * FROM Employee";
        ResultSet resultSet = statement.executeQuery(sql);

        while (resultSet.next()) {

            int iId = resultSet.getInt("id");
            int iAge = resultSet.getInt("age");
            String sFirst = resultSet.getString("fname");
            String sLast = resultSet.getString("lname");

            System.out.print("ID: " + iId);
            System.out.print("	Age: " + iAge);
            System.out.print("	First: " + sFirst);
            System.out.println("	Last: " + sLast);
        }

        resultSet.close();
        statement.close();
        connection.close();
    } catch (SQLException se) {

        for (Throwable t : se) {
            t.printStackTrace();
        }
    } 
    System.out.println("Goodbye!");
}

}

控制台选项卡中的输出为:

The output in the console tab is:

Connecting to database...
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/emp
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at FirstExample.main(FirstExample.java:21)
Goodbye!

我使用了 MySQL Connector/J.解压到MySQL安装目录下,将jar文件添加到CLASSPATH中.

I have used the MySQL Connector/J. It is unzipped in the MySQL installation directory and the jar file is added to the CLASSPATH.

另请参阅此图片.有一个 !在项目根目录处标记.image01

Also refer to this image. There is an ! mark at the project root.image01

我收到如下图所示的错误:image02 当我包括了 2 条评论语句:

I get the error as in the next image: image02 when I include the 2 commented statements:

static final String S_JDBC_DRIVER = "com.mysql.jdbc.Driver";
Class.forName(S_JDBC_DRIVER);

推荐答案

CLASSPATH 环境变量用于除最琐碎的应用程序之外的所有应用程序.通常,这些库包含在 jar 清单的 Class-Path 条目中,或包含在 java 命令行的 -cp 选项中.

For all but the most trivial applications the CLASSPATH environment variable is NOT used. Normally the libraries are include in the Class-Path entry in the manifest of the jar, or in the -cp option of the java commandline.

在这种情况下,您需要将 MySQL JDBC 驱动程序添加到 Eclipse 项目的构建路径中.

In this case you need to add the MySQL JDBC driver to the buildpath of your Eclipse project.

这篇关于找不到适合 jdbc mysql 的驱动程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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