JDBC Namespace
This is one of the new features introduced in Spring 3.0. To use the jdbc namespace you’ll need to include the line as shown in Listing 8-31 in your application configuration namespaces.
Listing 8-31. jdbc namespace configuration in your application configuration XML file
1 |
xmlns:jdbc=http://www.springframework.org/schema/jdbc |
You should also include the schema location as shown in Listing 8-32.
Listing 8-32. jdbc namespace schema configuration in your application configuration XML file
1 2 |
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd |
The Jdbc namespace has two important elements. They are:
- initialize-database – which allows to initialize database with provided scripts
- embedded-database – which allows to initialize an embedded database
initialize-database
Can use the XML tags to initialize the data-source with the scripts provided. Listing 8-33 shows initializing database using the scripts.
Listing 8-33. XML configuration to initialize datasource using jdbc namespace
1 2 3 |
<jdbc:initialize-database data-source="dataSource"> <jdbc:script location="classpath:/testschema.sql"></jdbc:script> </jdbc:initialize-database> |
The various properties which can be used in the jdbc namespace in Listing 8-33 are:
- data-source – javax.sql.DataSource bean reference. Will be used to connect and execute the provided scripts.
- enabled – should it be enabled or not
- ignore-failures – specify that whether it should be ignored if statements end with errors.
This tag can have zero or more script tags and the property which this tag has is the location property.
embedded-database
Listing 8-34. XML configuration for embedded database using jdbc namespace
1 2 3 4 |
<jdbc:embedded-database id="datasource" type="H2"> <jdbc:script location="classpath:/schema.sql"/> <jdbc:script location="classpath:/test/data.sql"/> </jdbc:embedded-database> |
The main attributes in this are:
- id – becomes the bean name
- type – embedded database type. Supported databases are namely H2, Derby and HSQL. HSQL is the default database if it is not specified.
Spring JDBC Callback Interfaces
Even though most of the low level details related to JDBC API access such as Connections, Statements, ResultSets etc are hidden or abstracted from the developers, there may still be situations where the client code want to intercept the calling of the underlying APIs. For such situations Spring provides hook in the form of callbacks.
PreparedStatementCreator
The interface PreparedStatementCreator allows the client code to create Prepared Statement instances which represent the pre-compiled version of the SQL query passed. Lets assume that we would like to select the customer object based on customer primary key id. Since for every select operation, the customer id will be changing, we will pre-compile this query using PreparedStatement as shown in Listing 8-35.
Listing 8-35. Usage of PreparedStatementCreator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import org.springframework.jdbc.core.PreparedStatementCreator; public class CustomerPreparedStatementCreator implements PreparedStatementCreator{ private static String SQL = "SELECT * FROM CUSTOMER WHERE ID = ?"; private Customer customer; public CustomerPreparedStatementCreator(Customer customer){ this.customer = customer; } public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { PreparedStatement ps = connection.prepareStatement(SQL); ps.setInt(1, customer.getId()); return ps; } } |
RowCallbackHandler
An interface used by JdbcTemplate for processing rows of a ResultSet on a per-row basis. Implementations of this interface perform the actual work of processing each row but don’t need to worry about exception handling. SQLExceptions will be caught and handled by the calling JdbcTemplate.
Listing 8-36. Usage of RowCallbackHandler
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public class TestingClass { public static void main(String args[]) throws Exception { ApplicationContext ac = new ClassPathXmlApplicationContext( "applicationContext.xml", TestingClass.class); DataSource dataSource = (DataSource) ac.getBean("dataSource"); JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); jdbcTemplate.query("select first_name from customer", new RowCallbackHandler() { public void processRow(ResultSet resultSet) throws SQLException { while (resultSet.next()) { // do whatever you want with the resultset String firstName = resultSet.getString(1); } } }); } } |
Page Visitors: 10627
Tomcy John
Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023
Hi my friend! I wish to say that this article is awesome, nice written and come with approximately all important infos. Iˇ¦d like to peer more posts like this .
simple, well written. keep writing..