<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 59, 文章 - 4, 評(píng)論 - 184, 引用 - 7
    數(shù)據(jù)加載中……

    [BIRT]-[Tutorial]-使用ScriptDataSet從POJO中獲得數(shù)據(jù)(二)

    配置文件dbconfig.xml如下:
    1<config>
    2    <dbinfo>
    3        <url>jdbc:oracle:thin:@133.1.72.44:1521:test</url>
    4        <user>test</user>
    5        <pwd>test</pwd>
    6        <connNumber>10</connNumber>
    7    </dbinfo>
    8</config>
    9

    這樣數(shù)據(jù)庫(kù)連接類部分就完成了
    下面寫一個(gè)簡(jiǎn)單數(shù)據(jù)訪問(wèn)類,由
    TableRowSetRow三個(gè)類組成:

    Table.java代碼如下

      1package com.bat.afp.DAOComm;
      2
      3import java.sql.Connection;
      4import java.sql.ResultSet;
      5import java.sql.ResultSetMetaData;
      6import java.sql.SQLException;
      7import java.sql.Statement;
      8import org.apache.log4j.Logger;
      9
     10/**
     11 * @author liuyf
     12 */

     13public class Table {
     14
     15    private static final Logger    logger    = Logger.getLogger(Table.class);
     16
     17    private String                tableName;
     18
     19    public Table(String name) {
     20        this.tableName = name;
     21    }

     22
     23    /**
     24     * 根據(jù)條件查出該Table中的某些列
     25     * 
     26     * @param sql
     27     * @param cols
     28     * @return
     29     * @throws SQLException
     30     */

     31    private RowSet executeSQL(String criteria, String[] columns) throws SQLException {
     32        Connection conn = null;
     33        Statement st = null;
     34        ResultSet rs = null;
     35        RowSet rows = new RowSet();
     36        try {
     37            conn = DBUtil.getInstance().getConnection();
     38            st = conn.createStatement();
     39            StringBuffer buffer = new StringBuffer();
     40            for (int i = 0; i < columns.length - 1++i) {
     41                buffer.append(columns[i]);
     42                buffer.append(",");
     43            }

     44            buffer.append(columns[columns.length - 1]);
     45            String column = buffer.toString();
     46            rs = st.executeQuery("select " + column + " from " + tableName
     47                    + (criteria == null ? "" : (" where " + criteria)));
     48            int cols = columns.length;
     49            while (rs.next()) {
     50                Row row = new Row();
     51                for (int i = 0; i < cols; ++i) {
     52                    String name = columns[i];
     53                    String value = rs.getString(i + 1);
     54                    row.put(name, value);
     55                }

     56                rows.add(row);
     57            }

     58        }

     59        finally {
     60            try {
     61                if (st != null)
     62                    st.close();
     63            }
     catch (Exception e) {
     64            }

     65            try {
     66                if (conn != null)
     67                    conn.close();
     68            }
     catch (Exception e) {
     69            }

     70        }

     71        return rows;
     72    }

     73
     74    private RowSet executeSQL(String sql) throws SQLException {
     75        Connection conn = null;
     76        Statement st = null;
     77        ResultSet rs = null;
     78        RowSet rows = new RowSet();
     79        try {
     80            conn = DBUtil.getInstance().getConnection();
     81            st = conn.createStatement();
     82            rs = st.executeQuery(sql);
     83            ResultSetMetaData rsmd = rs.getMetaData();
     84            int cols = rsmd.getColumnCount();
     85            while (rs.next()) {
     86                Row row = new Row();
     87                for (int i = 0; i < cols; ++i) {
     88                    String name = rsmd.getColumnName(i + 1);
     89                    String value = rs.getString(i + 1);
     90                    row.put(name, value);
     91                }

     92                rows.add(row);
     93            }

     94        }

     95        finally {
     96            try {
     97                if (st != null)
     98                    st.close();
     99            }
     catch (Exception e) {
    100            }

    101            try {
    102                if (conn != null)
    103                    conn.close();
    104            }
     catch (Exception e) {
    105            }

    106        }

    107        return rows;
    108    }

    109
    110    private RowSet execute(String criteria) throws SQLException {
    111        Connection conn = null;
    112        Statement st = null;
    113        ResultSet rs = null;
    114        RowSet rows = new RowSet();
    115        try {
    116            conn = DBUtil.getInstance().getConnection();
    117            st = conn.createStatement();
    118            rs = st.executeQuery("select * from " + tableName
    119                    + (criteria == null ? "" : (" where " + criteria)));
    120            ResultSetMetaData rsmd = rs.getMetaData();
    121            int cols = rsmd.getColumnCount();
    122            while (rs.next()) {
    123                Row row = new Row();
    124                for (int i = 0; i < cols; ++i) {
    125                    String name = rsmd.getColumnName(i + 1);
    126                    String value = rs.getString(i + 1);
    127                    row.put(name, value);
    128                }

    129                rows.add(row);
    130            }

    131        }

    132        finally {
    133            try {
    134                if (st != null)
    135                    st.close();
    136            }
     catch (Exception e) {
    137            }

    138            try {
    139                if (conn != null)
    140                    conn.close();
    141            }
     catch (Exception e) {
    142            }

    143        }

    144        return rows;
    145    }

    146
    147    /**
    148     * 根據(jù)條件和給出的列名查詢某些列的值
    149     * 
    150     * @param criteria
    151     * @param columns
    152     * @return
    153     * @throws SQLException
    154     */

    155    public RowSet getRowsOfSomeColumn(String criteria, String[] columns) throws SQLException {
    156        RowSet rs = executeSQL(criteria, columns);
    157        return rs;
    158    }

    159
    160    /**
    161     * 根據(jù)SQL語(yǔ)句查詢并返回行集
    162     * 
    163     * @param sql
    164     * @return
    165     * @throws SQLException
    166     */

    167    public RowSet getRowsBySql(String sql) throws SQLException {
    168        RowSet rs = executeSQL(sql);
    169        return rs;
    170    }

    171
    172    /**
    173     * 根據(jù)查詢條件返回查到的第一行數(shù)據(jù)
    174     * 
    175     * @param criteria
    176     * @return
    177     * @throws SQLException
    178     */

    179    public Row getRow(String criteria) throws SQLException {
    180        RowSet rs = execute(criteria);
    181        return rs.get(0);
    182    }

    183
    184    /**
    185     * 根據(jù)查詢條件返回行集 查詢條件為sql語(yǔ)句中where后的條件 為null則無(wú)條件
    186     * 
    187     * @param criteria
    188     * @return
    189     * @throws SQLException
    190     */

    191    public RowSet getRows(String criteria) throws SQLException {
    192        RowSet rs = execute(criteria);
    193        return rs;
    194    }

    195
    196    public RowSet getRows(String columnName, String columnvalue) throws SQLException {
    197        if(columnName == null || columnName.equals("")){
    198            return null;
    199        }

    200        String SQL = "select * from " + tableName + " where " + columnName + " = " + columnvalue;
    201        RowSet rs = executeSQL(SQL);
    202        return rs;
    203        
    204    }

    205
    206    /**
    207     * 查詢?cè)揟able的所有數(shù)據(jù)
    208     * 
    209     * @return
    210     * @throws SQLException
    211     */

    212    public RowSet getRows() throws SQLException {
    213        return getRows(null);
    214    }

    215
    216    /**
    217     * 插入行
    218     * 
    219     * @param row
    220     * @throws SQLException
    221     */

    222    public void putRow(Row row) throws SQLException {
    223        putRow(row, null);
    224    }

    225
    226    /**
    227     * 插入行或更新行 如conditions為null,則為插入 如conditions不為null,則為更新
    228     * 
    229     * @param row
    230     * @param conditions
    231     * @throws SQLException
    232     */

    233    public void putRow(Row row, String conditions) throws SQLException {
    234        Connection conn = null;
    235        Statement st = null;
    236        try {
    237            String ss = "";
    238            if (conditions == null{
    239                ss = "INSERT INTO " + tableName + " VALUES (";
    240                for (int i = 0; i < row.length(); ++i) {
    241                    String v = row.get(i);
    242                    ss += "'" + v + "'";
    243                    if (i != row.length() - 1)
    244                        ss += "";
    245                }

    246                ss += ")";
    247            }
     else {
    248                ss = "UPDATE " + tableName + " SET ";
    249                for (int i = 0; i < row.length(); ++i) {
    250                    String k = row.getKey(i);
    251                    String v = row.get(i);
    252                    ss += k + "='" + v + "'";
    253                    if (i != row.length() - 1)
    254                        ss += "";
    255                }

    256                ss += " WHERE ";
    257                ss += conditions;
    258            }

    259            logger.debug("Sql: " + ss);
    260            conn = DBUtil.getInstance().getConnection();
    261            st = conn.createStatement();
    262            st.executeUpdate(ss);
    263        }

    264        finally {
    265            try {
    266                if (st != null)
    267                    st.close();
    268            }
     catch (Exception e) {
    269            }

    270            try {
    271                if (conn != null)
    272                    conn.close();
    273            }
     catch (Exception e) {
    274            }

    275        }

    276    }

    277
    278    public void delRow(Row row) throws SQLException {
    279        Connection conn = null;
    280        Statement st = null;
    281        try {
    282            String ss = "";
    283            ss = "delete from " + tableName + " where ";
    284            for (int i = 0; i < row.length(); ++i) {
    285                String k = row.getKey(i);
    286                String v = row.get(i);
    287                ss += k + "='" + v + "'";
    288                if (i != row.length() - 1)
    289                    ss += " and ";
    290            }

    291            conn = DBUtil.getInstance().getConnection();
    292            st = conn.createStatement();
    293            st.executeUpdate(ss);
    294        }

    295        finally {
    296            try {
    297                if (st != null)
    298                    st.close();
    299            }
     catch (Exception e) {
    300            }

    301            try {
    302                if (conn != null)
    303                    conn.close();
    304            }
     catch (Exception e) {
    305            }

    306        }

    307    }

    308
    309    public void delRow(String conditions) throws SQLException {
    310        Connection conn = null;
    311        Statement st = null;
    312        try {
    313            String ss = "";
    314            ss = "delete from " + tableName + " where ";
    315            ss += conditions;
    316            conn = DBUtil.getInstance().getConnection();
    317            st = conn.createStatement();
    318            st.executeUpdate(ss);
    319        }
     catch (SQLException se) {
    320            se.printStackTrace();
    321        }

    322        finally {
    323            try {
    324                if (st != null)
    325                    st.close();
    326            }
     catch (Exception e) {
    327            }

    328            try {
    329                if (conn != null)
    330                    conn.close();
    331            }
     catch (Exception e) {
    332            }

    333        }

    334    }

    posted on 2005-09-06 13:30 fisher 閱讀(1919) 評(píng)論(0)  編輯  收藏 所屬分類: Eclipse Tech

    主站蜘蛛池模板: 黄网址在线永久免费观看| 无码视频免费一区二三区| xxxxwww免费| 成人毛片手机版免费看| 一区二区三区亚洲视频| 亚洲色婷婷六月亚洲婷婷6月| 亚洲av之男人的天堂网站| 亚洲电影免费观看| 亚洲日韩在线中文字幕综合 | 精品日韩亚洲AV无码一区二区三区 | 最近中文字幕高清免费中文字幕mv| 无码精品A∨在线观看免费| 日韩免费三级电影| 亚洲日本一区二区三区在线| 亚洲第一永久在线观看| 亚洲avav天堂av在线网毛片| 丝袜足液精子免费视频| 国产精彩免费视频| 四虎影在线永久免费观看| 国产精品亚洲成在人线| 亚洲精品123区在线观看| 四虎精品免费永久免费视频| 一区二区三区福利视频免费观看| 岛国大片免费在线观看| 亚洲中文字幕无码永久在线 | 亚洲AV美女一区二区三区| 亚洲色丰满少妇高潮18p| 在线观看人成视频免费无遮挡| 成年人视频免费在线观看| 亚洲一区二区精品视频| 亚洲人成毛片线播放| 国产精品成人啪精品视频免费| 国产91免费视频| 亚洲最大av无码网址| 亚洲情A成黄在线观看动漫软件| aa级毛片毛片免费观看久| 女人18毛片水真多免费播放 | 日韩亚洲国产二区| 91亚洲精品自在在线观看| XXX2高清在线观看免费视频| 四虎www成人影院免费观看|