Quantcast
Channel: Active questions tagged servlets - Stack Overflow
Viewing all articles
Browse latest Browse all 675

How to install JDBC driver in servlet based project without facing java.lang.ClassNotFoundexception

$
0
0

There is a VERY similar question to mine but in my case I don't have any duplicate jars in my build path, so the solution does not work for me. I've searched google for a couple of hours now, but none of the solutions I've found there actually resolve my issue. I'm creating a web site with some database connectivity for a homework. I'm using a MySQL database, developing in Eclipse and running on Windows.

I keep getting java.lang.ClassNotFoundException: com.mysql.jdbc.Driver with the following code:

import java.sql.*;//...public void someMethodInMyServlet(PrintWriter out){    Connection connection = null;    PreparedStatement query = null;    try {        out.println("Create the driver instance.<br>");        Class.forName("com.mysql.jdbc.Driver").newInstance();        out.println("Get the connection.<br>");        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "secret");        query = connection.prepareStatement( "SELECT * FROM customers");        //...    } catch (Exception e)    {        out.println(e.toString()+"<br>");    }}//...

When I run the above code I get the following output:

Create the driver instance.java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

It doesn't get past the Class.forName... line and I can't figure out why! Here is what I did:

  1. Download mysql-connector.
  2. Put it in my MySQL folder C:\Program Files\MySQL\mysql-connector-java-5.1.12\mysql-connector-java-5.1.12-bin.jar.
  3. Opened the project properties in Eclipse.
  4. Add External Jar to my Build Path and I selected mysql-connector-java-5.1.12-bin.jar.

Every time I attempt to use the servlet I get the same error regardless if I have the jar in there or if I don't. Could you help me figure this out?


Viewing all articles
Browse latest Browse all 675

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>