示例2- 自定义数据集-客户化文档
目录

示例2

业务场景-获取系统数据源链接

自定义数据集获取产品中内置的系统数据库连接,查询DEMO_EMPLOYEE物理表中4个字段,得到resultset结果集,最终将结果集转为DataSet对象。

需注意的是获取系统数据源的方法:

示例代码

package com.api;

 

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.sql.Statement;

import com.runqian.report4.usermodel.*;

import com.runqian.report4.dataset.*;

 

/**
 * @author jade 用自定义数据集实现连接系统默认数据源,查询employee物理表,生成dataset对象
 */

public class MyDataSetDb implements IDataSetFactory {

         Connection con = null;

         Statement stmt = null;

         public DataSet createDataSet(Context ctx, DataSetConfig dsc, boolean retrieve) {

                   con = getConnection(ctx, dsc);// 通过ctx获取数据库连接

                             // 执行sql语句,返回结果对象resultset

                   ResultSet rs = getRs(

                                     con,

                                     " SELECT DEMO_EMPLOYEE.雇员ID,DEMO_EMPLOYEE.名字,DEMO_EMPLOYEE.出生日期,DEMO_EMPLOYEE.职务 FROM DEMO_EMPLOYEE");

                            // 创建dataset

                   DataSet ds = new DataSet(null);

                   try {

                                            // 获取物理表列名称,用做自定义数据集的列名

                            ResultSetMetaData md = rs.getMetaData();

                            int cols = md.getColumnCount();

                                             // 创建列名

                            for (int i = 1; i <= cols; i++) {

                                     ds.addCol(md.getColumnName(i));

                            }

                                            // 创建row

                            while (rs.next()) {

                                     Row row = ds.addRow();

                                     for (int i = 1; i <= cols; i++) {

                                               row.setData(i, rs.getString(i));

                                     }

                            }

                   } catch (Exception e) {

                            e.printStackTrace();

                   }

                             // 关闭各种对象

                   release(rs, stmt, con);

                   return ds;

         }

 

            // 获取数据库连接

         public Connection getConnection(Context ctx, DataSetConfig dsc) {

           /*  注意在v5版本产品中获取系统数据源的方法:
                 String datasourceName1 = dsc.getDataSourceName();//此方法获取的数据源名称为 “其他数据源”
                 String datasourceName2=ctx.getDefDataSourceName();//v5中ctx.getDefDataSourceName()获取的是reportConfig中的datasouce,若没配置则为null   */

             try {

                         //获取demo数据源连接

                 IConnectionFactory icf = Context.getInitCtx().getConnectionFactory("demo");

                 con=icf.getConnection();

                 System.out.println("得到的数据源是:" + con);

             } catch (Exception ex) {

                 ex.printStackTrace();

             }

             returncon;

         }

           // 获取执行sql后的结果集

         public ResultSet getRs(Connection con, String sql) {

                   ResultSet rs = null;

                   try {

                            stmt = con.createStatement();

                            rs = stmt.executeQuery(sql);

                   } catch (SQLException e) {

                            e.printStackTrace();

                   }

                   return rs;

         }

 

         // 释放连接

         public static void release(ResultSet rs, Statement stmt, Connection con) {

                   try {

                            if (rs != null)

                                     rs.close();

                   } catch (Exception e) {

                            e.printStackTrace();

                   }

                   try {

                            if (stmt != null)

                                     stmt.close();

                   } catch (Exception e) {

                            e.printStackTrace();

                   }

                   try {

                            if (con != null)

                                     con.close();

                   } catch (Exception e) {

                            e.printStackTrace();

                   }

         }

}