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语句查询,结果在显示页面上将数据库全部的记录都显示出来。而我想要实现的是只显现我要查询的单个记录。感谢大侠们的关注~~~
pstmt=con.prepareStatement("select * from classNotice where id= " +id);
pstmt.setInt(1, id);
这样就行了。这样你就把id值设置到了。第一个问号中。可以以此类推
pstmt=con.prepareStatement("select * from classNotice where id='"+id+"' ");
显示这样的错误: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.怎么看呀~~
看看在之前给ID赋值的时候是不是有问题
select * from classNotice 查的是全部,加上“where id=?”类似的条件就能是1条了。
正规应该这样。
while -->if 这样肯定是一个。但结果对思路不对