Creating JEE Database Applications

In the previous chapter, we learned how to use source control management software from Eclipse. Specifically, we learned how to use SVN and Git from Eclipse. In this chapter, we will get back to discussing JEE application development. Most web applications today require access to the database. In this chapter, we will learn two ways to access databases from JEE web applications: using JDBC APIs, and using JPA APIs.

JDBC4 has been part of JDK since version 1.1. It provides uniform APIs to access different relational databases. Between JDBC APIs and the database sits the JDBC driver for that database (either provided by the vendor of the database or a third-party vendor). JDBC translates common API calls to database-specific calls. The results returned from the database are also converted into objects of common data access classes. Although JDBC APIs require you to write a lot more code to access the database, it is still popular in JEE web applications because of its simplicity, flexibility of using database-specific SQL statements, and low learning curve.

JPA is the result of Java Specification Request 220 (which stands for JSR). One of the problems of using JDBC APIs directly is converting object representation of data to relation data. Object representation is in your JEE application, which needs to be mapped to tables and columns in the relational database. The process is reversed when handling data returned from the relational database. If there is a way to automatically map object-oriented representation of data in web applications to relational data, it would save a lot of developer time. This is also called object-relational mapping (ORM). Hibernate (http://hibernate.org/) is a very popular framework for ORM in Java applications.

Many of the concepts of such popular third-party ORM frameworks were incorporated in JPA. Just as JDBC provides uniform APIs for accessing relational databases, JPA provides uniform APIs for accessing ORM libraries. Third-party ORM frameworks provide implementations of JPA on top of their own framework. The JPA implementation may use the JDBC APIs underneath.

We will explore many features of JDBC and JPA in this chapter as we build applications using these frameworks. In fact, we will build the same application, once using JDBC and then using JPA.

The application that we are going to build is for student-course management. The goal is to take an example that can show how to model relationships between tables and use them in JEE applications. We will use a MySQL database and Tomcat web application container. Although this chapter is about database programming in JEE, we will revisit some of the things we learned about JSTL and JSF in Chapter 2, Creating a Simple JEE Web Application. We will use them to create user interfaces for our database web application. Make sure that you have configured Tomcat in Eclipse as described in Chapter 2, Creating a Simple JEE Web Application.

We will cover the following topics:

  • Core JDBC concepts
  • Using JDBC to access the database
  • Using JDBC connection pool
  • Core JPA concepts 
  • Using JPA to map entities (classes) to tables in the database
  • Configuring relationships between JPA entities

Let's first create a database and tables for this application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.142.232