我用arraylist,代码太长,不好贴

解决方案 »

  1.   

    my email : [email protected]
    remanwang(玩玩儿) 帮忙啊
      

  2.   

    我先把结果组装好放进对象
    在把对象放进arraylist
      

  3.   

    1.如果已知result长度,用array比较好。反之,如果结果空间不定,用vector比较合理。
    2.Vector的内存开销比array要大得多。
      

  4.   

    贴个简单的吧 public class DocAttch
     {
     
      protected Integer _ID ;
      protected Integer _DOCID ;
      protected String _FILENAME ;
      protected String _REALNAME ;
      public Integer get_ID()
      {
        return this._ID;
      }
      public void set_ID(Integer a_strID)
      {
        this._ID=a_strID;
      }
      public Integer get_DOCID()
      {
        return this._DOCID;
      }
      public void set_DOCID(Integer a_strDOCID)
      {
        this._DOCID=a_strDOCID;
      }
      public String get_FILENAME()
      {
        return this._FILENAME;
      }
      public void set_FILENAME(String a_strFILENAME)
      {
        this._FILENAME=a_strFILENAME;
      }
      public String get_REALNAME()
      {
        return this._REALNAME;
      }
      public void set_REALNAME(String a_strREALNAME)
      {
        this._REALNAME=a_strREALNAME;
      } 
       /****************************************************************
        * 构造函数
        ****************************************************************/
       public DocAttch()
       {    this._ID= null;
        this._DOCID= null;
        this._FILENAME= null;
        this._REALNAME= null;
       }
    }
      

  5.   

    public class DocAttchImpl extends DocAttch
    {    public DocAttchImpl()
        {
        }    public void settxtid(String s)
        {
          Integer si;
          if(s != null)
          {
            si=new Integer(s);
            this.set_ID(si);
          }
        }
        public void settxtdocid(String s)
        {
          Integer si;
          if(s != null)
          {
            si=new Integer(s);
            this.set_DOCID(si);
          }
        }
        public void settxtfilename(String s)
        {
          if(s != null)
            this.set_FILENAME(s);
        }
        public void settxtrealname(String s)
        {
          if(s != null)
            this.set_REALNAME(s);
        }  public boolean Create() throws Exception
      {
          int i=0;
          java.sql.Connection myConn = null; 
          java.sql.Statement st = null;      String strSql = "insert into [doc_attch]([DOCID],[FILENAME],[REALNAME]) values(";
          if(this.get_DOCID()!=null)  strSql=strSql + this.get_DOCID().toString()  + ",";
            else  strSql=strSql + "null" + ",";
          if(this.get_FILENAME()!=null)  strSql=strSql + "'" + this.ToSql(this.get_FILENAME()) + "'" + ",";
            else  strSql=strSql + "null" + ",";
          if(this.get_REALNAME()!=null)  strSql=strSql + "'" + this.ToSql(this.get_REALNAME()) + "'" + ")";
            else  strSql=strSql + "null" + ")";      try
          {
            myConn = DbConnectionManager.getConnection() ;
            st = myConn.createStatement() ;
            i=st.executeUpdate(strSql);
          }
          catch(Exception e)
          {
          }
          finally
          {
            if(st!=null)  st.close();
            if(myConn!=null)  myConn.close();
          }
          if(i>0){return true;}else{return false;}
      }
      public boolean Delete() throws Exception
      {
          java.sql.Connection myConn = null; 
          java.sql.Statement st = null;
          int i=0;    String strSql = "delete from [doc_attch] where [id]="  + this.get_ID().toString() ;      cat.info(strSql);
          try
          {
            myConn = DbConnectionManager.getConnection() ;
            st = myConn.createStatement() ;
            i=st.executeUpdate(strSql);
          }
          catch(Exception e)
          {
            i=-1;
          }
          finally
          {
            if(st!=null)  st.close();
            if(myConn!=null)  myConn.close();
          }
          if(i>=0){return true;}else{return false;}
      }  public boolean Save() throws Exception
      {
          java.sql.Connection myConn = null; 
          java.sql.Statement st = null;
          int i=0;      String strSql = "update [doc_attch] set ";
          strSql=strSql + "docid=";
          if(this.get_DOCID()!=null)  strSql=strSql + this.get_DOCID().toString()  + ",";
            else strSql=strSql + "null" + ",";
          strSql=strSql + "filename=";
          if(this.get_FILENAME()!=null)  strSql=strSql + "'" + this.ToSql(this.get_FILENAME()) + "'" + ",";
            else strSql=strSql + "null" + ",";
          strSql=strSql + "realname=";
          if(this.get_REALNAME()!=null)  strSql=strSql + "'" + this.ToSql(this.get_REALNAME()) + "'";
            else strSql=strSql + "null";
        strSql = strSql + " where [id]= "  + this.get_ID().toString() ;
          try
          {
            myConn = DbConnectionManager.getConnection() ;
            st = myConn.createStatement() ;
            i=st.executeUpdate(strSql);
          }
          catch(Exception e)
          {
          }
          finally
          {
            if(st!=null)  st.close();
            if(myConn!=null)  myConn.close();
          }
          if(i>0){return true;}else{return false;}
      }
      

  6.   

    public boolean Load(int a_intid) throws Exception
      {
        java.sql.Connection myConn = null;
        java.sql.Statement st = null;
        java.sql.ResultSet rs = null;    int i=0;
        boolean flag=false;
        int itmp;
        double dtmp;
        String stmp;
        java.util.Date rtmp;
        
        String strSql = "select * from [doc_attch] where [id]=";
        strSql=strSql + Integer.toString(a_intid);
        try
        {
          myConn = DbConnectionManager.getConnection() ;
          st = myConn.createStatement();
          rs = st.executeQuery(strSql) ;
          if(rs.next())
          {          itmp=rs.getInt("ID");
              if(!rs.wasNull()) this._ID=new Integer(rs.getInt("ID"));
              itmp=rs.getInt("DOCID");
              if(!rs.wasNull()) this._DOCID=new Integer(rs.getInt("DOCID"));
              stmp=rs.getString("FILENAME");
              if(!rs.wasNull()) this._FILENAME=theTrim(rs.getString("FILENAME"));
              stmp=rs.getString("REALNAME");
              if(!rs.wasNull()) this._REALNAME=theTrim(rs.getString("REALNAME"));
              flag=true;
          }
        }
        catch(Exception e)
        {
        }
        finally
        {
          rs=null;
          if(st!=null)  st.close();
          if(myConn!=null)  myConn.close();
        }
        return flag ;
      }  public ArrayList LoadByPage(int PageNumber,int PageSize,String Condition) throws Exception
      {
          java.sql.Connection myConn = null; 
          java.sql.Statement st = null;
          java.sql.ResultSet rs = null;
          ArrayList al=new ArrayList();
          int i;
          int itmp;
          double dtmp;
          String stmp;
          java.util.Date rtmp;
          
        String strSql = "select id,doc_no,script_no,title,createdate from [doc_attch] ";        try
        {
          myConn = DbConnectionManager.getConnection() ;
          st = myConn.createStatement(1004,1007);
          if(!Condition.equals(""))
          {
            Condition=" where " + Condition;
          }
          
          rs = st.executeQuery(strSql + Condition) ;
          rs.absolute((PageNumber-1)*PageSize+1);
          i=0;
          while(i<PageSize && !rs.isAfterLast())
          {
            DocAttch fc=new DocAttch();          itmp=rs.getInt("ID");
              if(!rs.wasNull()) fc.set_ID(new Integer(rs.getInt("ID")));
            al.add(fc);
            rs.next();
            i++;
          }
        }
        catch(Exception e)
        {
          al=null;
        }
        finally
        {
          rs=null;
          if(st!=null)  st.close();
          if(myConn!=null)  myConn.close();
        }
        return al ;
      }  public ArrayList LoadAll(String Condition) throws Exception
      {
          java.sql.Connection myConn = null; 
          ArrayList al=new ArrayList();
        try
        {
          myConn = DbConnectionManager.getConnection() ;
          al=LoadAll(Condition,myConn);
        }
        catch(Exception e)
        {
          al=null;
        }
        finally
        {
          if(myConn!=null)  myConn.close();
        }
        return al ;
      }  public ArrayList LoadAll(String Condition,java.sql.Connection myConn) throws Exception
      {
          java.sql.Statement st = null;
          java.sql.ResultSet rs = null;
          ArrayList al=new ArrayList();
          int i;
          int itmp;
          double dtmp;
          String stmp;
          java.util.Date rtmp;
          
        String strSql = "select * from [doc_attch]";        try
        {
          st = myConn.createStatement(1004,1007);
          if(!Condition.equals(""))
          {
            Condition=" where " + Condition;
          }
          
          rs = st.executeQuery(strSql + Condition) ;
          i=0;
          while(!rs.isAfterLast())
          {
            DocAttch fc=new DocAttch();          itmp=rs.getInt("ID");
              if(!rs.wasNull()) fc.set_ID(new Integer(rs.getInt("ID")));
              itmp=rs.getInt("DOCID");
              if(!rs.wasNull()) fc.set_DOCID(new Integer(rs.getInt("DOCID")));
              stmp=rs.getString("FILENAME");
              if(!rs.wasNull()) fc.set_FILENAME(theTrim(rs.getString("FILENAME")));
              stmp=rs.getString("REALNAME");
              if(!rs.wasNull()) fc.set_REALNAME(theTrim(rs.getString("REALNAME")));
              al.add(fc);
              rs.next();
          }
        }
        catch(Exception e)
        {
          al=null;
        }
        finally
        {
          rs=null;
          if(st!=null)  st.close();
        }
        return al ;
      }  public ArrayList LoadAll(String a_strFieldName,int a_intFID) throws Exception
      {
        Integer itmp=new Integer(a_intFID);
        String strSql=a_strFieldName + "=" + itmp.toString();
        return LoadAll(strSql) ;
      }
      
      public int Count() throws Exception
      {
        java.sql.Connection myConn = null;
        java.sql.Statement st = null;
        java.sql.ResultSet rs = null;
        int fc=0;    try
        {
          myConn = DbConnectionManager.getConnection() ;
          st = myConn.createStatement() ;
          rs = st.executeQuery("select count(*) as doc_attchCount from [doc_attch] ");
          if(rs.next())
          {
            fc=rs.getInt("doc_attchCount");
          }
        }
        catch(Exception e)
        {
          fc=-1;
        }
        finally
        {
          rs=null;
          if(st!=null)  st.close();
          if(myConn!=null)  myConn.close();
        }
        return fc ;
      }  private String ToSql(String a_strSql)
        {
            return StringUtils.replace(a_strSql, "'", "''");
        }
      private String theTrim(String a_strSql)
        {
            return a_strSql.trim();
        }}
      

  7.   

    其实都可以,看你自己的习惯了。在我周围使用ArrayList得多一些。但是也是先组装成一个Vector实例,然后返回他的Enumeration.请看下面的一个ejbFind的例子:
    //FilesBean.java
    ....
    public Enumeration ejbFindByTitle(String title,int order) throws FinderException,RemoteException{
        //通用数据库连接
        PreparedStatement pstmt=null;
        Connection conn=null;
        DataSource ds = null;
        Vector v=new Vector();
        try{
    //System.out.println("ejbFindByTitle("+title+")方法被调用。");
    //System.out.println("begin to connect the database...");
    /*
              * 获得数据库连接
              */
              Context ic = new InitialContext();
    ds = (DataSource)ic.lookup("jdbc/webbos");
    conn = ds.getConnection();
    /*
    * 在数据库中查找这个标题
    */
    //System.out.println("begin scaning...");
    if(order==EInclude.ORDERBYNULL)
    pstmt=conn.prepareStatement("select fileID from file where title like ?");
    if(order==EInclude.ORDERBYAUTHOR)//order by author
    pstmt=conn.prepareStatement("select fileID from file where title like ? order by author");
    if(order==EInclude.ORDERBYUPLOADDATE)//order by uploadDate;
    pstmt=conn.prepareStatement("select fileID from file where title like ? order by uploadDate"); 
    if(order==EInclude.ORDERBYSUBJECTID)//order by subjectID
    pstmt=conn.prepareStatement("select fileID from file where title like ? order by subjectID");
    pstmt.setString(1,"%"+title+"%");
    //System.out.println("executing...");
    ResultSet rs=pstmt.executeQuery();
    //System.out.println("finish data scaning...");
    /*
    *插入每一个找到的主键到一个向量中
    */
    while(rs.next()){
    int fileID=rs.getInt("fileID");
    v.addElement(new FilesPK(fileID));
    }
    /*
    * 返回一个找到的主键的列举
    */
    return v.elements();
        }
        catch(Exception e){
    throw new FinderException(e.toString());
        }
        finally{
    /*
    * 为其他Bean释放数据库连接
    */
    try{
    if(pstmt!=null) pstmt.close();
    }
    catch(Exception e){}
    try{
    if(conn!=null) conn.close();
    }
    catch(Exception e){}
        }
    }
      

  8.   

    看来你是要写BMP吧。我认为可以另外加一个表A和表B,A里面存放目前所有的表名,B记录每一表的字段类型,名称,是否可以空,主键等等。
    然后对表结构修改的时候就对这两个表同时修改,保证这两个表记录的是当前你的表结构。
    从数据库读写的时候,首先从这两个表来确定读什么字段,怎么读。
      

  9.   

    那就用变常数组Vector呗,要是不知道字段多少可以Vector 套Vector 呗。
      

  10.   

    /** 将查询数据库获得的结果集转化为 二维数组 返回<br>
         * 利用 ResultSetMetaData 获得结果集中的字段数目 k=ResultSetMetaData.getColumnCount();<br>
         * 定义vector[k]; 将每个字段的结果集放入 vector[k]中。<br>
         * 定义二维数组String[k][v[0].size]; 将vector[k] copyinto 到相应的二维数组中<br>
         * 返回的二维数组为:String【i】【j】  i=字段数目; j=单个字段的记录条数<br>
         * @param rs ResultSet 作为参数<br>
         * @return<br>
         */
        public String[][] resultTwoArray(ResultSet rs)
        {
            try
            {
                ResultSetMetaData rsmd=rs.getMetaData();
                int k=rsmd.getColumnCount();                //  取出字段数目
    System.out.println("字段返回类型=|"+rsmd.getColumnTypeName(1)+"|");
                System.out.println("########查询字段数目k="+k);
                if(k<1)
                {   String[][] nul=null;
                    return nul;
                }
                Vector v[]=new Vector[k];
                for(int i=0;i<k;i++)
                    v[i]=new Vector();            while(rs.next())
                {   for(int i=0;i<k;i++)
                      v[i].addElement(rs.getObject(i+1));
                }            if(v[0]==null||v[0].size()==0)
                {   String[][] nul2=null;
                    return nul2;
                }
                String[][] arr=new String[k][v[0].size()];
                for(int p=0;p<k;p++)
                    v[p].copyInto(arr[p]);                  //  将 Vector[p] copy 到数组 arr[p]中            //******************** null will be replaced with "" *************//
                for(int i=0;i<arr.length;i++)
                {   for(int j=0;j<arr[0].length;j++)
                        if(arr[i][j]==null)
                            arr[i][j]="";
                }
                return arr;
            }
            catch(Exception e)
            {   e.printStackTrace();
            }
            return null;
        }
      

  11.   

    tang1980(与同仁共勉之)  真不错哦,又快又省内存
    我想用arraylist + string[col] 都不如你这个返回数组的好吧
    对不起,java语法不太熟,那个vector 用不用在程序里释放掉?
      

  12.   

    vector就象 数组差不多,
    在java中,资源是会自动回收的,这些是不用客户担心的。IF YOU HAVE ANY QUESTION ,YOU CAN CONTACT ME WITH E-MAIL:[email protected]