ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

JDBC 4.0 Enhancements in Java SE 6
Pages: 1, 2, 3, 4

Testing

The classpath setting to compile the Java source should include the derby.jar and junit4.jar files located in the lib directory under project main directory. We also need to include etc, etc/jdbc and etc/log4j directories in the classpath so the application can access the JDBC properties and Log4J configuration files. I created an Ant build script (located in JdbcApp/build directory) to automate the tasks of compiling and packaging the Java code.



The test class used to test the loan details data access object is called LoanAppDetailsDAOTest. We pass in parameters such as loan ID and borrower name to get the loan details.

The following section shows code examples on auto-loading the JDBC driver and annotation-based SQL query features of JDBC 4.0 specification.

JDBC Driver Auto-Loading

The BaseDAO abstract class has a method called getConnection to get a database connection. The following code snippet shows this method (notice that we don't have to register the JDBC driver). The JDBC driver is automatically loaded as long as the appropriate driver class name (org.apache.derby.jdbc.ClientDriver) is located in java.sql.Driver file.

protected Connection getConnection() throws DAOException {
        // Load JDBC properties first
        if (jdbcUrl == null || jdbcUser == null ||
                        jdbcPassword == null) {
                loadJdbcProperties();
        }
        // Get Connection
        Connection conn = null;
        try {
                conn = DriverManager.getConnection(jdbcUrl, jdbcUser,
                                jdbcPassword);
        } catch (SQLException sqle) {
                throw new DAOException("Error in getting a DB connection.",
                                sqle);
        }
        return conn;
}

SQL Annotations

LoanAppDetailsQuery interface has the annotated SQL queries to get a list of active loans (criteria is loanstatus="A") and loan details based on the borrower name (in the case of a single borrower owning multiple loans). We saw these SQL annotations earlier in the article. Here's the sample code showing how we can call the SQL query defined using an Annotation.

public DataSet<LoanAppDetails> getAllActiveLoans() throws Exception {
        // Get Connection
        Connection conn = getConnection();
        LoanAppDetailsQuery query = null;
        DataSet<LoanAppDetails> loanDetails = null;
        query = QueryObjectFactory.createQueryObject(
                        LoanAppDetailsQuery.class, conn);
        loanDetails = query.getAllActiveLoans();
        return loanDetails;
}

public DataSet<LoanAppDetails> getLoanDetailsByBorrowerName(
                String borrFirstName, String borrLastName) throws Exception {
        // Get Connection
        Connection conn = getConnection();
        LoanAppDetailsQuery query = null;
        DataSet<LoanAppDetails> loanDetails = null;
        query = QueryObjectFactory.createQueryObject(
                        LoanAppDetailsQuery.class, conn);
        loanDetails = query.getLoanDetailsByBorrowerName(
                        borrFirstName,borrLastName);
        return loanDetails;
}

Conclusion

JDBC 4.0 provides ease of development and improves the developer experience when working with SQL. Another goal of JDBC 4.0 is to provide enterprise-level JDBC features to expose the API to a richer set of tools to manage JDBC resources. Also, the JDBC 4.0 API provides a migration path for JDBC drivers to be compliant with the J2EE Connector architecture (JCA). This provides JDBC vendors the ability to move toward implementing the JDBC technology Connector API. This is important in the context of using a JDBC data source in an enterprise Service Oriented Architecture (SOA) application where JDBC data source can be deployed as just another adapter in the Enterprise Service Bus (ESB) architecture without having to write ESB specific implementation code for the JDBC data source.

In this article, we looked at JDBC 4.0 enhancements, such as RowId support, JDBC driver loading, and Annotations-based SQL. There will be other features added in JDBC 4.0 in the future to support SQL:2003 specification. For more information on JDBC 4.0 spec, refer to the specification document.

Resources

Srini Penchikala is an information systems subject matter expert at Flagstar Bank.


Return to ONJava.com.