把数据库里的东西添加到arraylist里

来源:互联网 发布:python拆分中文字符串 编辑:程序博客网 时间:2024/06/10 07:01

一直以来,我都是把db里的东西添加到vector里然后返回给前台使用,今天在网上看到一个放到list里去的代码,特意记录在这里,以备将来使用

把数据库里的东西添加到arraylist里(片断)
public ArrayList cltExecuteQuery(String script) throws SQLException {
                Object obj;
                ArrayList list = new ArrayList();
                Object[][] objRowCol = null;
                ResultSetMetaData meta = null;

                m_Rows = 0;

                try {
                        if (m_conn == null || m_conn.isClosed()) { // クローズしていたらDB接続を取得
                                m_conn = this.dbConnection();
                        }
                } catch (SQLException e) {
                        throw e;
                }

                //-------------------------------------------------------
                // ステートメント・インスタンスを生成する.
                //-------------------------------------------------------
                try {
                        m_st = m_conn.createStatement();
                } catch (SQLException e) {
                        // 例外を呼び出し元にThrowする.
                        throw e;
                }

                //                -------------------------------------------------------
                // SQLを実行する.
                //-------------------------------------------------------
                try {
                        //SQL SELECTを実行
                        m_rs = m_st.executeQuery(script);
                        meta = m_rs.getMetaData();
                        //コラム数を取得
                        m_Cols = meta.getColumnCount();
                        //レコードを取得
                        while (m_rs.next()) {
                                CCodeName rscol = new CCodeName();
                                m_Rows++;
                                rscol.setCode(m_rs.getObject(1).toString());
                               
                                rscol.setName(m_rs.getObject(2).toString());

                                list.add(rscol);
                               
                        }
                       

                } catch (SQLException e) {
                        // 例外を呼び出し元にThrowする.
                        throw e;
                } finally {
                        try {
                                // close the ResultSet object using the close() method
                                if (m_rs != null) {
                                        m_rs.close();
                                }
                                // close the Statement object using the close() method
                                if (m_st != null) {
                                        m_st.close();
                                }
                                // close the Connection object using the close() method
                                if (m_conn != null) {
                                        m_conn.close();
                                }
                        } catch (SQLException e) {
                                throw e;
                        }

                }
                return list;
        }
 

原创粉丝点击