2

我是jsp的初学者,我试图从数据库中列出用户,这是我的代码:

登录.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>

我提到了网站tutorialspoint:http ://www.tutorialspoint.com/jsp/jsp_database_access.htm

但我收到了这个错误(它很长,很抱歉):

   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)

这是我的项目结构: 在此处输入图像描述

抱歉这个问题太长了。请指导我解决这个问题。谢谢:)

4

2 回答 2

4

问题是您的项目中没有包含 MySQL 驱动程序。在堆栈跟踪中明确说明:

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

对于 Web 应用程序项目,请确保将所有第三方库(即 jars)放在 WEB-INF/lib 文件夹中,别无他处。

此外,根据您的图像,您似乎有两个 MySQL 驱动程序。只使用一个,摆脱另一个。

更多信息:

于 2013-10-16T16:49:34.797 回答
0

尝试将 mysql 连接器 jar 添加到项目的部署程序集中。为此,请右键单击项目并打开“部署程序集”选项卡。然后将 Jar 添加到程序集中。

于 2013-10-16T16:53:16.593 回答