V
V
Viktoria Smirnova2017-08-20 18:21:22
Java
Viktoria Smirnova, 2017-08-20 18:21:22

ClassNotFoundException error when using Servlet?

Guys, I found a simple tutorial on the net on using a servlet and JDBC.

There are two classes:

import java.io.*;
 import javax.servlet.*;
 import javax.servlet.http.*;
 import java.sql.*;

 public class DirectorsViewServlet extends HttpServlet {

  public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
    DirectorSelect ds = new DirectorSelect();
    out.println(ds.selectDirectors());
  }

  public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    doGet(req, res);
  }
}


and

import java.sql.*;

 class DirectorSelect{
  public String selectDirectors() {
    Connection koneksi = null;
    Statement stat = null;
    String str = "";
    try{
        Class.forName("com.mysql.jdbc.Driver");
        koneksi = DriverManager.getConnection("jdbc:mysql://localhost/dbizza","root","");
        stat = koneksi.createStatement();
        ResultSet hasil = stat.executeQuery("SELECT * FROM directors");
        while (hasil.next()) {
            str = str + (hasil.getInt(1) + "\t" + hasil.getString(2)+ "\t" + hasil.getString(3)+ "\t" + hasil.getDate(4)+ "\t" + hasil.getString(5));
        }
        stat.close();
        koneksi.close();
    } catch (SQLException sqle) {
        str = "SQLException error";
    } catch (ClassNotFoundException cnfe) {
        str = "ClassNotFoundException error";
    }
    return str;
 }
}


web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 version="2.5"> 
<servlet>
    <servlet-name>DirViewServlet</servlet-name>
    <servlet-class>DirectorsViewServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>DirViewServlet</servlet-name>
    <url-pattern>/directors-view</url-pattern>
</servlet-mapping>
</web-app>


everything seems to be simple and when run on the command line (java and javac) everything works, but an error occurs from the browser: "ClassNotFoundException". I read something similar on English-language resources about the JDBC drivers, but I didn’t figure out exactly what the problem was. How to fix it?

I use IntelliJIDEA, Apache Tomcat 8.5

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
roswell, 2017-08-20
@Vika7

You need to put the JDBC driver JAR in $CATALINA_HOME/lib . By default, this directory is located at the root of the Tomcat installation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question