The process with CSV and XLS Engines is straightforward.
Instantiate the class with of intended output, and invoke generate()
method.
Engine e = new CSVEngine(sqlConnection, query, fileName);
File f = e.generate();
Engine e = new XLSEngine(sqlConnection, query, fileName);
File f = e.generate();
To be able to use, some steps need to be undertaken before using XMLEngine.
Make sure the structure of the target XML file to be generated is predefined.
If XSD is not defined, follow the steps below:
Use xjc-tool to generate the required Java classes needed by XMLEngine (browse()
method).
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:
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();
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/
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();