public String searchNotice(){
DbCon db=new DbCon();
Connection con=db.getCon();
PreparedStatement pstmt=null;
ResultSet rs=null;
ArrayList arr=new ArrayList();
String str="";

try {
pstmt=con.prepareStatement("select * from classNotice ");
// pstmt.setInt(1, id);
// System.out.println("ljfsalfjsf");
rs=pstmt.executeQuery();
// System.out.println("Yes or No");
while(rs.next()){
ClassNotice cn=new ClassNotice();
cn.setNoticeTitle(rs.getString(2));
cn.setNoticeDate(rs.getDate(3));
cn.setNoticeContent(rs.getString(4));
arr.add(cn);
}
ActionContext.getContext().put("data", arr);
str="success";
// System.out.println("Come here ~");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
str="error";
}finally{
db.close(con, pstmt, rs);
}
return str;
 jsp页面:<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><!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>公告内容</title>
<style type="text/css">
h2.style1{
font-size:30px;
color:#ff0000;
}
</style>
</head>
<body>
<table align="center">
<caption>
<th>
<h2 class="style1">公告内容</h2>
</th>
</caption>
<c:forEach items="${data}" var="a">
<tr>
<th align="left">标题:${a.noticeTitle }</th>
</tr>
<tr>
<th align="left">日期:${a.noticeDate }</th>
</tr>
<tr>
<th align="left">内容:${a.noticeContent }</th>
</tr>
<tr><th><br/><br/></th></tr>
</c:forEach>
<tr>
<th align="right"><a href="searchpage.action">返回公告首页</a></th>
</tr>
</table>
</body>
</html>    通过所编的Java代码的SQL语句查询,结果在显示页面上将数据库全部的记录都显示出来。而我想要实现的是只显现我要查询的单个记录。感谢大侠们的关注~~~

解决方案 »

  1.   

    补充下:如果将SQL语句设置为select * from classNotice where id=?,再将注释去掉的话,得出的结果是:没有显示内容。这到底是哪里出问题了呢?
      

  2.   

    String id;//定义get,set方法 
    pstmt=con.prepareStatement("select * from classNotice where id= " +id);
      

  3.   

    用struts框架实现数据库的连接呀~~笔者比较了解Struts,所以在其基础上连接数据库的,理论上是用hibernate比较好,但现在水平较低,还不能运用
      

  4.   

    +id,这是什么呀?可这么用的吗  似乎不行呀 where id=   ,应该设置占位符?吧,但设置占位符后,就像我注释后的那些,结果也没能实现
      

  5.   

               pstmt=con.prepareStatement("select * from classNotice where id=?");
                pstmt.setInt(1, id);
    这样就行了。这样你就把id值设置到了。第一个问号中。可以以此类推
      

  6.   

    如果你想用字符串拼接可以这样  
     pstmt=con.prepareStatement("select * from classNotice where id='"+id+"' ");
      

  7.   

    查询不出 很可能是id没传进来你可以在方法前打印一下id的值是多少,然后再拿这条语句去数据库查询一下
      

  8.   

    确实发觉id没有传进,打印id,值总是0。不知为何?
    显示这样的错误:id=0
    2010-7-12 11:11:43 org.apache.catalina.core.ApplicationDispatcher invoke
    严重: Servlet.service() for servlet jsp threw exception
    java.lang.NumberFormatException: For input string: "noticeTitle"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at javax.el.ListELResolver.coerce(ListELResolver.java:166)
    at javax.el.ListELResolver.getValue(ListELResolver.java:51)
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:53)
    type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.怎么看呀~~
      

  9.   

    DEBUG一下嘛,
    看看在之前给ID赋值的时候是不是有问题
      

  10.   

    楼主是刚看java的。
    select * from classNotice   查的是全部,加上“where id=?”类似的条件就能是1条了。
           正规应该这样。
     while -->if  这样肯定是一个。但结果对思路不对