servlet:DBController.java
java.sql.Statement statement = conn.createStatement();
ResultSet rs2=statement.executeQuery("select * from stud_db");
while(rs2.next())
{
int id2= rs2.getInt("id");
String name2= rs2.getString("name");
int age2=rs2.getInt("age");
String address2= rs2.getString("address");
float percentage2 = rs2.getFloat("percentage");
request.setAttribute("id",id2);
request.setAttribute("name",name2);
request.setAttribute("age", age2);
request.setAttribute("address",address2);
request.setAttribute("percentage", percentage2);
request.getRequestDispatcher("/next.jsp").include(request,response);
我的jsp页面
下一个.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>next page</title>
</head>
<body>
<center><h1>DATA SUCCESSFULLY ENTERED</h1></center>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<h2>Details are shown below </h2>
<body>
<form action="${pageContext.request.contextPath}/DBController" method="POST">
<table border="2">
<tr>
<td>ID</td>
<td>NAME</td>
<td>AGE</td>
<td>ADDRESS</td>
<td>PERCENTAGE</td>
</tr>
<c:forEach var="row" items="${result.rows}"> </c:forEach>
<tr>
<td><c:out value="${row.id}"/></td>
<td><c:out value="${row.name}"/></td>
<td><c:out value="${row.age}"/></td>
<td><c:out value="${row.address}"/></td>
<td><c:out value="${row.percentage}"/></td>
</table>
</form>
<h1>Thank you..</h1>
</body>
</html>
最佳答案
<c:forEach var="row" items="${result.rows}">
<tr>
<td><c:out value="${row.id}"/></td>
<td><c:out value="${row.name}"/></td>
<td><c:out value="${row.age}"/></td>
<td><c:out value="${row.address}"/></td>
<td><c:out value="${row.percentage}"/></td>
</tr> // Check this
</c:forEach> // Also this
</table>
关于java - 无法从数据库检索数据。出现运行时错误。这是我的 servlet DBController.java 的一个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22500558/