java怎么把数据库的数据取出来
用struts怎么把数据库里的一张表的内容读到页面上呢?有具体的jsp和java代码吗?(一个jsp和一个action)追加40分。Vectorresult=list()...
用struts怎么把数据库里的一张表的内容读到页面上呢?
有具体的jsp和java代码吗?(一个jsp和一个action)
追加40分。
Vector result =list();
request.setAttribute("Result", result);
request.setAttribute("ResultSize", String.valueOf(result.size()));
return mapping.findForward("successselect");
list();如下:
Vector v = new Vector();
try{
db.pstmt = db.con.prepareStatement("select * from login");
db.rs = db.pstmt.executeQuery();
while (db.rs.next())
{ v.addElement(db.rs.getString(1));}
}
catch(Exception ex)
{ ex.printStackTrace();
return null;
}
return v;
不知道对不对。。。
然后jsp该怎么写呢?用<logic:iterate>...
<logic:iterate id="???" name="Result">
id要写什么?是随便的吗? 展开
有具体的jsp和java代码吗?(一个jsp和一个action)
追加40分。
Vector result =list();
request.setAttribute("Result", result);
request.setAttribute("ResultSize", String.valueOf(result.size()));
return mapping.findForward("successselect");
list();如下:
Vector v = new Vector();
try{
db.pstmt = db.con.prepareStatement("select * from login");
db.rs = db.pstmt.executeQuery();
while (db.rs.next())
{ v.addElement(db.rs.getString(1));}
}
catch(Exception ex)
{ ex.printStackTrace();
return null;
}
return v;
不知道对不对。。。
然后jsp该怎么写呢?用<logic:iterate>...
<logic:iterate id="???" name="Result">
id要写什么?是随便的吗? 展开
2个回答
展开全部
java类
public ArrayList selectall(ActiveForm af){
ArrayList list = new ArrayList();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DBConnection.getcon();
String selectsql = "";
if(af.getAttitle().equals("所有活动")){
selectsql = "select *,zongshu=(select count(abiaoti) from active) from active,atitle where active.atid=atitle.atid";
}else{
selectsql = "select *,zongshu=(select count(abiaoti) from active) from active,atitle where active.atid=atitle.atid and atitle.attitle='"+af.getAttitle()+"'";
}
ps = con.prepareStatement(selectsql);
rs = ps.executeQuery();
while(rs.next()){
ActiveForm af2 = new ActiveForm();
af2.setAid(rs.getInt("aid"));
af2.setAbiaoti(rs.getString("abiaoti"));
af2.setAtext(rs.getString("atext"));
af2.setAtid(rs.getInt("atid"));
af2.setAcost(rs.getInt("acost"));
af2.setAcity(rs.getString("acity"));
af2.setAcount(rs.getInt("acount"));
af2.setAdate(rs.getString("adate"));
af2.setAlevel(rs.getInt("alevel"));
af2.setAstate(rs.getInt("astate"));
af2.setAttitle(rs.getString("attitle"));
af2.setZongshu(rs.getString("zongshu"));
list.add(af2);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
DBConnection.close(con, ps, rs);
}
return list;
}
action内容
public ActionForward activefind(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ActiveForm activeform = (ActiveForm)form;
int acost = activeform.getAcost();
String acity = activeform.getAcity();
int acount = activeform.getAcount();
String adate = activeform.getAdate();
int alevel = activeform.getAlevel();
int astate = activeform.getAstate();
String attitle = request.getParameter("keyword");
StringFormat sf = new StringFormat();
activeform.setAcost(acost);
activeform.setAcity(sf.splitstr(StringFormat.notNull(acity)));
activeform.setAcount(acount);
activeform.setAdate(sf.splitstr(StringFormat.notNull(adate)));
activeform.setAlevel(alevel);
activeform.setAstate(astate);
activeform.setAttitle(sf.splitstr(StringFormat.notNull(attitle)));
ActiveDao ad = new ActiveDao();
ArrayList list = ad.selectall(activeform);
if(list.size()>0){
ActiveForm af = (ActiveForm) list.get(0);
request.getSession().setAttribute("zongshu", af.getZongshu());
request.getSession().setAttribute("list", list);
// request.setAttribute("zongshu", af.getZongshu());
// request.setAttribute("list", list);
return mapping.findForward("yes");
}else{
request.getSession().setAttribute("zongshu", "0");
list = new ArrayList();
request.getSession().setAttribute("list",list);
return mapping.findForward("yes");
}
}
jsp内容(涉及到保密,部分代码)
<%
String zongshu;
if (session.getAttribute("zongshu") != null) {
zongshu = (String)session.getAttribute("zongshu");
} else {
zongshu = "0";
}
%>
<div id="h-main-right">
<form id="form1" name="form1" method="post" action="<%=request.getContextPath()%>/ActiveAction.do?activetype=activeselect">
<div id="h-right-top">
<table width="100%" height="30" border="0" cellpadding="0"
cellspacing="0">
<tr>
<td width="40%">
当前共有
<%=zongshu%>
个会员活动
</td>
<td width="60%">
关键字
<label>
<input name="abiaoti" type="text" size="12" class="h-biao" />
城市
<input name="acity" type="text" size="12" class="h-biao" />
<select name="alevel">
<option value="100">
不限等级
</option>
<option value="0">
黑钻活动
</option>
<option value="1">
其他钻活动
</option>
</select>
<input type="submit" name="Submit" value="搜索活动" id="h-anniu" />
</label>
</td>
</tr>
</table>
</div>
<div id="h-right-bottom">
<table width="100%" height="80" border="0" cellpadding="0"
cellspacing="0">
<tr>
<td width="35%" height="25" bgcolor="#F1F5FA">
<span class="STYLE2">主题</span>
</td>
<td width="10%" height="25" bgcolor="#F1F5FA">
<span class="STYLE2">报名费</span>
</td>
<td width="15%" height="25" bgcolor="#F1F5FA">
<span class="STYLE2">活动城市</span>
</td>
<td width="6%" height="25" bgcolor="#F1F5FA">
<span class="STYLE2">人气</span>
</td>
<td width="12%" height="25" bgcolor="#F1F5FA">
<span class="STYLE2">发布日期</span>
</td>
<td width="10%" height="25" bgcolor="#F1F5FA">
<span class="STYLE2">报名权限</span>
</td>
<td width="12%" height="25" bgcolor="#F1F5FA">
<span class="STYLE2">活动状态</span>
</td>
</tr>
<%
ArrayList list = new ArrayList();
if (session.getAttribute("list") != null) {
list = (ArrayList) session.getAttribute("list");
}else{
ActiveDao ad = new ActiveDao();
list = ad.se();
}
if (list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
ActiveForm af = (ActiveForm) list.get(i);
%>
<tr>
<td width="35%" height="25"><a href="">
<%=af.getAbiaoti()%></a>
</td>
<td width="10%" height="25">
<%=af.getAcost()%>
</td>
<td height="25">
<%=af.getAcity()%>
</td>
<td width="6%" height="25">
<%=af.getAcount()%>
</td>
<td width="12%" height="25">
<%=af.getAdate()%>
</td>
<% if(af.getAlevel()==0){%>
<td width="10%" height="25">
<img src="<%=request.getContextPath()%>/images/yes-images/activity-02.jpg" alt="黑钻"/>
</td>
<%} %>
<td width="12%" height="25">
<%
if (af.getAstate() == 0) {
%>
已过期
<%
} else {
%>
正在举办
<%
}
%>
</td>
</tr>
<%
}
}
%>
public ArrayList selectall(ActiveForm af){
ArrayList list = new ArrayList();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DBConnection.getcon();
String selectsql = "";
if(af.getAttitle().equals("所有活动")){
selectsql = "select *,zongshu=(select count(abiaoti) from active) from active,atitle where active.atid=atitle.atid";
}else{
selectsql = "select *,zongshu=(select count(abiaoti) from active) from active,atitle where active.atid=atitle.atid and atitle.attitle='"+af.getAttitle()+"'";
}
ps = con.prepareStatement(selectsql);
rs = ps.executeQuery();
while(rs.next()){
ActiveForm af2 = new ActiveForm();
af2.setAid(rs.getInt("aid"));
af2.setAbiaoti(rs.getString("abiaoti"));
af2.setAtext(rs.getString("atext"));
af2.setAtid(rs.getInt("atid"));
af2.setAcost(rs.getInt("acost"));
af2.setAcity(rs.getString("acity"));
af2.setAcount(rs.getInt("acount"));
af2.setAdate(rs.getString("adate"));
af2.setAlevel(rs.getInt("alevel"));
af2.setAstate(rs.getInt("astate"));
af2.setAttitle(rs.getString("attitle"));
af2.setZongshu(rs.getString("zongshu"));
list.add(af2);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
DBConnection.close(con, ps, rs);
}
return list;
}
action内容
public ActionForward activefind(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ActiveForm activeform = (ActiveForm)form;
int acost = activeform.getAcost();
String acity = activeform.getAcity();
int acount = activeform.getAcount();
String adate = activeform.getAdate();
int alevel = activeform.getAlevel();
int astate = activeform.getAstate();
String attitle = request.getParameter("keyword");
StringFormat sf = new StringFormat();
activeform.setAcost(acost);
activeform.setAcity(sf.splitstr(StringFormat.notNull(acity)));
activeform.setAcount(acount);
activeform.setAdate(sf.splitstr(StringFormat.notNull(adate)));
activeform.setAlevel(alevel);
activeform.setAstate(astate);
activeform.setAttitle(sf.splitstr(StringFormat.notNull(attitle)));
ActiveDao ad = new ActiveDao();
ArrayList list = ad.selectall(activeform);
if(list.size()>0){
ActiveForm af = (ActiveForm) list.get(0);
request.getSession().setAttribute("zongshu", af.getZongshu());
request.getSession().setAttribute("list", list);
// request.setAttribute("zongshu", af.getZongshu());
// request.setAttribute("list", list);
return mapping.findForward("yes");
}else{
request.getSession().setAttribute("zongshu", "0");
list = new ArrayList();
request.getSession().setAttribute("list",list);
return mapping.findForward("yes");
}
}
jsp内容(涉及到保密,部分代码)
<%
String zongshu;
if (session.getAttribute("zongshu") != null) {
zongshu = (String)session.getAttribute("zongshu");
} else {
zongshu = "0";
}
%>
<div id="h-main-right">
<form id="form1" name="form1" method="post" action="<%=request.getContextPath()%>/ActiveAction.do?activetype=activeselect">
<div id="h-right-top">
<table width="100%" height="30" border="0" cellpadding="0"
cellspacing="0">
<tr>
<td width="40%">
当前共有
<%=zongshu%>
个会员活动
</td>
<td width="60%">
关键字
<label>
<input name="abiaoti" type="text" size="12" class="h-biao" />
城市
<input name="acity" type="text" size="12" class="h-biao" />
<select name="alevel">
<option value="100">
不限等级
</option>
<option value="0">
黑钻活动
</option>
<option value="1">
其他钻活动
</option>
</select>
<input type="submit" name="Submit" value="搜索活动" id="h-anniu" />
</label>
</td>
</tr>
</table>
</div>
<div id="h-right-bottom">
<table width="100%" height="80" border="0" cellpadding="0"
cellspacing="0">
<tr>
<td width="35%" height="25" bgcolor="#F1F5FA">
<span class="STYLE2">主题</span>
</td>
<td width="10%" height="25" bgcolor="#F1F5FA">
<span class="STYLE2">报名费</span>
</td>
<td width="15%" height="25" bgcolor="#F1F5FA">
<span class="STYLE2">活动城市</span>
</td>
<td width="6%" height="25" bgcolor="#F1F5FA">
<span class="STYLE2">人气</span>
</td>
<td width="12%" height="25" bgcolor="#F1F5FA">
<span class="STYLE2">发布日期</span>
</td>
<td width="10%" height="25" bgcolor="#F1F5FA">
<span class="STYLE2">报名权限</span>
</td>
<td width="12%" height="25" bgcolor="#F1F5FA">
<span class="STYLE2">活动状态</span>
</td>
</tr>
<%
ArrayList list = new ArrayList();
if (session.getAttribute("list") != null) {
list = (ArrayList) session.getAttribute("list");
}else{
ActiveDao ad = new ActiveDao();
list = ad.se();
}
if (list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
ActiveForm af = (ActiveForm) list.get(i);
%>
<tr>
<td width="35%" height="25"><a href="">
<%=af.getAbiaoti()%></a>
</td>
<td width="10%" height="25">
<%=af.getAcost()%>
</td>
<td height="25">
<%=af.getAcity()%>
</td>
<td width="6%" height="25">
<%=af.getAcount()%>
</td>
<td width="12%" height="25">
<%=af.getAdate()%>
</td>
<% if(af.getAlevel()==0){%>
<td width="10%" height="25">
<img src="<%=request.getContextPath()%>/images/yes-images/activity-02.jpg" alt="黑钻"/>
</td>
<%} %>
<td width="12%" height="25">
<%
if (af.getAstate() == 0) {
%>
已过期
<%
} else {
%>
正在举办
<%
}
%>
</td>
</tr>
<%
}
}
%>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询