0

This is my first time working with JSP. I've set up a tomcat 9.0.8 server and I'm using Java 8.5, along with MySQL 8.0.11, and I'm using Eclipse EE on Windows 10.

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

I've encountered this error, and I've seen multiple other people ask the same question so I've tried a lot of solutions but none of them work. I've downloaded mysql-connector-java-8.0.11 These are some of the solutions that I've tried so far:

  1. Added <%@ page import = "com.mysql.jbdc.Driver %> at start of file
  2. Tried both Class.forName("com.mysql.jdbc.Driver"); and Class.forName("com.mysql.jdbc.Driver").newInstance();
  3. Added the JAR file in:

    1. apache-tomcat-9.0.8\lib
    2. ..\eclipse-workspace\ProjectFolder
    3. ..\eclipse-workspace\ProjectFolder\WEB_INF\lib\
  4. Added in the BUILD PATH for the project as an external JAR

Please help out. I don't know what more to do.

EDIT: This is my current code.

<%@
    page import="java.sql.*, javax.sql.*"
%>

<%
    String name = request.getParameter("name");
    String age = request.getParameter("age");
    Class.forName("com.mysql.jdbc.Driver");
    java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "user", "pass");
    Statement s = con.createStatement();
%>

<html>
<head>
    <title> Processing </title>
</head>
<body>
    Hello <%=name%>
</body>
</html>

EDIT:

I've got it working by selecting the JAR in the "order and export" settings from the build path options.

Arvind
  • 155
  • 3
  • 12

1 Answers1

0

In Eclipse:

  1. Right Click on your project
  2. Select properties
  3. Select Java Build Path (from the left sidebar of the opened dialogue)
  4. Click on Libraries (on right sidebar)
  5. Click on Add External JARs
  6. Select your MySQL connector Jar and click open.

Now apply and re-run your code.

For reference: http://www.oxfordmathcenter.com/drupal7/node/44

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • I've done this. This is the fourth point in my question. It doesn't change anything. – Arvind May 19 '18 at 21:00
  • Then I think you can hit a try by clean & build your project or close the project and restart it. Because the adding external JAR will add the jars in reference lib. –  May 19 '18 at 21:07
  • 1
    Apart from adding the jar in your java build path, you must also ensure that it's included in your `Deployment Assembly`. The jar will need to be copied over to the WEB-INF/lib folder. – Timir May 20 '18 at 05:35
  • This is what I'd done, but there was one more step to finally make it work. I had to go to "order and export" in the build path and select the jar. – Arvind May 23 '18 at 10:55