运行listPerson.jsp 出现com.mysql. cannot be resolved to a t

J2EE 码拜 9年前 (2016-06-02) 1714次浏览
[b][color=#FF0000]在MyEclipse里面运行listPerson.jsp出现下面提示错误:[/b][/color]
[color=#FF0000]An error occurred at line: 42 in the jsp file: /listPerson.jsp
com.mysql.jdbc.Driver cannot be resolved to a type

39:
40:  try {
41:  // 娉ㄥ唽 MySQL 椹卞姩. 涔熷彲浠ヤ娇鐢ㄤ笅闈袱绉嶆柟寮忕殑浠讳竴绉?
42:  DriverManager.registerDriver(new com.mysql.jdbc.Driver());
43:  //new com.mysql.jdbc.Driver();
44:  //Class.forName(“com.mysql.jdbc.Driver”).newInstance();
45:

Stacktrace:] with root cause
org.apache.jasper.JasperException: Unable to compile class for JSP:
at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:485)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:379)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:354)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:341)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:657)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
[/color]
代码如下:
<%@ page language=”java” pageEncoding=”UTF-8″
contentType=”text/html;charset=UTF-8″%>
<%@ page import=”java.sql.DriverManager”%>
<%@ page import=”java.sql.Connection”%>
<%@ page import=”java.sql.Statement”%>
<%@ page import=”java.sql.ResultSet”%>
<jsp:directive.page import=”java.sql.Date” />
<jsp:directive.page import=”java.sql.Timestamp” />
<jsp:directive.page import=”java.sql.SQLException” />
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<title>My JSP “listPerson.jsp” starting page</title>
<meta http-equiv=”pragma” content=”no-cache”>
<meta http-equiv=”cache-control” content=”no-cache”>
<meta http-equiv=”expires” content=”0″>
<meta http-equiv=”keywords” content=”keyword1,keyword2,keyword3″>
<meta http-equiv=”description” content=”This is my page”>
<style type=”text/css”>
body, td, th, input {
font-size: 12px;
text-align: center;
}
</style>
</head>
<body>
<table align=right>
<tr>
<td><a href=”addPerson.jsp”>新建人员资料</a></td>
</tr>
</table>
<br />
<br />
<%
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 注册 MySQL 驱动. 也可以使用下面两种方式的任一种
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//new com.mysql.jdbc.Driver();
//Class.forName(“com.mysql.jdbc.Driver”).newInstance();
// 获取数据库连接。 三个参数分别为 连接URL,用户名,密码
conn = DriverManager.getConnection(“jdbc:mysql://localhost:3306/databaseWeb”, “root”, “123456”);
// 获取 Statement。 Statement 对象用于执行 SQL。相当于控制台。
stmt = conn.createStatement();
// 使用 Statement 执行 SELECT 语句。返回结果集。
rs = stmt.executeQuery(“select * from tb_person”);
%>
<form action=”operatePerson.jsp” method=get>
<table bgcolor=”#CCCCCC” cellspacing=1 cellpadding=5 width=100%>
<tr bgcolor=#DDDDDD>
<th></th>
<th>ID</th>
<th>姓名</th>
<th>英文名</th>
<th>性别</th>
<th>年龄</th>
<th>生日</th>
<th>备注</th>
<th>记录创建时间</th>
<th>操作</th>
</tr>
<%
// 遍历结果集。rs.next() 返回结果集中能否还有下一条记录。假如有,自动滚动到下一条记录并返回 true
while (rs.next()) {
int id = rs.getInt(“id”); // 整形类型
int age = rs.getInt(“age”);
String name = rs.getString(“name”); // 字符串类型
String englishName = rs.getString(“english_name”);
String sex = rs.getString(“sex”);
String description = rs.getString(“description”);
Date birthday = rs.getDate(“birthday”); // 日期类型,只有日期信息而没有时间信息
Timestamp createTime = rs.getTimestamp(“create_time”); // 时间戳类型,既有日期又有时间。
out.println(” <tr bgcolor=#FFFFFF>”);
out.println(” <td><input type=checkbox name=id value=” + id + “></td>”);
out.println(” <td>” + id + “</td>”);
out.println(” <td>” + name + “</td>”);
out.println(” <td>” + englishName + “</td>”);
out.println(” <td>” + sex + “</td>”);
out.println(” <td>” + age + “</td>”);
out.println(” <td>” + birthday + “</td>”);
out.println(” <td>” + description + “</td>”);
out.println(” <td>” + createTime + “</td>”);
out.println(” <td>”);
out.println(” <a href=”operatePerson.jsp?action=del&id=” + id
+ “” onclick=”return confirm(“确定删除该记录?”)”>删除</a>”);
out.println(” <a href=”operatePerson.jsp?action=edit&id=” + id + “”>修改</a>”);
out.println(” </td>”);
out.println(” </tr>”);
}
%>
</table>
<table align=left>
<tr>
<td><input type=”hidden” value=”del” name=”action”> <a
href=”#”
onclick=”var array=document.getElementsByName(“id”);for(var i=0; i<array.length;
){array[i].checked=true;i++;}”>全选</a> <a href=”#”
onclick=”var array=document.getElementsByName(“id”);for(var i=0; i<array.length;
){array[i].checked=false;i++;}”>取消全选</a> <input type=”submit”
onclick=”return confirm(“即将删除所选择的记录。能否删除?”); ” value=”删除”>
</td>
</tr>
</table>
</form>
<%
} catch (SQLException e) {
out.println(“发生了异常:” + e.getMessage());
e.printStackTrace();
} finally {
// 关闭
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
}
%>
</body>
</html>
解决方案

30

Class.forName(“com.mysql.jdbc.Driver”).newInstance();
用这种方式注册驱动吧~

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明运行listPerson.jsp 出现com.mysql. cannot be resolved to a t
喜欢 (0)
[1034331897@qq.com]
分享 (0)