how to add mysql driver jar file in eclipse

2019-07-11 20:56发布

问题:

I have the jar file: mysql-connector-java-5.1.14-bin.jar and 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:

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

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

I get error under "com.mysql".

What is my mistake?

回答1:

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

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

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