0

How do I link up eclipse and mysql? I have already added the mysql-connector-java-5.1.38-bin.jar via project>(right-click)>properties>Java Build Path>Libraries.

However each time I run my application, I am still getting the Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver error. Hope someone can advise. Thank you.

javaperson
  • 109
  • 1
  • 12

2 Answers2

1

When you add the mysql-connector jar, it is available for class compilations of your Project code. Usually the drivers would be loaded using reflection, i.e. through Class.forName("driver_class_name_here"). This happens during the run-time and compiler need not bother about what class name is being provided.

Usually the web-applications are packaged as war archives and deployed into the web-containers or servers. When the server executes on these archives, it expects the referred class in the classpath. The classpath includes many locations and one among them is the WEB-INF\lib folder of the archive.

When we build the web-archive through Eclipse or by using build tools like ant or maven, we need to specify what to be included inside the archive. On Eclipse, we do that through the Deployment Assembly tab on Project Properties.

For a web-project with the following structure:

enter image description here

We add the specific library to the archive through Deployment Assembly > Add > Java Build Path Entries > Next, select the libraries and say Finish. We specify that the mysql connector to be included into the archive as:

enter image description here

James Jithin
  • 10,183
  • 5
  • 36
  • 51
0

you still need to connect to your database

Connection conn;
java.sql.Statement st;
ResultSet rs;
String url;
String db = "databasename";
url = "jdbc:mysql://localhost:3306/" + db + "";
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, "root", "");
st = conn.createStatement();
String sqlquery = "enter your query";
rs = st.executeQuery(sql);
  • It's a web application and I am using connection pooling. The context.xml file is placed in the WEB-INF folder. This same application runs without any problem in NetBeans, but in Eclipse I am having an issue. I suspect that my context.xml file is not being read when the application is deployed, because when I tried to simulate the same problem in netbeans by commenting out the section of context.xml, I get the same error. I am not sure how to go about rectifying this. – javaperson Apr 28 '16 at 13:48