struts1.x中如何从数据库中取出数据并显示到JSP上
用户发出showinfo.do请求,ShowAction接收请求,调用ShowInfoBean从数据库中取出数据然后显示到JSP上,这个过程应该如何实现呢,我是struts的初学者
我的代码是这样的,可是最后还是打印不出数据库中的数据
//ShowAction.java
//action类获取用户的请求、查询数据库信息
package info.action;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import info.bean.*;
import java.io.*;
import java.util.*;
public class ShowAction extends Action
{
protected ArrayList getInfos(){
PreparedStatement prst=null;
ResultSet rs=null;
Connection con=null;
DBConnection dbcon=new DBConnection();
Info info=null;
ArrayList infos=new ArrayList();
//下面的id、job都是在登陆的时候存进session的
String id=(String)request.getSession().getAttribute("id");
String job=(String)request.getSession().getAttribute("job");
dbcon.setSql(job);
try{
con=dbcon.getConnection();
prst=con.prepareStatement(dbcon.getSql());
prst.setString(1,id);
rs=prst.executeQuery();
while(rs.next()){
info=new Info();
info.setId(rs.getString(1));
info.setName(rs.getString(2));
info.setSex(rs.getString(3));
info.setAge(rs.getInt(4));
info.setMail(rs.getString(5));
info.setPost(rs.getString(6));
info.setAdress(rs.getString(7));
info.setPhone(rs.getString(8));
infos.add(info);
}
}
catch (
SQLException e)
{}finally{
dbcon.closedb(rs,prst,con);
}
return infos;
}
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)
throws
IOException,
ServletException {
return mapping.findForward("myinfo");
}
}
//Info.java
package info.vo;
public class Info
{
private String id;
private String name;
private String sex;
private int age;
private String mail;
private String post;
private String phone;
private String adress;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
}
//ShowInfo.jsp用来显示取出的数据
<%@ page language="java" contentType="text/html; charset=gb2312"
import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib uri="../WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="../WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="../WEB-INF/struts-html.tld" prefix="html"