Report data connection secondary development

First, the extended reporting new data connection, JAVA interface methods need to implement the following background:

 

No.

Method name

parameter

Explanation

1

testConnection

type: Database type url: database link urlusername: user name (HTTP interfaces without the parameter) password: password (HTTP interfaces without the parameter)

For the databases, can be determined by the method specified in the database can connect to the custom HTTP interface, it may be determined whether the method is implemented by linking to the specified HTTP interface

2

connect

connName: Data connection name url: url database link

For databases, you can use this method to specify the database link. For a custom HTTP interface, the link may be achieved by the method specified in HTTP interface

3

open

sql: database query (HTTP interfaces without the parameters) fetchSize: (HTTP interfaces without the parameter)

For the databases, the method can be initialized by the data set for the custom HTTP interface, it may be returned by the specified interface data set to implement the method

4

getConnURL

connName: Data Link name

For the database, the data can be obtained through the data link link name URL for custom HTTP interface, it can be returned to the url specified by implementing the method

5

getTableName

type: Database type url: database link urlusername: user name (HTTP interfaces without the parameter) password: password (HTTP interfaces without the parameter)

For the databases, you can be acquired in a given database table name by this method. For a custom HTTP interface, it may be returned by a custom database table to implement the method

6

getResultsByPage

connName: database connection name sql: database query (HTTP interfaces without the parameter) count: display per page

For the database, the data can be obtained through the connection name and paging sql statement. For a list of custom HTTP data interfaces, it may be returned by a custom implementation of the method

7

getFieldNames

sqlStr: sql statement (HTTP interfaces without the parameter)

For the database, the result set of data may be acquired by the sql statement for all column names for custom HTTP interface may be implemented by a custom return the data column names

8

getFieldTypeByIndex

index: Index

For the database, the field type may be acquired result set of data corresponding to a column for custom HTTP interface may be implemented by return a custom field type of data for which the index by

9

getFieldNameByIndex

index: Index

For the database, the result set of data may be acquired by the index of the corresponding column name data field names for custom HTTP interface, it can be used to return a custom field by implementing the method

10

getValueByIndex

index: Index type: field type

For the databases, the data value may be acquired by the result set corresponding to the column index and field type for the custom HTTP interface, it can be customized to return a corresponding column values ​​by implementing the method

11

getFieldCount

 

For the databases, the data set may be acquired for the total number of columns of custom HTTP interface, it returns the corresponding value can be achieved by this method

12

close

 

For the databases, the data set can be turned off for a custom HTTP interface, the data set can be turned off by implementing the method

13

disConnect

 

For the database, the link can be turned off for a custom HTTP interface, the link can be turned off by implementing the method

 

Two, demo example:

package com.efreport.database;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;

import java.io.*;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.*;
import java.util.*;


/**
 * Created by whj on 2019/10/28.
 */
public class SGCC {


    public Connection connection; //链接
    public ResultSet rs; // 结果集
    public PreparedStatement ps; //
    public JSONObject connObject = new JSONObject();
    public String connFileName;


    /**
     * @描述 设置conn.xml文件的路径
     * @参数 [fileName]
     * @返回值 void
     * @创建人 whj
     * @创建时间 2019/10/28
     * @修改人和其它信息
     */
    public boolean setConnFileName(String fileName) {
        try {
            connFileName = fileName;
            File jsonFile = new File(fileName);
            FileInputStream fis = new FileInputStream(jsonFile); //解析内核文件
            InputStreamReader inputStreamReader = new InputStreamReader(fis, "UTF-8");
            BufferedReader in = new BufferedReader(inputStreamReader);

            StringBuffer sb = new StringBuffer();

            String str;
            while ((str = in.readLine()) != null) {
                sb.append(str);
            }
            in.close();

            String jsonText = sb.toString();

            JSONArray jsonArray = JSONArray.parseArray(jsonText);

            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject object = jsonArray.getJSONObject(i);
                String name = object.getString("name");
                connObject.put(name, object);
            }


        } catch (Exception e) {
            return false;
        }

        return true;
    }

    public void setConnObject(JSONArray jsonArray) {
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject object = jsonArray.getJSONObject(i);
            String name = object.getString("name");
            this.connObject.put(name, object);
        }
    }

    ;

    /**
     * @描述 根据conn.xml中的链接名来判断是否能链接上指定数据库
     * @参数 [connName]
     * @返回值 boolean
     * @创建人 whj
     * @创建时间 2019/10/28
     * @修改人和其它信息
     */
    public boolean connect(String connName, String url) {


        JSONObject object = connObject.getJSONObject(connName); //获取链接信息
        String username = object.getString("username"); //用户名
        String password = object.getString("password"); //密码
        String type = object.getString("type"); //类型
        try {
            connection = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * @描述 根据conn.xml中的链接名来判断是否能链接上指定数据库
     * @参数 [connName]
     * @返回值 boolean
     * @创建人 whj
     * @创建时间 2019/10/28
     * @修改人和其它信息
     */
    public boolean connect(String url, String username, String password) {
        try {
            connection = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * @描述 获取数据库中所有的表
     * @参数 []
     * @返回值 java.lang.String
     * @创建人 whj
     * @创建时间 2019/10/28
     * @修改人和其它信息
     */
    public static String getTableName(String type, String url, String username, String password) {
        JSONArray tables = new JSONArray();
        String sql = "SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema = (select database()) AND table_type = 'base table'";

        try {


            Class.forName("sgcc.nds.jdbc.driver.NdsDriver");

            Connection connection = DriverManager.getConnection(url, username, password);
            PreparedStatement ps = connection.prepareStatement(sql);
            ResultSet rs = ps.executeQuery(); //执行获得结果集
            while (rs.next()) {
                tables.add(rs.getString("TABLE_NAME"));
            }
            rs.close();
            ps.close();
            connection.close();


            return tables.toJSONString();
        } catch (Exception e) {
            return null;
        }

    }

    /**
     * @描述 根据sql、页数、每页显示条数获取数据
     * @参数 [sql, page, count]
     * @返回值 java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @创建人 whj
     * @创建时间 2019/11/4
     * @修改人和其它信息
     */
    public List<Map<String, Object>> getResultsByPage(String connName, String sql, int count) {
        JSONObject object = connObject.getJSONObject(connName); //根据连接名获取数据库链接对象
        String type = object.getString("type"); //数据库类型
        List<Map<String, Object>> list = new ArrayList<>();
        try {
            Class.forName("sgcc.nds.jdbc.driver.NdsDriver");
            ps = connection.prepareStatement(sql);
            rs = ps.executeQuery(); // 返回查询结果集合
            ResultSetMetaData metaData = rs.getMetaData(); // 获得列的结果
            while (rs.next()) {
                if (count > 0) {
                    Map<String, Object> map = new HashMap();
                    int cols_len = metaData.getColumnCount(); // 获取总的列数
                    for (int i = 0; i < cols_len; i++) {

                        String col_name = metaData.getColumnName(i + 1); // 获取第 i列的字段名称
                        Object col_value = rs.getObject(col_name); // 获取第i列的内容值
                        col_value = col_value == null ? "" : col_value.toString();
                        map.put(col_name, col_value);
                    }
                    list.add(map);
                    count--;
                } else {
                    break;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return list;
    }


    /**
     * @描述 测试数据库链接
     * @参数 [type 数据库类型, url 数据库URL, username 用户名, password 密码]
     * @返回值 boolean
     * @创建人 whj
     * @创建时间 2019/11/2
     * @修改人和其它信息
     */
    public static boolean testConnection(String type, String url, String username, String password) {
        try {
            Class.forName("sgcc.nds.jdbc.driver.NdsDriver");
            Connection connection = DriverManager.getConnection(url, username, password);
            connection.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * @描述 根据指定的sql获取查询结果的所有字段名
     * @参数 [sqlStr]
     * @返回值 java.lang.String
     * @创建人 whj
     * @创建时间 2019/10/28
     * @修改人和其它信息
     */
    public String getFieldNames(String sqlStr) {
        JSONArray names = new JSONArray();  //所有字段名
        try {
            PreparedStatement ps = connection.prepareStatement(sqlStr);
            rs = ps.executeQuery();
            ResultSetMetaData data = rs.getMetaData(); //获得结果集元数据
            for (int i = 1; i <= data.getColumnCount(); i++) {
                // 获得指定列的列名
                String columnName = data.getColumnName(i);
                names.add(columnName);
            }

        } catch (Exception e) {
            return null;

        }
        return names.toString();
    }

    /**
     * @描述 rs.next
     * @参数 []
     * @返回值 boolean
     * @创建时间 2019/10/28
     * @修改人和其它信息
     */
    public boolean next() {
        try {
            return rs.next();
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * @描述 根据index获取字段类型
     * @参数 [index]
     * @返回值 int
     * @创建人 whj
     * @创建人 whj
     * @创建时间 2019/10/28
     * @修改人和其它信息
     */
    public int getFieldTypeByIndex(int index) {
        try {

            ResultSetMetaData data = rs.getMetaData(); //获得结果集元数据
            // 获得所有列的数目及实际列数
            int columnType = data.getColumnType(index);
            if (columnType == 1 || columnType == 12 || columnType == -1 || columnType == -9) {//STRING
                return 1;
            } else if (columnType == -7 || columnType == -6 || columnType == 5 || columnType == 4 || columnType == -5) {//INT
                return 2;
            } else if (columnType == 8 || columnType == 2 || columnType == 3 || columnType == 6 || columnType == 7) {//DOUBLE
                return 3;
            } else if (columnType == 91 || columnType == -15 || columnType == -16) {//Date
                return 4;
            } else if (columnType == 92 || columnType == 93) {//Time
                return 5;
            } else if (columnType == 16) {//BOOLEAN
                return 6;
            } else {
                return 1;
            }

        } catch (Exception e) {
            return 0;
        }

    }

    /**
     * @描述 根据index获取字段名称
     * @参数 [index]
     * @返回值 int
     * @创建人 whj
     * @创建时间 2019/10/28
     * @修改人和其它信息
     */
    public String getFieldNameByIndex(int index) {
        try {

            ResultSetMetaData data = rs.getMetaData(); //获得结果集元数据
            // 获得所有列的数目及实际列数
            String columnType = data.getColumnName(index);
            //整型
            return columnType;

        } catch (Exception e) {
            return null;
        }

    }


    /**
     * @描述 根据index和type获取字段值
     * @参数 [index], [type]
     * @返回值 int
     * @创建人 whj
     * @创建时间 2019/10/28
     * @修改人和其它信息
     */
    public String getValueByIndex(int index, int type) {
        //int type = getFieldTypeByIndex(index);
        try {


            if (type == 1) {
                String value = rs.getString(index);
                return value == null ? "" : value;
            } else if (type == 2) {
                String value = rs.getString(index);
                return value == null ? "" : value;
            } else if (type == 3) {
                String value = rs.getString(index);
                return value == null ? "" : value;
            } else if (type == 4) {//date
                Timestamp timestamp = rs.getTimestamp(index);
                if (timestamp == null) {
                    return "";
                } else {
                    long time = timestamp.getTime();
                    return String.valueOf(time);
                }
                //return timestamp==null?"":timestamp.toString();
            } else if (type == 5) {//time
                Timestamp timestamp = rs.getTimestamp(index);
                if (timestamp == null) {
                    return "";
                } else {
                    long time = timestamp.getTime();
                    return String.valueOf(time);
                }
            } else if (type == 6) {
                return rs.getString(index) == null ? "" : rs.getString(index);
            } else {
                return null;
            }


        } catch (Exception e) {
            return null;
        }
    }


   /* *//**
     * @描述 根据name获取字段类型
     * @参数 [index]
     * @返回值 int
     * @创建人 whj
     * @创建时间 2019/10/28
     * @修改人和其它信息
     *//*
    public int getFieldTypeByName(String name) {
        return 0;
    }


    *//**
     * @描述 根据name获取字段值
     * @参数 [index]
     * @返回值 int
     * @创建人 whj
     * @创建时间 2019/10/28
     * @修改人和其它信息
     *//*
    public String getValueByName(String name) {
        return null;
    }*/

    /**
     * @描述 根据sql打开新的prepareStatement, resultSet
     * @参数 [sql]
     * @返回值 boolean
     * @创建人 whj
     * @创建时间 2019/10/28
     * @修改人和其它信息
     */
    public boolean open(String sql, int fetchSize) {

        //当SQL语句里面含有以下这些关键字时,不执行
        String[] excludeSql = new String[]{"delete ", "update ", "drop ", "insert ", "select into"};
        for (int i = 0; i < excludeSql.length; i++) {
            String key = excludeSql[i];
            if (sql.contains(key)) {
                return false;
            }
        }
        try {
            ps = connection.prepareStatement(sql);
            ps.setFetchSize(fetchSize);
            rs = ps.executeQuery();
        } catch (Exception e) {
            return false;
        }
        return true;
    }


    /**
     * @描述 关闭当前连接里面的prepareStatement, resultSet
     * @参数 []
     * @返回值 boolean
     * @创建人 whj
     * @创建时间 2019/10/28
     * @修改人和其它信息
     */
    public boolean close() {
        try {
            rs.close();
            ps.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * @描述 获取当前结果集的列总数
     * @参数 []
     * @返回值 int
     * @创建人 whj
     * @创建时间 2019/10/28
     * @修改人和其它信息
     */
    public int getFieldCount() {
        try {
            ResultSetMetaData data = rs.getMetaData(); //获得结果集元数据
            return data.getColumnCount();
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * @描述 根据连接名获取url
     * @参数 [connName]
     * @返回值 java.lang.String
     * @创建人 whj
     * @创建时间 2019/10/28
     * @修改人和其它信息
     */
    public String getConnURL(String connName) {
        try {

            JSONObject object = connObject.getJSONObject(connName); //获取链接信息
            Class.forName("sgcc.nds.jdbc.driver.NdsDriver");
            if (connObject == null || object == null) {
                return null;
            } else {
                return object.getString("url");
            }

        } catch (Exception e) {
            return null;
        }

    }

    public boolean disConnect() {
        try {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                rs.close();
            }
            if (connection != null) {
                connection.close();
            }
            return true;
        } catch (Exception e) {
            return false;
        }

    }


    public static void main(String[] args) {
        SGCC db = new SGCC();
        String fileName = "d:" + File.separator + "conn.json";
        db.setConnFileName(fileName);
        JSONObject object = db.connObject;
        try {
            JSONObject obj = object.getJSONObject("redis");
            db.connect("redis", obj.getString("url"));
            String sql = "(StringTest , 1 , 2)";
            db.open(sql, 10);
            while (db.next()) {
                System.out.println("FieldNames:" + db.getFieldNames(sql));
                System.out.println("FieldCount:" + db.getFieldCount());
                System.out.println("FieldName:" + db.getFieldNameByIndex(1));
                int fieldType = db.getFieldTypeByIndex(1);
                System.out.println("FieldType:" + db.getFieldTypeByIndex(1));
                System.out.println("FieldValue:" + db.getValueByIndex(1, fieldType));
            }


        } catch (Exception e) {

        }


    }


}

DEPLOYMENT

Written class files compiled into a .jar file, the other link libraries they need and placed itself under the server \ webapps \ EFReport \ WEB-INF \ lib directory, and then placed itself jar \ webapps \ EFReport \ WEB-INF \ lib \ db directory \ webapps \ EFReport \ WEB-INF \ classes \ directory under sqldrivers with. Restart tomcat, you can use the new write database connection in the designer.

   

Published 39 original articles · won praise 1 · views 10000 +

Guess you like

Origin blog.csdn.net/efreport/article/details/105215569