struts2框架中如何查询数据库中的一条记录

student_Forever2010 2010-07-12 09:00:49

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语句查询,结果在显示页面上将数据库全部的记录都显示出来。而我想要实现的是只显现我要查询的单个记录。感谢大侠们的关注~~~
...全文
348 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
问题解决了,谢谢各位啦~~结贴啦~
kString 2010-07-12
  • 打赏
  • 举报
回复
楼主是刚看java的。
select * from classNotice 查的是全部,加上“where id=?”类似的条件就能是1条了。
正规应该这样。
while -->if 这样肯定是一个。但结果对思路不对
aifei_10 2010-07-12
  • 打赏
  • 举报
回复
DEBUG一下嘛,
看看在之前给ID赋值的时候是不是有问题
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 li7134551921 的回复:]
查询不出 很可能是id没传进来

你可以在方法前打印一下id的值是多少,然后再拿这条语句去数据库查询一下
[/Quote]
确实发觉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 report

message

description The server encountered an internal error () that prevented it from fulfilling this request.



怎么看呀~~
DAVE_LUO 2010-07-12
  • 打赏
  • 举报
回复
楼上正解
闭目鱼 2010-07-12
  • 打赏
  • 举报
回复
查询不出 很可能是id没传进来

你可以在方法前打印一下id的值是多少,然后再拿这条语句去数据库查询一下
lijiemissfei 2010-07-12
  • 打赏
  • 举报
回复
如果你想用字符串拼接可以这样
pstmt=con.prepareStatement("select * from classNotice where id='"+id+"' ");
lijiemissfei 2010-07-12
  • 打赏
  • 举报
回复
pstmt=con.prepareStatement("select * from classNotice where id=?");
pstmt.setInt(1, id);
这样就行了。这样你就把id值设置到了。第一个问号中。可以以此类推

  • 打赏
  • 举报
回复
[Quote=引用 2 楼 simaa0106 的回复:]
String id;//定义get,set方法
pstmt=con.prepareStatement("select * from classNotice where id= " +id);
[/Quote]
+id,这是什么呀?可这么用的吗 似乎不行呀 where id= ,应该设置占位符?吧,但设置占位符后,就像我注释后的那些,结果也没能实现
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 qjsjp 的回复:]
struts和数据库有啥关系
[/Quote]
用struts框架实现数据库的连接呀~~笔者比较了解Struts,所以在其基础上连接数据库的,理论上是用hibernate比较好,但现在水平较低,还不能运用
JavPer 2010-07-12
  • 打赏
  • 举报
回复
struts和数据库有啥关系
simaa0106 2010-07-12
  • 打赏
  • 举报
回复
String id;//定义get,set方法
pstmt=con.prepareStatement("select * from classNotice where id= " +id);
  • 打赏
  • 举报
回复
补充下:如果将SQL语句设置为select * from classNotice where id=?,再将注释去掉的话,得出的结果是:没有显示内容。这到底是哪里出问题了呢?

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧