0

I'm new to databases but I have successfully set up a MySQL server on a Linux machine I have here. I can do MySQL things in the terminal and it works as expected. Now I'm trying to add the dependencies to my Java project in Eclipse so that I can query that database. I have added mysql-connector-java-5.1.38-bin.jar to my build path, in a folder called "lib" in the project folder, as suggested by Google results. Here's my code:

private void getDBEntries() throws SQLException, ClassNotFoundException {
    Class.forName("com.mysql.jdbc.Driver"); //Search results suggested adding this line

    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "[password]");
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery("select * from phones_table");

    while(rs.next()) {
        myList.add(rs.getString(1));
    }

    rs.close();
    st.close();
    con.close();
}

This is giving me a ClassNotFoundException on the Class.forName line. If I remove that line, I get the "no suitable driver found" SQLException on the following line.

I've already tried every common solution suggested to other people with similar problems, but no luck. Maybe there's something simple I'm not understanding.

Edit: I do know how to import jars, but I didn't realise that the mysql.jar had to be in the Java lib folder and referenced externally in Eclipse. However, after doing that I still have the same issue.

user2226001
  • 89
  • 1
  • 8
  • 1
    How you have added the mysql.jar file in eclipse? You have to add it by right click on the project and choose the external mysql.jar file. – Shriram Feb 04 '16 at 15:45
  • @Shriram I just have the jar file in the project. Why does it need to be external? – user2226001 Feb 04 '16 at 15:49
  • Eventhough you placed in lib folder it is an external jar file. just point it to the mysql.jar in the lib folder. – Shriram Feb 04 '16 at 15:51
  • @Shriram Okay, I've changed it so that the .jar file is in jre/lib/ext folder and referenced externally in Eclipse. But I still have the same issue. – user2226001 Feb 04 '16 at 16:04

0 Answers0