Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to connect program data source with Java

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to connect program data sources with Java". In daily operation, I believe many people have doubts about how to connect program data sources with Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to connect program data sources with Java". Next, please follow the editor to study!

In practical applications, it may be necessary to dynamically change the data source according to the table name, for example, in the program dataset, through the passed table name parameters, the corresponding table is extracted from the database as the data source. For example, FineReport reads the data source through the AbstractTableData abstract class, and all the above data sources inherit and implement their abstract methods, so as long as you implement the AbstractTableData abstract class, you can use a custom type of data source (program dataset), which is a way to connect with the parameterized program dataset.

The FineReport report engine can read the defined data source and use it as a report data source, based on the principle of inheriting AbstractTableData.

1. Define parameters

Define a parameter and define the structure of the data table as follows:

Public ParamTableDataDemo () {/ / define the tableName parameter this.parameters = new Parameter [] {new Parameter ("tableName")}; / / define the program dataset column name columnNames = new String [columnNum]; for (int I = 0; I

< columnNum; i++) { columnNames[i] = "column#" + String.valueOf(i); } } 2、设置数据 将数据放入到定义的表中,代码如下: public void init() { // 确保只被执行一次 if (valueList != null) { return; } // 保存得到的数据库表名 String tableName = parameters[0].getValue().toString(); // 构造SQL语句,并打印出来 String sql = "select * from " + tableName + ";"; FRContext.getLogger().info("Query SQL of ParamTableDataDemo: \n" + sql); // 保存得到的结果集 valueList = new ArrayList(); // 下面开始建立数据库连接,按照刚才的SQL语句进行查询 Connection conn = this.getConnection(); try { Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); // 获得记录的详细信息,然后获得总列数 ResultSetMetaData rsmd = rs.getMetaData(); colNum = rsmd.getColumnCount(); // 用对象保存数据 Object[] objArray = null; while (rs.next()) { objArray = new Object[colNum]; for (int i = 0; i < colNum; i++) { objArray[i] = rs.getObject(i + 1); } // 在valueList中加入这一行数据 valueList.add(objArray); } // 释放数据库资源 rs.close(); stmt.close(); conn.close(); // 打印一共取到的数据行数量 FRContext.getLogger().info( "Query SQL of ParamTableDataDemo: \n" + valueList.size() + " rows selected"); } catch (Exception e) { e.printStackTrace(); } } 3、完整的数据集代码 整的带参程序数据集的代码如下 package com.fr.data;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.Statement;import java.util.ArrayList;import com.fr.base.Env;import com.fr.base.FRContext;import com.fr.data.AbstractTableData;import com.fr.base.Parameter;public class ParamTableDataDemo extends AbstractTableData { // 列名数组,保存程序数据集所有列名 private String[] columnNames = null; // 定义程序数据集的列数量 private int columnNum = 10; // 保存查询表的实际列数量 private int colNum = 0; // 保存查询得到列值 private ArrayList valueList = null; // 构造函数,定义表结构,该表有10个数据列,列名为column#0,column#1,。。。。。。column#9 public ParamTableDataDemo() { // 定义tableName参数 setDefaultParameters(new Parameter[] { new Parameter("tableName") }); // 定义程序数据集列名 columnNames = new String[columnNum]; for (int i = 0; i < columnNum; i++) { columnNames[i] = "column#" + String.valueOf(i); } } // 实现其他四个方法 public int getColumnCount() { return columnNum; } public String getColumnName(int columnIndex) { return columnNames[columnIndex]; } public int getRowCount() { init(); return valueList.size(); } public Object getValueAt(int rowIndex, int columnIndex) { init(); if (columnIndex >

= colNum) {return null;} return ((Object []) valueList.get (rowIndex)) [columnIndex];} / prepare data public void init () {/ / ensure that if (valueList! = null) {return is executed only once } / / the saved database table name String tableName = parameters [0] .getValue (). ToString (); / / construct the SQL statement and print out String sql = "select * from" + tableName + ";"; FRContext.getLogger () .info ("Query SQL of ParamTableDataDemo:\ n" + sql) / / Save the result set valueList = new ArrayList (); / / start to establish a database connection and query Connection conn = this.getConnection () according to the SQL statement just now; try {Statement stmt = conn.createStatement () ResultSet rs = stmt.executeQuery (sql); / / get the details of the record, and then get the total number of columns ResultSetMetaData rsmd = rs.getMetaData (); colNum = rsmd.getColumnCount () / / Save data with objects Object [] objArray = null; while (rs.next ()) {objArray = new Object [colNum]; for (int I = 0; I < colNum) ) {objArray [I] = rs.getObject (I + 1);} / / add this row of data valueList.add (objArray) to the valueList } / / release database resources rs.close (); stmt.close (); conn.close () / / print the total number of data rows fetched FRContext.getLogger () .info ("Query SQL of ParamTableDataDemo:\ n" + valueList.size () + "rows selected") } catch (Exception e) {e.printStackTrace ();}} / / get database connections driverName and url can be replaced with the public Connection getConnection () {String driverName = "org.sqlite.JDBC" you need String url = "jdbc:sqlite://D:\\ FineReport_8.0\\ WebReport\\ FRDemo.db"; String username = ""; String password = ""; Connection con = null; try {Class.forName (driverName); con = DriverManager.getConnection (url, username, password) } catch (Exception e) {e.printStackTrace (); return null;} return con } / / release some resources, because there may be repeated calls, so you need to release valueList and release public void release () throws Exception {super.release (); this.valueList = null;}}

Compile ParamTableDataDemo.java and copy the generated ParamTableDataDemo.class class file to the report project / WEB-INF/classes directory. Since the class is in the com.fr.data package, you should eventually put the class under / WEB-INF/classes/com/fr/data. At this point, the program data source is defined.

4. Configure the program dataset

Create a new report, create a new program data source in the report dataset, and select the program dataset we defined, such as the figure below. The name can be customized, such as divtable.

5. Use the program dataset

After configuring the program data source, you can use the defined program dataset. Select the dataset and click Preview.

Button, you can enter the table name to dynamically get the corresponding data table and make a template, as shown in the following figure

If the data cannot be previewed, make sure that the URL address is correct when defining the database connection in the code snippet.

As you can see, the data in the STSCORE table has been extracted into the program dataset table, and like other types of datasets, cell data column binding can be achieved by dragging and dropping.

At this point, the study of "how to connect program data sources with Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report