Chapter 8: Spring JDBC Support

Overview

In the previous chapters, you saw how easy it is to build a fully Spring-managed application. You now have a solid understanding of bean configuration and Aspect Oriented Programming (AOP)—in other words, you know how to wire up the entire application using Spring. However, one of the parts of the puzzle is missing: How do you get the data that drives the application?

Apart from simple throwaway command line utilities, almost every application needs to persist data to some kind of data store. The most usual and convenient data store is a relational database.

The most notable open source databases are perhaps MySQL (www.mysql.com) and PostgreSQL (www.postgresql.org). MySQL is very fast, but it lacks support for more advanced features such as stored procedures and an internal scripting language. On the other hand, it runs on almost any operating system and platform. PostgreSQL is a bit pickier when it comes to the host platform, but it supports lots of advanced features. It is also worth noting that PL/pgSQL is very close to Oracle's PL/SQL.

Even if you choose the fastest and most reliable database, you cannot afford to loose the offered speed and flexibility by using a poorly designed and implemented data access layer. Applications tend to use the data access layer very frequently; thus any unnecessary bottlenecks in the data access code impact the entire application, no matter how well designed it is.

In this chapter, we show you how you can use Spring to simplify the implementation of data access code using JDBC. We start off by looking at the horrendous amount of code you would normally need to write without Spring and then compare it to a data access class implemented using Spring's data access classes. The result is truly amazing—Spring allows you to use the full power of human-tuned SQL queries while minimizing the amount of support code you need to implement. Specifically, we will discuss the following:

  • Comparing traditional JDBC code and Spring JDBC support: We explore how Spring simplifies the old-style JDBC code while keeping the same functionality. You will also see how Spring accesses the low-level JDBC API and how this low-level API is mapped into convenience classes such as JdbcTemplate.

  • Connecting to the database: Even though we do not go into every little detail of database connection management, we do show you the fundamental differences between a simple Connection and a DataSource. Naturally, we discuss how Spring manages the DataSources and which DataSources you can use in your applications.

  • Mapping the data to Java objects: We show you how to effectively and easily map the selected data to Java objects. You also learn that Spring JDBC is a viable alternative to Object-Relational Mapping (ORM) tools.

  • Inserting, updating, and deleting data: Finally, we discuss how you can implement the insert, update, and delete operations so that any changes to the database you are using do not have a devastating impact on the code you have written.

image from book
What is a database?

Developers sometimes struggle to describe what a database is. In one case, a database represents the actual data, and in other cases, it may represent a piece of software that manages the data, an instance of a process of this software, or even the physical machine that runs the manager process.

Formally, a database is a collection of data; the database software (such as Oracle, PostgreSQL, MySQL, etc.) is called database management software, or, more specifically, a relational database management system (RDBMS); the instance of RDBMS is called a database engine; and finally, the machine that runs the database engine is called the database server.

However, most developers immediately understand the meaning of the term database from the context in which it is used. This is why we use this term to represent all four meanings just described.

image from book



Pro Spring
Pro Spring
ISBN: 1590594614
EAN: 2147483647
Year: 2006
Pages: 189

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net