盈帆报表数据连接二次开发盈帆报表数据连接二次开发 一、在盈帆报表扩展新的数据连接,JAVA后台需要实现以下接口方法:
二、 demo示例: 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.*; public class SGCC { public Connection connection; //链接 public ResultSet rs; // 结果集 public PreparedStatement ps; // public JSONObject connObject = new JSONObject(); public String connFileName; /** * @描述 设置conn.xml文件的路径 * @参数 [fileName] * @返回值 void * @创建人 * @创建时间 * @修改人和其它信息 */ 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 * @创建人 * @创建时间 * @修改人和其它信息 */ 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 * @创建人 * @创建时间 * @修改人和其它信息 */ 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 * @创建人 * @创建时间 * @修改人和其它信息 */ 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>> * @创建人 * @创建时间 * @修改人和其它信息 */ 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 * @创建人 * @创建时间 * @修改人和其它信息 */ 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 * @创建人 * @创建时间 * @修改人和其它信息 */ 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 * @创建时间 * @修改人和其它信息 */ public boolean next() { try { return rs.next(); } catch (Exception e) { return false; } } /** * @描述 根据index获取字段类型 * @参数 [index] * @返回值 int * @创建人 * @创建时间 * @修改人和其它信息 */ 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 * @创建人 * @创建时间 * @修改人和其它信息 */ 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 * @创建人 * @创建时间 * @修改人和其它信息 */ 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 * @创建人 * @创建时间 * @修改人和其它信息 *//* public int getFieldTypeByName(String name) { return 0; } *//** * @描述 根据name获取字段值 * @参数 [index] * @返回值 int * @创建人 * @创建时间 * @修改人和其它信息 *//* public String getValueByName(String name) { return null; }*/ /** * @描述 根据sql打开新的prepareStatement, resultSet * @参数 [sql] * @返回值 boolean * @创建人 * @创建时间 * @修改人和其它信息 */ 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 * @创建人 * @创建时间 * @修改人和其它信息 */ public boolean close() { try { rs.close(); ps.close(); return true; } catch (Exception e) { return false; } } /** * @描述 获取当前结果集的列总数 * @参数 [] * @返回值 int * @创建人 * @创建时间 * @修改人和其它信息 */ public int getFieldCount() { try { ResultSetMetaData data = rs.getMetaData(); //获得结果集元数据 return data.getColumnCount(); } catch (Exception e) { return 0; } } /** * @描述 根据连接名获取url * @参数 [connName] * @返回值 java.lang.String * @创建人 * @创建时间 * @修改人和其它信息 */ 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) { } } 三、部署 写好的class文件编译成.jar文件,将其所需要的其他连接库与本身放置服务器\webapps\EFReport\WEB-INF\lib目录下,再将本身jar放置\webapps\EFReport\WEB-INF\lib\db目录与\webapps\EFReport\WEB-INF\classes\sqldrivers目录下。重新启动tomcat,即可在设计器中使用新写的数据库连接。
|