2

I have added the jar file for jdbc in build path for my project, but still its giving me this exception javax.servlet.ServletException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver"

I've added the jar file in

Project --> Proeperties -->Java Bild Path -->Libraries -->Add External Library.

<html>
<head><title>Member Details</title></head>
<body>
<h2>Members Details...!</h2>
<table>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="javax.sql.*;" %>

<% 

java.sql.Connection con;
java.sql.Statement s;
java.sql.ResultSet rs;
java.sql.PreparedStatement pst;

con=null;
s=null;
pst=null;
rs=null;

// Remember to change the next line with your own environment
String url= "jdbc:mysql://localhost:3306/employees";
String id= "root";
String pass = "admin";
try{

    Class.forName("com.mysql.jdbc.Driver");
    con = java.sql.DriverManager.getConnection(url, id, pass);

    }catch(SQLException e) {
        System.out.println("Connection Failed! Check output console");
        e.printStackTrace();
        return;
    }

String sql = "SELECT id, age, first, last FROM employee";
try{
    s = con.createStatement();
    rs = s.executeQuery(sql);
    %>

    <tr>
    <th>Employee ID</th>
    <th>Employee Age</th>
    <th>First Name</th>
    <th>Last Name</th>
    </tr>
    <%
    while( rs.next() ){
    %><tr>
    <td><%= rs.getString("id") %></td>
    <td><%= rs.getString("age") %></td>
    <td><%= rs.getString("first") %></td>
    <td><%= rs.getString("last") %></td>
    </tr>
    </table>
        <%
        }
        %>
    <%
    }catch(Exception e){
        e.printStackTrace();
        }

finally{
        if(rs!=null)
            rs.close();
        if(s!=null)
            s.close();
        if(con!=null)
            con.close();
            }
%>
</body>
</html>

Exception StackTrace:

org.apache.jasper.JasperException: javax.servlet.ServletException:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver   
org.apache.jasper.servlet.JspServletWrapper.handleJspException   
(JspServletWrapper.java:549)
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)

root cause:

 javax.servlet.ServletException: java.lang.ClassNotFoundException: 
  com.mysql.jdbc.Driver     org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:912)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841)
org.apache.jsp.members_jsp._jspService(members_jsp.java:164)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)

root cause

    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:126)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:63)
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Unknown Source)
org.apache.jsp.members_jsp._jspService(members_jsp.java:94)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
9
  • 1
    Where you add this jar file ?? Commented Mar 10, 2014 at 8:05
  • 1
    Project > Properties > Java Build Path > Libraries > Add External Library Commented Mar 10, 2014 at 8:06
  • 1
    try{ Class.forName("com.mysql.jdbc.Driver"); con = java.sql.DriverManager.getConnection(url, id, pass); }catch(SQLException e) { System.out.println("Connection Failed! Check output console"); e.printStackTrace(); return; } Commented Mar 10, 2014 at 8:07
  • @Ali Edit your question with the new details. And remember to format the code Commented Mar 10, 2014 at 8:08
  • 1
    Can you post full stack trace? Commented Mar 10, 2014 at 8:09

1 Answer 1

2

You need the JDBC driver at runtime, not just at build time. The jar should NOT be added tho the build path of your eclipse project. It should simply be dropped inside WebContent/WEB-INF/lib, which will automatically add it to the build path AND to the set of jars that must be deployed as libraries of your webapp.

Sign up to request clarification or add additional context in comments.

3 Comments

Thnx, it worked, but now my question is, that when i was doing a project just like that one, i didnt even added the jdbc jar file in lib folder and just added the it in the build path and it did worked,,,
It probably wasn't a web project. Web projects are deployed and executed by an app server, not by Eclipse directly. They have their own rules.
got the point, the project that i was doing in the past was just a java project, not a web project...may be that could be the reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.