Java 如何在eclipse中添加mysql驱动jar文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22481205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:59:36  来源:igfitidea点击:

how to add mysql driver jar file in eclipse

javamysqleclipsejdbcjar

提问by user1994587

I have the jar file: mysql-connector-java-5.1.14-bin.jarand I want to add it to my project. I add the jar at this way: project-> properties -> (Java Build Path) Libraries and add it from external jars. But when I try to use it and write:

我有 jar 文件:mysql-connector-java-5.1.14-bin.jar我想将它添加到我的项目中。我以这种方式添加 jar:项目-> 属性->(Java 构建路径)库并从外部 jar 中添加它。但是当我尝试使用它并编写时:

import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;

import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

I get error under "com.mysql".

我在“com.mysql”下收到错误消息。

What is my mistake?

我的错误是什么?

回答by Gord Thompson

If the JAR file for the JDBC driver appears in the "Referenced Libraries" branch of the project, like this:

如果 JDBC 驱动程序的 JAR 文件出现在项目的“引用库”分支中,如下所示:

libraries.png

图书馆.png

then you don't need an importstatement for it. Just use DriverManager.getConnection()and Java should be able to find the JDBC driver itself.

那么你不需要import它的声明。只要使用DriverManager.getConnection(),Java 应该能够找到 JDBC 驱动程序本身。

String myConnectionString =
        "jdbc:mysql://localhost:3307?" +
        "useUnicode=yes&characterEncoding=UTF-8";
// the following statement assumes
//     import java.sql.*;
Connection dbConnection = DriverManager.getConnection(myConnectionString, "root", "myPassword");