【发布时间】:2019-11-03 13:54:40
【问题描述】:
我正在尝试使用 jsp 将我的数据库内容显示到网页上 除了标题“id name”等代码的html部分外,它显示为空白。 我认为我的代码是正确的。请帮助
我尝试过许多网站的相同代码。不幸的是,所有这些代码都以相同的方式结束
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<%
String host="jdbc:mysql://localhost:3306/dbname";
Connection conn=null;
Statement stat=null;
ResultSet res=null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try{
conn=DriverManager.getConnection(host,"root","password");
stat=conn.createStatement();
String data="select * from emp";
res=stat.executeQuery(data);
while(res.next())
{%>
<tr>
<td><%=res.getInt(1) %></td>
<td><%=res.getString("name") %></td>
<td><%=res.getString("email") %></td>
<td><%=res.getString("phone") %></td>
<td><%=res.getString("address") %></td>
</tr>
<%
}
} catch (Exception e) {
e.printStackTrace();
}
%>
</tbody>
</table>
</html>
只有代码的 html 部分中的标题(或)表名显示在网页上
【问题讨论】:
-
日志有错误吗?
-
jdbc 与 mysql 的连接似乎存在 java ClassNotFoundException,但是当我尝试在控制台上的 eclipse 中获取输出时,相同的代码执行良好(但在 JSP 中不起作用)。 .Error:java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver