Connecting to a relational database with Java

NOTE: Flexmonster Data Compressor is considered deprecated, so the approach described below is not recommended for connecting to SQL databases.
To see the relevant way of connecting to relational databases, refer to this article.

Requirements

  • Flexmonster Pivot version 2.213 or higher
  • Java 7 or higher
  • A driver for the database

Supported databases

Step 1: Embed the component into your webpage

If Flexmonster is not yet embedded, set up an empty component in your webpage:

In pure JavaScript

Complete the Integrating Flexmonster guide. Your code should look similar to the following example:

let pivot = new Flexmonster({
  container: "pivotContainer",
  componentFolder: "node_modules/flexmonster/",
  toolbar: true
});

In React

Complete the Integration with React guide. Your code should look similar to the following example:

<FlexmonsterReact.Pivot
 toolbar={true}
/>

In Angular

Complete the Integration with Angular guide. Your code should look similar to the following example:

<fm-pivot
 [toolbar]="true">
</fm-pivot>

In Vue

Complete the Integration with Vue guide. Your code should look similar to the following example:

<Pivot
 toolbar
/>

Step 2: Setup the Data Compressor on the server

Add the following dependencies to your project:

  • An appropriate database driver
  • flexmonster-compressor.jar - located in the Pivot Table for Databases/server/java/ folder of the download package.

Below is a connection and query sample for a MySQL database:

Class.forName("com.mysql.jdbc.Driver").newInstance();
String connectionString = "jdbc:mysql://localhost:3306/foodmart";
Connection connection = 
	DriverManager.getConnection(connectionString, "user", "pass");
String query = "SELECT * FROM customer";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query);

Then the following line of code will convert ResultSet to an InputStream:

InputStream inputStream = Compressor.compressDb(resultSet);

Now you can create a response from the InputStream. For simple cases, it is suitable to read all content at once:

Scanner s = new Scanner(inputStream).useDelimiter("\\A");
String output = s.hasNext() ? s.next() : "";

It is also possible to create a streaming response. This means that the end user will get the response with minimal delay and the server will use less memory. This is the recommended approach for large datasets:

response.setContentType("text/plain");
OutputStream outputStream = response.getOutputStream();
int length = 0;
byte[] buffer = new byte[10240];
while ((length = inputStream.read(buffer)) > 0) {
    outputStream.write(buffer, 0, length);
    outputStream.flush();
}

The full project is available at Pivot Table for Databases/server/java/ inside the download package.

Step 3: Enable cross-origin resource sharing (CORS)

By default, the browser prevents JavaScript from making requests across domain boundaries. CORS allows web applications to make cross-domain requests. Here are some useful links explaining how to setup CORS on your server:

Step 4: Configure the report with your own data

Now it’s time to configure the pivot table on the webpage. Let’s create a minimal report for this (replace filename and other parameters with your specific values):

var pivot = new Flexmonster({
	container: "pivotContainer",
	toolbar: true,
	report: {
		dataSource: {
			type: "csv",
			/* URL to the Data Compressor Java */
			filename: "http://localhost:8400/FlexmonsterCompressor/get"
		}
	},
	licenseKey: "XXXX-XXXX-XXXX-XXXX-XXXX"
});

Launch the webpage from a browser — there you go! A pivot table is embedded into your project. Check out the example on JSFiddle.

Java Compressor API

Processing for streams:

InputStream compressStream(InputStream inputStream, char delimiter)
InputStream compressStream(InputStream inputStream)

Processing for Databases:

InputStream compressDb(ResultSet resultSet)

Examples

Saving to a file

Class.forName("com.mysql.jdbc.Driver").newInstance();
String connectionString = "jdbc:mysql://localhost:3306/foodmart";
Connection connection = 
	DriverManager.getConnection(connectionString, "user", "pass");
String query = "SELECT * FROM customer";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query);

InputStream inputStream = Compressor.compressDb(resultSet);
OutputStream outputStream = new FileOutputStream("data.csv");
int length = 0;
byte[] buffer = new byte[10240];
while ((length = inputStream.read(buffer)) > 0) {
    outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();

What's next?

You may be interested in the following articles: