Roshan
Roshan

Reputation: 645

Servlet Exception: invalid driver class name

i am a beginner in jsp, i was trying to list the users from the database, here is my code:

Login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" 
    import="java.io.*,java.util.*,java.sql.*"%>
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>InfoVisual</title>

<link rel="stylesheet" href="css/style.css" type="text/css" media="screen">
</head>
<body>
<sql:setDataSource var="chh_cw" driver="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost/chh-cw"
     user="root"  password="1234"/>
<sql:query dataSource="${chh_cw}" var="result">
select DispUserName from user_mst;
</sql:query>
<table border="1" width="100%">
<tr>
   <th>District</th>
</tr>
<c:forEach var="row" items="${result.rows}">
<tr>
   <td><c:out value="${row.DispUserName}"/></td>
</tr>
</c:forEach>
</table>
    <form method="post" action="Login">
        <div class="login">

            Select District
            <select name="district">

            </select>
            <br />Username <input type="text" name="username"/>
            <br />Password <input type="password" name="pswd"/>
            <br /><input type="submit" name="submit" value="Login"/>
        </div>
    </form>
</body>
</html>

i referred to the site tutorialspoint: http://www.tutorialspoint.com/jsp/jsp_database_access.htm

but i am getting this error(it's quite long, sorry for that):

   org.apache.jasper.JasperException: An exception occurred processing JSP page /Login.jsp at line 17

14: <link rel="stylesheet" href="css/style.css" type="text/css" media="screen">
15: </head>
16: <body>
17: <sql:setDataSource var="chh_cw" driver="com.mysql.jdbc.Driver"
18:      url="jdbc:mysql://localhost/chh-cw"
19:      user="root"  password="1234"/>
20: <sql:query dataSource="${chh_cw}" var="result">


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    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:722)
root cause

javax.servlet.ServletException: javax.servlet.jsp.JspTagException: In &lt;driver&gt;, invalid driver class name: "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver"
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:908)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841)
    org.apache.jsp.Login_jsp._jspService(Login_jsp.java:126)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    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:722)
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)
    java.lang.Class.forName0(Native Method)
    java.lang.Class.forName(Class.java:270)
    org.apache.taglibs.standard.tag.common.sql.DataSourceWrapper.setDriverClassName(DataSourceWrapper.java:55)
    org.apache.taglibs.standard.tag.common.sql.SetDataSourceTagSupport.doStartTag(SetDataSourceTagSupport.java:111)
    org.apache.jsp.Login_jsp._jspx_meth_sql_005fsetDataSource_005f0(Login_jsp.java:152)
    org.apache.jsp.Login_jsp._jspService(Login_jsp.java:92)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    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:722)

here is my project structure: enter image description here

sorry this question became too long. please guide me through this problem. Thanx :)

Upvotes: 2

Views: 5376

Answers (2)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85789

The problem is that you have not included the MySQL driver in your project. It is clearly stated in the stacktrace:

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

For web application projects, make sure to drop all the third party libraries (i.e. jars) in WEB-INF/lib folder, nowhere else.

Also, by your image, seems that you have two MySQL Drivers. Use one only, get rid of the other.

More info:

Upvotes: 4

ovunccetin
ovunccetin

Reputation: 8683

Try to add mysql connector jar to deployment assembly of the project. To do this, right click the project and open "Deployment Assembly" tab. Then add the Jar into the assembly.

Upvotes: 0

Related Questions