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.
If Flexmonster is not yet embedded, set up an empty component in your webpage:
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 });
Complete the Integration with React guide. Your code should look similar to the following example:
<FlexmonsterReact.Pivot toolbar={true} />
Complete the Integration with Angular guide. Your code should look similar to the following example:
<fm-pivot [toolbar]="true"> </fm-pivot>
Complete the Integration with Vue guide. Your code should look similar to the following example:
<Pivot toolbar />
Add the following dependencies to your project:
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.
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:
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.
Processing for streams:
InputStream compressStream(InputStream inputStream, char delimiter)
InputStream compressStream(InputStream inputStream)
Processing for Databases:
InputStream compressDb(ResultSet resultSet)
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();
You may be interested in the following articles: