• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

和同事写了一个下午的递归读取数据库的javabean

发布: 2007-7-01 18:47 | 作者: admin | 来源: | 查看: 12次 | 进入软件测试论坛讨论

领测软件测试网
/* * 创建日期 2005-4-14 * * TODO 要更改此生成的文件的模板,请转至 * 窗口 - 首选项 - Java - 代码样式 - 代码模板 */package treeMenu;import java.util.*;import java.sql.*;import dbmanager.DBConnection;import javax.servlet.jsp.JspWriter;/** * @author 呆猴 lucky * * TODO 要更改此生成的类型注释的模板,请转至 * 窗口 - 首选项 - Java - 代码样式 - 代码模板 */public class Tree {    private DBConnection conn;    public ArrayList arrayid;//定义包含所有id的数组    public ArrayList arrayname;//定义包含所有名称的数组    public ArrayList arrayparent;//定义包含所有父id的数组    public ArrayList class1_id;//定义包含所有一级信息id的数组    public Tree(){    try{      conn=new DBConnection();    }    catch(Exception e){      System.out.println("sorry");    }    }    /**     *      * 定义读取所有相关记录和一级信息的方法     */    public void buidTreeinit() throws SQLException{    ResultSet rs=conn.runRs("select * from sp_sys_menu_item");    String aa="";    String id="";    String name="";    String parent_id="";    int i=0;    arrayid=new ArrayList();    arrayname=new ArrayList();    arrayparent=new ArrayList();    class1_id=new ArrayList();     while(rs.next()){     id=rs.getString("id");     name=rs.getString("name");     parent_id=rs.getString("parent_id");     arrayid.add(id);//把所有id信息赋值到arrayid数组中     arrayname.add(name);//把所有name信息赋值到arrayname数组中     arrayparent.add(parent_id);//把所有parent_id信息赋值到arrayparent数组中     /**      * 把所有的一级信息赋值到数组class1_id中      */     if(parent_id.equals("0"))      {      class1_id.add(id);      }    }    conn.free();    }    /**     * 开始定义树型结构的构造     * @param parentid     * @throws SQLException     */    public void buildTree(JspWriter out,String parentid,int j) throws Exception{        j++;   ArrayList tmplist=new ArrayList();//包含所有父id为parent_id的记录的名称数组   String mmm="&nbsp;&nbsp;&nbsp;&nbsp;";   String nnn="|";   for(int q=0;q<j;q++){      nnn=nnn+"--";      mmm=mmm+"&nbsp;&nbsp;&nbsp;&nbsp;";   }      String table2="";      table2=table2+"<tr bgcolor=\"#FFFFFF\">";      table2=table2+"<td width=\"70%\" height=\"30\">"+mmm+nnn+" <name></td>";      table2=table2+"<td width=\"30%\" height=\"30\" align=\"center\">";      table2=table2+"修改&nbsp;&nbsp;";      table2=table2+"注册下级菜单&nbsp;&nbsp;";      table2=table2+"&nbsp;&nbsp;<del>";      table2=table2+"</td>";      table2=table2+"</tr>";   while(arrayparent.indexOf(parentid)!=-1)   {     String tmpname=(String)arrayname.get(arrayparent.indexOf(parentid));//获取所有父id为parent_id的记录的名称     String tmpid=(String)arrayid.get(arrayparent.indexOf(parentid));//获取该子信息的id,用于赋予下级子信息的父id     if(has_child(tmpid)){     out.print(table2.replaceAll("<name>",tmpname).replaceAll("<del>",""));     }     else{     out.print(table2.replaceAll("<name>",tmpname).replaceAll("<del>","删除"));      }     int tmp=arrayparent.indexOf(parentid);//获取参数parent_id所在位置     arrayparent.remove(tmp);//删除参数parent_id所在位置的parent_id     arrayid.remove(tmp);//删除参数parent_id所在位置的id     arrayname.remove(tmp);//删除参数parent_id所在位置name     if(has_child(tmpid))//如果该条信息有相关子信息重新执行buildTree方法       {            buildTree(out,tmpid,j);       }        }     }    /**     * 进行是否有子信息判断     * @param parentid     * @return     */    public boolean has_child(String parentid)    {     boolean bb=false;         if(arrayparent.indexOf(parentid)!=-1)     {      

      bb=true;          }     return bb;    }    /**     * 树型结构显示     * @param args     * @throws Exception     */    public void showTree(JspWriter out) throws Exception{     Tree aa=new Tree();        aa.buidTreeinit();        String table1="";        table1=table1+"<tr bgcolor=\"#CCCCCC\">";        table1=table1+"<td width=\"70%\" height=\"30\">&nbsp;&nbsp;<name></td>";      table1=table1+"<td width=\"30%\" height=\"30\" align=@#center@#>";      table1=table1+"修改&nbsp;&nbsp;";      table1=table1+"注册下级菜单&nbsp;&nbsp;";      table1=table1+"&nbsp;&nbsp;<del>";      table1=table1+"</td>";        table1=table1+"</tr>";        for(int i=0;i<aa.class1_id.size();i++)        {         if(aa.has_child((String)aa.class1_id.get(i))){         out.print(table1.replaceAll("<name>",(String)aa.arrayname.get(i)).replaceAll("<del>",""));         }         else{         out.print(table1.replaceAll("<name>",(String)aa.arrayname.get(i)).replaceAll("<del>","删除"));          }         aa.buildTree(out,(String)aa.class1_id.get(i),0);        }    }}

数据库连接和数据库操作类:

/* * 创建日期 2005-4-14 * * TODO 要更改此生成的文件的模板,请转至 * 窗口 - 首选项 - Java - 代码样式 - 代码模板 */package dbmanager;import java.sql.*;/** * @author Administrator * * TODO 要更改此生成的类型注释的模板,请转至 * 窗口 - 首选项 - Java - 代码样式 - 代码模板 */public class DBConnection { String user="sa"; String password="sa"; String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver"; String sConnStr="jdbc:microsoft:sqlserver://172.16.204.10:1433;DatabaseName=HLSP_MIS"; Connection connect=null; ResultSet rs=null; Statement stmt = null, stmt1 = null; public DBConnection(){  try{   Class.forName(sDBDriver);   connect = DriverManager.getConnection(sConnStr,user,password);   stmt = connect.createStatement();   stmt1=connect.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);

  }  catch(Exception e){      System.err.println(e.getMessage());  } } //执行数据库查询 public ResultSet runRs(String sql){ rs=null; try{ rs=stmt1.executeQuery(sql); } catch(SQLException ex){ System.err.println(ex.getMessage()); } return rs; } //执行数据库其他操作 public  boolean  executeSql(String sql) { boolean che=false; try { stmt.executeUpdate(sql); che=true; } catch (SQLException ex) { System.err.println(ex.getMessage()); che=false; } return che; } //进行数据库连接释放 public boolean free(){ try {  connect.close();  if (stmt!=null){   stmt.close();      }  if(stmt1!=null){       stmt1.close();      }  return true;  }  catch (Exception e) {  return false;  }

 }}

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网