0 0

关于jsp查询的问题5

我现在已经做了这样一个页面,
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*,student.*" %>
<!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">


<link rel="stylesheet" type="text/css" href="css/jsp.css">
<script type="text/javascript" src="js/click.js"></script>
<script type="text/javascript" src="js/tdclick.js"></script>


<title>学生信息管理</title>
</head>
<body>
<h1 align="center">学生基本信息</h1>
<hr>
    <%
     	String name = request.getParameter("name");
    	List<Student> all=null;
    	try
    	{
    		if(name==null)
    		{
    			all=DaoFactory.getStudentInstance().queryall();
    		}
    		else
    		{
    			all=DaoFactory.getStudentInstance().querybyname(name);
    		}
    	}
    	catch(Exception e)
    	{
    		e.getStackTrace();
    	} 
     %>
<div align="center">
   <form name="form1" action="index.jsp"  method="post">
    	请输入姓名:<input type="text" name="name">
    	<input type="hidden" name="status" value="searchbyname">
    	<input type="submit" value="查询">
   </form>
</div>
<div align="center">
	    <form name="form2" action="StudentServlet" method="post">
		    <table>
		    <%
		    Iterator<Student> it=all.iterator();
		    int i=0;
		    %>
		    	<tr>
		    		<th width="80">选择</th>
		    		<th width="80">学号</th>
		    		<th width="80">姓名</th>
		    		<th width="80">成绩</th>
		    		<th width="80">修改</th>
		    	</tr>
		    	<%
		    	if(!it.hasNext())
		    	{
		    	%>
		    	<tr><td colspan="5">查无此数据!</td></tr>
		    	<%}
		    	else{
			    	while(it.hasNext())
			    	{  
			    		Student st=it.next();
			    		i++;
			    		int num=st.getNum();
			    		String names=st.getName();
			    		int score=st.getScore();
			    		
			    		if(i%2==1){
			    		%>
				    	<tr class="odd">
				    	<%}
			    		else{%>
			    		<tr class="ever">
			    			<%} %>
				    		<td width="80" height="30" align="center">
					    		<label>
					    		  <input type="checkbox" name="checkbox" id=<%=i %> value=<%=num%> onclick="A(this,id)">
					    		</label>
				    		</td>
				    		<td width="80" onclick="tdclick(this)"><%=num%></td>
				    		<td width="80" onclick="tdclick(this)"><%=names%></td>
				    		<td width="80" ><%=score%></td>
				    		<td width="80"><a href="StudentServlet?status=querybynum&num=<%=num%>">修改</a></td>
				    	</tr>
			   		<%} 
		    	}%>
		    </table>
		    <div align="center">
		    <input type="button" name="insertmethod" value="添加新纪录" onclick="click1()">
		    <input type="hidden" name="status" value="deletemethod">
		    <input type="submit" value="删除" onclick="delfirm(checkbox)">
			</div>
	    </form>
</div>
</body>
</html>

我现在想增加一个按学号查找的功能,而学号在数据库里是主键,所以,查询出来只有一个对象,我的DAO里
public Student querybynum(int num) throws Exception
	{
		Student st=null;
		String sql="select * from info where num=?";
		DBConnection dbc=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try {
			dbc=new DBConnection();
			pstmt=dbc.getConnection().prepareStatement(sql);
			pstmt.setInt(1, num);
			rs=pstmt.executeQuery();
			if(rs.next())
			{
				st=new Student();
				st.setNum(rs.getInt(1));
				st.setName(rs.getString(2));
				st.setScore(rs.getInt(3));
			}
		} catch (Exception e) {
			e.getStackTrace();
			System.out.println("操作异常!");
		}
		finally{
			rs.close();
			pstmt.close();
			dbc.close();
		}
		return st;
	}

也就只返回了一个student对象,而不是一个List对象,我想问的是,我在按姓名查找那个表单前面或者后面添加一个按学号查找的功能后,让按学号查找的结果仍然在form2表单里进行输出,该怎么写?
2012年9月13日 13:38

2个答案 按时间排序 按投票排序

0 0

引用
if(rs.next()) 
            { 
                st=new Student(); 
                st.setNum(rs.getInt(1)); 
                st.setName(rs.getString(2)); 
                st.setScore(rs.getInt(3)); 
            } 

改成
List<Student> students=new ArrayList<Student>();
if(rs.next())  
            {  
                st=new Student();  
                st.setNum(rs.getInt(1));  
                st.setName(rs.getString(2));  
                st.setScore(rs.getInt(3));
                students.add(st);  
            }  

然后将DAO方法的返回类型改成List<Student>,并将students返回即可

2012年9月13日 14:58
0 0

1、最简单的办法查不出放到List
2、写一个按照id范围查的方法  query(int beginId, int endId)【beginId<=id<=endId】  然后query(1,1)

2012年9月13日 13:54

相关推荐

Global site tag (gtag.js) - Google Analytics