JoinRowSet and FilteredRowSet provided by RowSet– Java’s class library for data computing – can perform cross-database related computing, but they have a lot of weaknesses. First, JoinRowSet only supports inner join, it doesn’t support outer join. Second, test shows that db2, mysql and hsql can work with JoinRowSet, yet the result set of join oracle11g to other databases is empty though no error reporting will appear. The fact is there were two users who perform cross-database join using oracle11g database even got the correct result. This suggests that JDBC produced by different database providers will probably affect the result obtained by using this method. Last, the code is complicated.
esProc has proved its ability in assisting Java to perform cross-database relating. It can work with various databases, such as oracle, db2, mysql, sqlserver, sybase and postgresql, to perform a variety of cross-database related computing, like inner join and outer join involving heterogeneous data. An example will teach you the way esProc works. Requirement: relate table sales in db2 to table employee in mysql through sale.sellerid and employee.eid, and then filter data in both sales and employee according to the criterion state=”California”. The way the code is written in this task applies to situations where other types of databases are involved.
The structure and data of table sales are as follows:
The structure and data of table employee are as follows:
Implementation approach: Call esProc script using Java program, join the multiple databases together to realize the cross-database relating, perform filtering and return the result to Java in the form of ResultSet.
A1: Connect to the data source db2 configured in advance.
A2: Connect to the data source mysql configured in advance. In fact oracle and other types of databases can be used too.
A3, A4: Retrieve table sequences: sales and employee, from db2 and mysql respectively. esProc’s Integration Development Environment (IDE) can display the retrieved data visually, as shown in the right part of the figure in the above.
A5: Relate sales to employee through sellerid=eid using esProc’s object reference mechanism.
A6: Filter the two table sequences according to state="California".
A7: Generate a new table sequence and get the desired fields.
A8,A9: Close the connection
A10: Return the result to the caller of esProc program.
This piece of program is called in Java using esProc JDBC to get the result. The code is as follows (save the above esProc program as test.dfx):
//create a connection using esProcjdbc
// call esProc program (the stored procedure) in which test is the name of file dfx
st =(com.esproc.jdbc.InternalCStatement)con.prepareCall("call test()");
// execute esProc stored procedure
// get the result set
ResultSet set = st.getResultSet();