Menu

Version2_Showcases

ENNAHDI EL IDRISSI, Mohamed

CSV and XLS Engines

The process with CSV and XLS Engines is straightforward.

Instantiate the class with of intended output, and invoke generate() method.

CSV Engine

Engine e = new CSVEngine(sqlConnection, query, fileName);
File f = e.generate();

XLS Engine

Engine e = new XLSEngine(sqlConnection, query, fileName);
File f = e.generate();

XML Engine

To be able to use, some steps need to be undertaken before using XMLEngine.

XSD or DTD

Make sure the structure of the target XML file to be generated is predefined.

If XSD is not defined, follow the steps below:

  1. Generate XML file from database client (if applicable). For instance, Use MySQL's Workbench to export data in XML format.
  2. Look up for an XML to XSD website online, and upload your XML file (for example: https://www.freeformatter.com/xsd-generator.html).
  3. Download the XSD file.

XJC-tool

Use xjc-tool to generate the required Java classes needed by XMLEngine (browse() method).

Example

The code below made use of Employees data (employee and respective supervisor).

<employees>
    <employee>
        <emp_no>10017</emp_no>
        <salary>71380</salary>
        <from_date>1993-08-03</from_date>
        <to_date>1994-08-03</to_date>
        <emp_no>10017</emp_no>
        <birth_date>1958-07-06</birth_date>
        <first_name>Cristinel</first_name>
        <last_name>Bouloucos</last_name>
        <gender>F</gender>
        <hire_date>1993-08-03</hire_date>
        <emp_no>10017</emp_no>
        <dept_no>d001</dept_no>
        <from_date>1993-08-03</from_date>
        <to_date>9999-01-01</to_date>
        <dept_no>d001</dept_no>
        <dept_name>Marketing</dept_name>
        <emp_no>110022</emp_no>
        <dept_no>d001</dept_no>
        <from_date>1985-01-01</from_date>
        <to_date>1991-10-01</to_date>
        <emp_no>10017</emp_no>
        <birth_date>1958-07-06</birth_date>
        <first_name>Cristinel</first_name>
        <last_name>Bouloucos</last_name>
        <gender>F</gender>
        <hire_date>1993-08-03</hire_date>
    </employee>
</employees>

In the XML file above, each employee node is nested in a root node called employees.

In this instance, XJC will generate three classes:

  1. Employee
  2. Employees
  3. ObjectFactory

ObjectFactory, as specified in the code below, simplifies the instanciation of XML related classes.

browse() method is the bridge between the database and the XML file to be generated.

Engine e = new XMLEngine(c, query, fileName) {
                @Override
                public Object browse(ResultSet rs) {
                    try {
                        ObjectFactory factory = new ObjectFactory();
                        Employees root = factory.createEmployees();
                        List<Employee> employees = new ArrayList<>();
                        while (rs.next()) {
                            Employee emp;
                            emp = factory.createEmployee(rs);
                            employees.add(emp);
                        }
                        root.setEmployees(employees);
                        logger.info("Rows Processed: " + i);
                        return root;
                    } catch (DatatypeConfigurationException | SQLException e) {
                        logger.error("", e);
                    } finally {
                        try {
                            rs.close();
                        } catch (SQLException e) {
                            logger.error("", e);
                        }
                    }
                    return null;
                }
            };
            File f = e.generate();

Source code

You may download source code of the project via this link:
https://sourceforge.net/p/automatic-report-generator/code/HEAD/tree/trunk/generator-examples-v2/

Sample data

For testing purposes, you may take advantage of this Docker image that provides a MySQL large database:
https://hub.docker.com/r/ennahdi/mysql-latest-employees

data-access-generator is a DAO layer that facilitates the access to the database via JDBC.
https://sourceforge.net/p/automatic-report-generator/code/HEAD/tree/trunk/data-access-generator/

It contains connection parameters and java.sql.Connection with the help of Singleton design pattern.

Connection c = DBConnection.getInstance();


Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.