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

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

问题描述

我正在尝试编写一个连接到eclipse中的MySQL数据库的程序,但是我收到错误java.sql.SQLException:找不到合适的驱动程序
$ b

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);
连接连接= DriverManager.getConnection(S_DB_URL,
S_USER,S_PASS);

System.out.println(创建语句...);
语句语句= 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(\tAge:+ iAge);
System.out.print(\tFirst:+ sFirst);
System.out.println(\tLast:+ sLast);
}

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

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

}



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

 连接到数据库... 
java.sql.SQLException:没有合适驱动程序找到jdbc:mysql:// localhost:3306 / emp
在java.sql.DriverManager.getConnection(未知源)
在java.sql.DriverManager.getConnection(未知来源)
在FirstExample.main(FirstExample.java:21)
再见!

我使用了MySQL Connector / J。它在MySQL安装目录中解压缩,并将jar文件添加到CLASSPATH。



还请参考此图像。有一个 !在项目根目录下 image01



我收到错误,如下图所示: image02 当我包含2个注释的语句:

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


解决方案

除了最简单的应用程序之外, CLASSPATH 环境变量使用。通常,库包含在jar的清单中的 Class-Path 条目中,或 -cp 选项的命令行。



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


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".

The java code is:

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("\tAge: " + iAge);
            System.out.print("\tFirst: " + sFirst);
            System.out.println("\tLast: " + 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!

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

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

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);

解决方案

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.

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

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

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