API导入

请问各位大神,帆软可以通过API去查数据吗,就是我不用sql或者存储过程去查数据,而是直接写一个API去查数据导入到帆软报表中展示。

FineReport 番薯_asama 发布于 2019-9-25 09:55
1min目标场景问卷 立即参与
回答问题
悬赏:4 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共1回答
最佳回答
1
cxyangLv5初级互助
发布于2019-9-25 10:20

前段时间用过客户给的接口,写Java然后查数据,然后用程序数据集

简单程序数据集-https://help.finereport.com/doc-view-650.html

带参程序数据集-https://help.finereport.com/doc-view-642.html


不知道是不是你想要的,给你参考参考

这是带参的代码,如果要不带参的再找 我


package com.fr.data;   
      

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fr.data.AbstractTableData;
import com.fr.third.org.apache.http.client.methods.CloseableHttpResponse;
import com.fr.third.org.apache.http.client.methods.HttpGet;
import com.fr.third.org.apache.http.client.utils.URIBuilder;
import com.fr.third.org.apache.http.impl.client.CloseableHttpClient;
import com.fr.third.org.apache.http.impl.client.HttpClients;
import com.fr.third.org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;  
      
    /**
     * 带参数的程序数据集Demo
     *
     * @author fanruan
     */  
    public class Param extends AbstractTableData {  
        /**
         * 列名数组,保存程序数据集所有列名
         */  
        private String[] columnNames;  
        /**
         * 定义程序数据集的列数量
         */  
     //   private int columnNum = 10;  
        /**
         * 保存查询表的实际列数量
         */  
   //     private int colNum = 0;  
        /**
         * 保存查询得到列值
         */  
        private Object[][] rowData;
      
        /**
         * 构造函数,定义表结构,该表有10个数据列,列名为column#0,column#1,。。。。。。column#9
         */  
        public Param() {  
            String[] columnNames = { "RECEIVETIME", "APPROVALTIME","APPROVALOPINIONS","APPROVALLINK","NAME" };  
             this.columnNames = columnNames;  
        }  
      
        /**
         * 实现其他四个方法
         *
         * @return columnNum
         */  
        @Override  
        public int getColumnCount() {   
            init();  
            return columnNames.length;  
        }  
      
        @Override  
        public String getColumnName(int columnIndex) {  
            return columnNames[columnIndex];  
        }  
      
        @Override  
        public int getRowCount() {  
            return rowData.length;   
        }  
      
        @Override  
        public Object getValueAt(int rowIndex, int columnIndex) {  
        
            return rowData[rowIndex][columnIndex];  
        }  
      
        /**
         * 准备数据
         */  
        private void init() {  
            // 确保只被执行一次  
            if (rowData != null) {  
                return;  
            }  
            // 保存得到的数据库表名  
            String area_id = parameters[0].getValue().toString();  
            String month = parameters[1].getValue().toString();
            String prj = parameters[2].getValue().toString();
            
             String instanceId = "";
        try {
            instanceId = getInstanceId(area_id,month,prj);
        } catch (Exception e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
                     
            Map<String, String> param=new HashMap<String, String>();
            
            param.put("instanceId", instanceId);
            String name=doGet("接口链接",param);
            String name1 = name.substring("successCallback(".length());
            String name2 = name1.substring(0, name1.length()-1);
            JSONObject jsonObject=JSON.parseObject(name2);
            JSONArray data = jsonObject.getJSONArray("data");
           Object[][] datas= new Object[data.size()][5];
          for (int i = 0; i < data.size(); i++) {
               JSONObject jo = data.getJSONObject(i);
               String receivtime = jo.getString("RECEIVETIME");
               String approvaltime = jo.getString("APPROVALTIME");
               String approvalopinions = jo.getString("APPROVALOPINIONS");
               String approvallink = jo.getString("APPROVALLINK");
               String names = jo.getString("NAME");
               datas[i][0]= receivtime;
               datas[i][1]= approvaltime;
               datas[i][2]= approvalopinions;
               datas[i][3]= approvallink;
               datas[i][4]= names;
           }
         
          this.rowData = datas;  
        }  
      
        /**
         * 带参数的get请求
         * @param url
         * @param param
         * @return String
         */
        public static String doGet(String url, Map<String, String> param) {
            // 创建Httpclient对象
            CloseableHttpClient httpclient = HttpClients.createDefault();
     
            String resultString = "";
            CloseableHttpResponse response = null;
            try {
                // 创建uri
                URIBuilder builder = new URIBuilder(url);
                if (param != null) {
                    for (String key : param.keySet()) {
                        builder.addParameter(key, param.get(key));
                    }
                }
                URI uri = builder.build();
                // 创建http GET请求
                HttpGet httpGet = new HttpGet(uri);
                // 执行请求
                response = httpclient.execute(httpGet);
                // 判断返回状态是否为200
                if (response.getStatusLine().getStatusCode() == 200) {
                    resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (response != null) {
                        response.close();
                    }
                    httpclient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return resultString;
        }
        
        /**
         * 释放一些资源,因为可能会有重复调用,所以需释放valueList,将上次查询的结果释放掉
         *
         * @throws Exception e
         */  
        @Override  
        public void release() throws Exception {  
            super.release();  
            this.rowData = null;  
        }
        
        
        public static String getInstanceId(String area,String month,String proj ) throws Exception {
            String prj = "1 = 1";
            if(!proj.isEmpty()) {
                prj = "b.pk_project = '"+proj+"'";
            }
            String externalSql = "select "+
                    "instanceid "+
                    "from "+
                //SQL拼写
                    " and approvalstatus = 'End' "+
                    " and "+prj+
                    " and b.month = '"+month+"' ) a where rown = 1 ";
                    
            Connection bidccon = getBidcConnection();
            Statement bidcstatment = bidccon.createStatement();
            ResultSet externalResult = bidcstatment.executeQuery(externalSql);
            externalResult.next();
            String instanceId = externalResult.getString("INSTANCEID");
            externalResult.close();
            bidccon.close();
            bidcstatment.close();
            return instanceId;
        }
        
        
        public  static Connection getBidcConnection() {  
              
            String driverName = "oracle.jdbc.driver.OracleDriver";  
            String url = "";  
            String username = "";  
            String password = "";
            Connection con;  
            try {  
                Class.forName(driverName);  
                con = DriverManager.getConnection(url, username, password);  
      
            } catch (Exception e) {  
                e.printStackTrace();  
                return null;  
            }  
            return con;  
        }
        
     /*   public static void main(String[] args) throws Exception {
            String result = getInstanceId("粤北","201908","");
            System.out.println(result);
        }*/
        
    } 
 

  • 3关注人数
  • 1679浏览人数
  • 最后回答于:2019-9-25 10:20
    请选择关闭问题的原因
    确定 取消
    返回顶部