The code we have discussed in the first part of the chapter is not very complex, but it is annoying to write, and because there is so much of it to write, the likelihood of coding errors is quite high. It is time to take a look at how Spring makes things easier and more elegant.
JDBC support in Spring is divided into the four packages detailed in Table 8-1; each handles different aspects of JDBC access.
Package | Description |
---|---|
org.springframework.jdbc.core | This package contains the foundations of JDBC classes in Spring; namely SQLExceptionTranslator, which is responsible for processing exceptions from JDBC calls to Spring JDBC runtime exceptions, and a core JDBC class, the JdbcTemplate. |
org.springframework.jdbc.datasource | Contains helper classes and DataSource implementations that you can use to run JDBC code outside of a J2EE container. |
org.springframework.jdbc.object | Contains classes that help convert the data returned from the database into objects or lists of objects. These objects and lists are plain Java objects and therefore are disconnected from the database. |
org.springframework.jdbc.support | The most important class in this package is SQLException translation support. This allows Spring to recognize error codes used by the database and map them to higher-level exceptions. |
Let us start the discussion of Spring JDBC support by looking at the lowest-level functionality. The first thing you need to do before you can even think about running SQL queries is establish a connection to the database.