JavaDeveloper JavaDeveloper - 1 year ago 35
MySQL Question

Why do I get ClassNotFoundException when I call it from outside class?

Below is the code that works perfectly fine when it is executed from main (ie in eclipse RunAs java application). However if I call

Statement getStatement
from outside ( meaning a servlet calling it via a put ) in the same package I get java.lang.ClassNotFoundException: com.mysql.jdbc.Driver. Any pointers to why ?

public class DBConnectivity {

private static Statement getStatement(String username, String password) throws SQLException, ClassNotFoundException {
Properties prop = new Properties();
prop.put("user", "root");
prop.put("password", "password");


Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/keeptrack", prop);
Statement stmt = conn.createStatement();

return stmt;

public void put(String username, String password, String table,
String name, String exercise, String wsj, String food)
throws SQLException, ClassNotFoundException {
Statement stmt = getStatement(username, password);
// checkIfTableExists(stmt, table);
"insert into " + table +
" " +
"values(\'" + name + "\', \'" + exercise + "\', \'" + wsj + "\', \'" + food + "\')");

public List<String> get(String username, String password, String table)
throws SQLException, ClassNotFoundException {
Statement stmt = getStatement(username, password);
//checkIfTableExists(stmt, table);
ResultSet rs = stmt.executeQuery("select FOOD from " + table);

List<String> foodData = new ArrayList<String>();

while( {
String output = rs.getString("FOOD");
// System.out.println("wohoo: " + output);

return foodData;

public static void main(String[] args) throws SQLException, ClassNotFoundException {
DBConnectivity conn = new DBConnectivity();
conn.put("root", "password", "trackTable", "D", "B", "C", "D");
conn.get("root", "password", "trackTable");

Here is the servlet which calls the database:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("firstname");
String password = request.getParameter("lastname");
String firstname = username;
String exercise = request.getParameter("exerise");
String wsj = request.getParameter("wsj");
String food = request.getParameter("food");

try {
new DBConnectivity().put(username, password, "trackTable", firstname, exercise, wsj, food);
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block

new DBConnectivity().put
call results in

Stack trace is :

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(
at org.ameya.dynasty.DBConnectivity.getStatement(
at org.ameya.dynasty.DBConnectivity.get(
at org.ameya.dynasty.ServletIndex.doGet(
at javax.servlet.http.HttpServlet.service(
at javax.servlet.http.HttpServlet.service(
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
at org.apache.catalina.core.ApplicationFilterChain.doFilter(
at org.apache.tomcat.websocket.server.WsFilter.doFilter(
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
at org.apache.catalina.core.ApplicationFilterChain.doFilter(
at org.apache.catalina.core.StandardWrapperValve.invoke(
at org.apache.catalina.core.StandardContextValve.invoke(
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(
at org.apache.catalina.core.StandardHostValve.invoke(
at org.apache.catalina.valves.ErrorReportValve.invoke(
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(
at org.apache.catalina.core.StandardEngineValve.invoke(
at org.apache.catalina.connector.CoyoteAdapter.service(
at org.apache.coyote.http11.AbstractHttp11Processor.process(
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(
at java.util.concurrent.ThreadPoolExecutor.runWorker(
at java.util.concurrent.ThreadPoolExecutor$
at org.apache.tomcat.util.threads.TaskThread$

Answer Source

Your error:

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

If it works running from Eclipse, that means you have the MySql jdbc dirver jar in the classpath of your project, but it is not available to your web application.

The project class path (build path) is not the same as the class path of your web application when deployed. Not all jars being on your project class path get exported automatically.

Usually jars in the WEB-INF/lib will be available for the web application, but jars outside of that will not.

You should either place the MySql jdbc driver jar in WEB-INF/lib, or add it to the lib folder of your Servlet container (Tomcat in your case judging by the exception stack trace).