jsp中如何把数据库中查询出来的数据输出到jsp页面。

我用的是sqlserve数据库,查询返回的是一个list接口类型。怎么样将查询来的数据输出到jsp动态页面。
最新回答
琉璃水色

2024-09-17 00:09:19

1、把数据库建好

CREATE TABLE `User` (

`id` int(11) NOT NULL,

`username` varchar(255) DEFAULT NULL,

`password` varchar(255) DEFAULT NULL,

`address` varchar(255) DEFAULT NULL,

PRIMARY KEY (`id`)

2、插入一个数据

insert into User values(1,"Martin","12345","zjut")

3、建立一个Dynamic Web Project,目录如下:

4、showinfo.jsp,这里需要注意的是导入类:import="com.entity.User"

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8" import="com.entity.User"%>

<!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=UTF-8">

<title>Insert title here</title>

</head>

<body>

<%

User user = (User)request.getAttribute("user");

%>

(此处空一行)

id:<%=user.getId() %>

username:<%=user.getUsername() %>

password:<%=user.getPassword() %>

address:<%=user.getAddress() %>

(此处空一行)

<!-- 

也可以用el表达式,但是同样需要servlet先将值存放在request中,但是用el表达式的话需要在上面加上:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

如下,获取id这样写就可以

${user.getId()}

-->

</body>

</html>

5、结果如下:

——__唯你懂我信

2024-09-17 02:04:03

<%@ page language="java" import="java.sql.*" pageEncoding="utf-8" errorPage="error.jsp"%>
<html>
<head>
<script type="text/javascript">
<!--
function modifyStu(){
var isMod=confirm("确定要修改该学生信息吗?");
if(!isMod)
return false;
}
function deleteStu(){
var isDel=confirm("确定要删除该学生的信息吗?");
if(!isDel)
return false;
}
-->
</script>

<title>list.jsp</title>
</head>
<body>
<table border="1" bgcolor="#ffffff" align="center" cellspacing="1" cellpadding="1">
<caption>学籍管理系统</caption>
<tr>
<td align="center" width=16%>学号</td>
<td align="center" width=16%>姓名</td>
<td align="center" width=8%>性别</td>
<td align="center" width=8%>年龄</td>
<td align="center" width=16%>籍贯</td>
<td align="center" width=12%>院系</td>
<td align="center" width=12%>更改</td>
<td align="center" width=12%>删除</td>
</tr>
<%
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=university";
Connection con=DriverManager.getConnection(url,"admin111","111111");
Statement stmt=con.createStatement();
String sql="select * from student";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
%>
<tr>
<td width=16% align="center"><%=rs.getObject(1) %></td>
<td width=16% align="center"><%=rs.getObject(2) %></td>
<td width=8% align="center"><%=rs.getObject(3) %></td>
<td width=8% align="center"><%=rs.getObject(4) %></td>
<td width=16% align="center"><%=rs.getObject(5) %></td>
<td width=12% align="center"><%=rs.getObject(6) %></td>
<td width=12% algin="center" onclick="return modifyStu()"><a href="change.jsp?xuehao=<%=rs.getObject(1) %>">修改</a></td>
<td width=12% algin="center" onclick="return deleteStu()"><a href="del.jsp?xuehao=<%=rs.getObject(1) %>">删除</a></td>
</tr>
<%
}
rs.close();
stmt.close();
con.close();
}catch(Exception e){
e.printStackTrace();
}
%>
</table>
<div align="center"><a href="insert.jsp">添加新记录</a></div>
</body>
</html>

楼主可以看一下这个简单的例子...

楼主就会明白的..

如果还是看不懂或是有什么不明白的可以来百度HI我

祝楼主早日成功!
一抹暖阳

2024-09-17 00:58:02

你用一个实体类 也就是一个javabean
把数据封装在javabean里面

我给你一个例子 吧

ResultSet rs=null;
public ArrayList getStudentByPage(int pageNow){
ArrayList al=new ArrayList();
try{
db.open();
rs=db.select("select top "+pageSize
+" * from Student where studyId not in(select top "
+pageSize*(pageNow-1)+" studyId from Student) ");
while(rs.next()){
al.add(new Student(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6)));
}
}catch(Exception e){
e.printStackTrace();
}finally{
db.close();
}
return al;
}

在jsp页面就是这样处理

<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="homework.bean.*,java.util.*" %>
<jsp:useBean id="um" scope="page" class="homework.bean.StudentManager">
</jsp:useBean>
<html>
<head>
<link href="image/style.css"
type=text/css rel=stylesheet>
<title>
ÍøÉÏ×÷ÒµÌύϵͳ-²é¿´Ñ§ÉúÐÅÏ¢
</title>
<script language="JavaScript" src="JavaScript/page.js" type="text/JavaScript">
</script>
</head>
<body bgcolor="#c0c0c0">
<div align="right">
<%
try{
Admin a=(Admin)session.getAttribute("username");
if(a==null){
response.sendRedirect("NotLogin.jsp");
}else{
}
}catch(Exception e){
}
%>
<font color="red" size="2">»¶Ó­ <a href="showAdminInfo.jsp"><U>
<%=session.getAttribute("username") %></U></a> ¹ÜÀíÔ±£¡</font>
</div><jsp:include flush="true" page="top.jsp"></jsp:include>
<h3 align="center">ѧÉúÐÅÏ¢Áбí</h3>
<div align="center">
<%
int pageSize=3;
int pageNow=1;
int rowCount=0;
int pageCount=0;
String s=request.getParameter("pageNow");
if(s!=null){
pageNow=Integer.parseInt(s);
}
ArrayList al=um.getStudentByPage(pageNow);
%>
<table border="2" bordercolor="#0000FF">
<tr><td width="101">ѧÉúѧºÅ</td><td width="91">ѧÉúÐÕÃû</td>
<td width="106">ÃÜÂë</td><td width="80">×ö×÷Òµ´ÎÊý</td>
<td width="106">ÐÔ±ð</td><td width="68">°à¼¶</td><td width="120">²Ù×÷Ñ¡Ïî</td></tr>
<%
if(al.isEmpty()){
%><table>
<tr><td align="center" height="10"><font color="red">ûÓÐÊý¾ÝÏÔʾ£¡</font></td>
</tr></table>
<%}
for(int i=0;i<al.size();i++){
Student u=(Student)al.get(i);
%>
<%int studyId=u.getStudyId();%>
<%String name=(String)u.getName(); %>
<tr><td><%=u.getStudyId()%></td><td><%=u.getName()%></td><td><%=u.getPassword()%></td>
<td><%=u.getSex()%></td><td><%=u.getClasses()%></td><td><%=u.getDoworktimes()%></td>
<td>[<a href="/HomeWorkWeb/delstudent?studyId=<%=studyId%>"
onClick="{if(confirm('ȷʵҪɾ³ý´ËѧÉúÐÅÏ¢Âð£¿')){return true;}return false;}">ɾ³ýÓû§</a>]
[<a href="StudentModify.jsp?studyId=<%=studyId%>&name=<%=name%>">ÐÞ¸ÄÐÅÏ¢</a>]</td>
</tr>
<%}%>
</table><br>
<a href="StudentInfo.jsp?pageNow=<%=1 %>"><u>[Ê×Ò³]</u></a>
<%
if(pageNow!=1){%>
<a href="StudentInfo.jsp?pageNow=<%= (pageNow-1)%>"><u>[ÉÏÒ»Ò³]</u></a>
<%}
pageCount =um.getPageCount();
for(int i=1;i<=pageCount;i++)
{%>
<a href="StudentInfo.jsp?pageNow=<%=i%>">[<%=i%>]</a>
<%}
if(pageNow!=pageCount){%>
<a href="StudentInfo.jsp?pageNow=<%= (pageNow+1)%>"><u>[ÏÂÒ»Ò³]</u></a>
<%}%>
<a href="StudentInfo.jsp?pageNow=<%=pageCount%>"><u>[βҳ]</u></a>
</div><br />
<div align="center"><br />
×ܹ²ÓÐ:<font color="red"><%=pageCount %></font>Ò³ ÕâÊǵÚ<font color="red"><%=pageNow %></font>Ò³
</div>
<center>
<form action="StudentInfo.jsp?pageNow" method="GET" name="form1"> ÊäÈëÒ³Êý:
<input type="text" name="pageNow" size="4" />
<input name="submit" type="submit" value="·­Ò³->" onClick="return checkBig()" />
</form>
<jsp:include flush="true" page="foot.jsp"></jsp:include>
</center>
</body>
</html>
愛你不愛你

2024-09-17 01:36:26

用一个作用域保存(session/request/application)都行 用forward调转到你想要的JSP页面中获取就行了
比方说你保存的:request.setAttribute("lst",list)假设list为你的集合 name为其中的属性 则跳转的JSP页面后 获取name用 :${list.name}即可!