最简单的方法,还是将类当作一个javabean来调用

解决方案 »

  1.   

    1、Struts自己带的例子是最典型的做法,当点击表格中一行的EDIT时,转到编辑页面编辑这条记录。2、也可以看http://dev.csdn.net/user/kui中《如何在Struts 数据库应用程序中实现记录的删除、更新及链接》这个例子。
      

  2.   

    class Tomuno{
    private String name="tomuno";
    private String age="100";
    private String id="8888";
    public String getId(){
     return this.id;}
    public String getName(){
     return this.name;}
    public String getAge(){
     return this.age;}
    }
    public class Smalldeer extends Action{
    ....
         Tomuno tomuno=new Tomuno();
         String id=(String)request.getParameter("id");
         String mapping=null; 
         String name;
         String age;      if(id.equals(tomuno.getId())){
            mapping="success";
            name = tomuno.getName();
            age = tomuno.getAge();
            request.getSession.setAttribute("name",name);
            request.getSession.setAttribute("age",age);      
    }
          else{mapping="fail";}     return actionmapping.findForward(mapping);
    }struts-config.xml
    <action path="/tomuno" type="Tomuno">
    <forward name="success" path="/smalldeer.jsp">
    <forward name="fail" path="http://www.tom.com">
    </action>tomuno.jsptomuno.do?id=8888smalldeer.jsp
    <font color="blue">
    My &nbsp;&nbsp; name &nbsp;&nbsp; is:
    <%=session.getAttribute("name")%>
    <br>
    and &nbsp;my &nbsp;age &nbsp; is;
    <%=session.getAttribute("age")%>
    </font>
      

  3.   

    从数据库里面取出这条记录,封装到Bean里面。放到页面显示~
    首先生成关于这条记录的简单Bean;
    jb和Eclipse都有自动生成的功能;例如:public class MagModuleBean implements Serializable {
      private String moduleCode;
      private String moduleName;
      private String levelCode;
      private String isShow;
      public String getModuleCode() {
        return moduleCode;
      }
      public void setModuleCode(String moduleCode) {
        this.moduleCode = moduleCode;
      }
      public String getModuleName() {
        return moduleName;
      }
      public void setModuleName(String moduleName) {
        this.moduleName = moduleName;
      }
      public String getLevelCode() {
        return levelCode;
      }
      public void setLevelCode(String levelCode) {
        this.levelCode = levelCode;
      }
      public String getIsShow() {
        return isShow;
      }
      public void setIsShow(String isShow) {
        this.isShow = isShow;
      }
      private void writeObject(ObjectOutputStream oos) throws IOException {
        oos.defaultWriteObject();
      }
      private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
        ois.defaultReadObject();
      }
    如果你要显示多条记录,那把bean都放在ArrayList里面不是更好么~
    解决的方法是使用类映射机制;
        /////////////////////////////////////////////////////////////////////////////
        //Function: 完成ResultSet对象向ArrayList对象为集合的对象的转化
        //Para:sql,指定的查询Sql
       //Para:className,Sql相对应得JavaBean/FormBean类的名字
       //Return:以类className为一条记录的结果集,完成ResultSet对象向ArrayList对象为集//合的className对象的转化
      //////////////////////////////////////////////////////////////////////////////
      public ArrayList Select(String sql,String className){
        ArrayList paraList=new ArrayList();
        try{
          if (conn == null){
            Connection();
          }
          PreparedStatement stmt = conn.prepareStatement(sql);
          ResultSet rs = stmt.executeQuery();
          String recordValue="";
          Object c1=null;
          paraList=new ArrayList();
          ResultSetMetaData rsmd = rs.getMetaData();
          int columnCount = rsmd.getColumnCount();
          while (rs.next()){
              c1=Class.forName(className).newInstance();
              for (int i=1; i<=columnCount; i++) {
                if(rs.getString(rsmd.getColumnName(i))!=null){
                  recordValue=rs.getString(rsmd.getColumnName(i));
                }else{
                  recordValue="";
                }
    Method 
    m=c1.getClass().getMethod(getSetMethodName(rsmd.getColumnName(i)),
    new Class[]{recordValue.getClass()});
                m.invoke (c1, new Object[]{recordValue});
              }
              paraList.add(c1);
          }
        }catch(SQLException ex){
          
    }catch(ClassNotFoundException e){}catch(NoSuchMethodException e) {}catch(InvocationTargetException e){}catch (IllegalAccessException e){}catch(InstantiationException e){} finaly{
            closeConnection();
    return paraList;
    }
      这个是个简单的例子;里面的用ArrayList封装可按照你的要求封装,但是有个问题也不好处理;
    bean的属性名和数据库里面的字段名称对应的问题!这个还是要注意的。如果你嫌上面写的麻烦,可以到Apache的网站上下载一个类包叫做dbutils 1.0.jar
    他的BasicRowProcessor类有个方法,以下是他的简介:
    toBeanList
    public java.util.List toBeanList(java.sql.ResultSet rs,
                                     java.lang.Class type)
                              throws java.sql.SQLException
    Convert a ResultSet into a List of JavaBeans. This implementation uses reflection and BeanInfo classes to match column names to bean property names. Properties are matched to columns based on several factors: The class has a writable property with the same name as a column. The name comparison is case insensitive. 
    The property's set method parameter type matches the column type. If the data types do not match, the setter will not be called. 
    Primitive bean properties are set to their defaults when SQL NULL is returned from the ResultSet. Numeric fields are set to 0 and booleans are set to false. Object bean properties are set to null when SQL NULL is returned. This is the same behavior as the ResultSet get* methods. 
    Specified by:
    toBeanList in interface RowProcessor
    Returns:
    A List of beans with the given type in the order they were returned by the ResultSet. 
    Throws: 
    java.sql.SQLException
    See Also:
    RowProcessor.toBeanList(java.sql.ResultSet, java.lang.Class)
    看到是否是你想要的呢?其实他封装的操作和上面写的实例原理是一样的。
    好了,希望对楼主有所帮助!