how to add mysql driver jar file in eclipse

15,877

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

libraries.png

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");
Share:
15,877
user1994587
Author by

user1994587

Updated on June 04, 2022

Comments

  • user1994587
    user1994587 almost 2 years

    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?

  • user1994587
    user1994587 about 10 years
    Thanks, that work. But the command: MysqlDataSource ds = new MysqlConnectionPoolDataSource(); stil get error. how can I fix that?
  • Gord Thompson
    Gord Thompson about 10 years
    @user1994587 In that case import com.mysql.jdbc.jdbc2.optional.*; works fine for me.