[BIRT]-[Tutorial]-使用ScriptDataSet从POJO中获得数据(二)

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

这样数据库连接类部分就完成了
下面写一个简单数据访问类,由TableRowSetRow三个类组成:<o:p></o:p>

Table.java代码如下
<o:p>

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

 13 ExpandedBlockStart.gifContractedBlock.gif public   class  Table  dot.gif {
 14InBlock.gif
 15InBlock.gif    private static final Logger    logger    = Logger.getLogger(Table.class);
 16InBlock.gif
 17InBlock.gif    private String                tableName;
 18InBlock.gif
 19ExpandedSubBlockStart.gifContractedSubBlock.gif    public Table(String name) dot.gif{
 20InBlock.gif        this.tableName = name;
 21ExpandedSubBlockEnd.gif    }

 22InBlock.gif
 23ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//**
 24InBlock.gif     * 根据条件查出该Table中的某些列
 25InBlock.gif     * 
 26InBlock.gif     * @param sql
 27InBlock.gif     * @param cols
 28InBlock.gif     * @return
 29InBlock.gif     * @throws SQLException
 30ExpandedSubBlockEnd.gif     */

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

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

 56InBlock.gif                rows.add(row);
 57ExpandedSubBlockEnd.gif            }

 58ExpandedSubBlockEnd.gif        }

 59ExpandedSubBlockStart.gifContractedSubBlock.gif        finally dot.gif{
 60ExpandedSubBlockStart.gifContractedSubBlock.gif            try dot.gif{
 61InBlock.gif                if (st != null)
 62InBlock.gif                    st.close();
 63ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 catch (Exception e) dot.gif{
 64ExpandedSubBlockEnd.gif            }

 65ExpandedSubBlockStart.gifContractedSubBlock.gif            try dot.gif{
 66InBlock.gif                if (conn != null)
 67InBlock.gif                    conn.close();
 68ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 catch (Exception e) dot.gif{
 69ExpandedSubBlockEnd.gif            }

 70ExpandedSubBlockEnd.gif        }

 71InBlock.gif        return rows;
 72ExpandedSubBlockEnd.gif    }

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

 92InBlock.gif                rows.add(row);
 93ExpandedSubBlockEnd.gif            }

 94ExpandedSubBlockEnd.gif        }

 95ExpandedSubBlockStart.gifContractedSubBlock.gif        finally dot.gif{
 96ExpandedSubBlockStart.gifContractedSubBlock.gif            try dot.gif{
 97InBlock.gif                if (st != null)
 98InBlock.gif                    st.close();
 99ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 catch (Exception e) dot.gif{
100ExpandedSubBlockEnd.gif            }

101ExpandedSubBlockStart.gifContractedSubBlock.gif            try dot.gif{
102InBlock.gif                if (conn != null)
103InBlock.gif                    conn.close();
104ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 catch (Exception e) dot.gif{
105ExpandedSubBlockEnd.gif            }

106ExpandedSubBlockEnd.gif        }

107InBlock.gif        return rows;
108ExpandedSubBlockEnd.gif    }

109InBlock.gif
110ExpandedSubBlockStart.gifContractedSubBlock.gif    private RowSet execute(String criteria) throws SQLException dot.gif{
111InBlock.gif &n
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值