public static ArrayList searchSort() { int sortid; ResultSet rst_sort= null; ResultSet rst_master= null; ResultSet rst_topicNum = null; ResultSet rst_responseNum = null; ResultSet rst_topic = null; Statement stmt_sort = null; PreparedStatement stmt_master = null; PreparedStatement stmt_topicNum = null; PreparedStatement stmt_responseNum = null; PreparedStatement stmt_topic = null; Connection connection = DBConnection.createConnection();// 获取连接 ArrayList result = new ArrayList(); try { stmt_sort = connection.createStatement(); //查询论坛的详细信息 rst_sort= stmt_sort.executeQuery("select * from bbssort"); //查询论坛版主名称 stmt_master = connection.prepareStatement("select master from bbsmaster where sortid = ?"); //查询每个论坛的主题总数 stmt_topicNum = connection.prepareStatement("select count(*) from bbstopic where sortid = ?"); //查询每个论坛回帖总数 stmt_responseNum = connection.prepareStatement("select count(*) from bbsresponse where sortid = ?"); //查询每个论坛的最新主题信息 stmt_topic = connection.prepareStatement("select * from bbstopic where sortid = ? order by topictime desc"); while (rst_sort.next()) { Sort sort = new Sort(); //构造论坛分类信息模型对象 sortid = rst_sort.getInt("sortid");// 得到论坛分类id sort.setId(sortid);//获得论坛id编号 sort.setName(rst_sort.getString("sortname"));// 获得论坛分类名称 /** <1> **/ stmt_master.setInt(1, sortid);// 执行第一个预编译 sql查询论坛版主名称 rst_master = stmt_master.executeQuery(); if( rst_master.next() ) sort.setMaster(rst_master.getString("master"));// 获得论坛版主名称 /** <2> **/ stmt_topicNum.setInt(1, sortid);// 执行第二个预编译 sql查询论坛讨论区中文章的数量 rst_topicNum = stmt_topicNum.executeQuery(); if (rst_topicNum.next()) sort.setTopicNum(rst_topicNum.getInt(1)); /** <3> **/ stmt_responseNum.setInt(1, sortid);// 执行第三个预编译sql 查询每个论坛回帖的总数 rst_responseNum = stmt_responseNum.executeQuery(); if( rst_responseNum.next() ) sort.setResponseNum(rst_responseNum.getInt(1)); /** <4> **/ stmt_topic.setInt(1, sortid);// 执行第四个预编译sql 查询每篇文章的基本信息 rst_topic = stmt_topic.executeQuery(); if (rst_topic.next()) { sort.setLastTopicId(rst_topic.getInt("topicid"));//获得最新发表的主题ID编号 sort.setLastTopic(rst_topic.getString("topicname"));//获得最新发表的主题名称 sort.setLastTopicOwner(rst_topic.getString("topicowner"));//获得主题作者 //获得最新发表时间 sort.setLastTopicTime(DateFormat.getDateTimeInstance().format(rst_topic.getDate("topictime"))); } result.add(sort);//论坛模型对象放入自动增长数组中 } return result; } catch (SQLException e) { e.printStackTrace(); return null; } finally { //关闭查询论坛分类的记录集 DBConnection.releaseResultSet(rst_sort); DBConnection.releaseStatement(stmt_sort); //关闭查询论坛版主的记录集 DBConnection.releaseResultSet(rst_master); DBConnection.releaseStatement(stmt_master); //关闭查询主题总数的记录集 DBConnection.releaseResultSet(rst_topicNum); DBConnection.releaseStatement(stmt_topicNum); //关闭查询回帖总数的记录集 DBConnection.releaseResultSet(rst_responseNum); DBConnection.releaseStatement(stmt_responseNum); //关闭查询最新发表主题信息的记录集 DBConnection.releaseResultSet(rst_topic); DBConnection.releaseStatement(stmt_topic); //关闭数据库连接,回收连接池 DBConnection.releaseConnection(connection); } } <p class="indent">