没有合适的驱动程序找到jdbc mysql?

编程入门 行业动态 更新时间:2024-10-27 00:29:39
本文介绍了没有合适的驱动程序找到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?

本文发布于:2023-10-18 23:43:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1505767.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:驱动程序   合适   mysql   jdbc

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!