Beginning Java Databases

Beginning Java Databases

by MUKHAR, Bjarki Holm, John Carnell, Mark Mamone, Ron Phillips
     
 
Java has evolved into a robust, high performance programming language that is well suited to a range of different environments, be it on a middle tier Application Server or a client browser. Regardless of the architecture of your application you are using, it will almost certainly need to make use of data that is stored in some form of database. Relational databases

Overview

Java has evolved into a robust, high performance programming language that is well suited to a range of different environments, be it on a middle tier Application Server or a client browser. Regardless of the architecture of your application you are using, it will almost certainly need to make use of data that is stored in some form of database. Relational databases are the data store of choice in the vast majority of businesses, and have also evolved enormously over the recent years, into powerful and feature-rich data management systems.

This book aims to teach you how to use these two powerful technologies to build successful Java database applications. You will find out how relational databases work and how you can use them in your Java programs, through the JDBC interface. You will see how to apply your new skills in an enterprise environment and by the end will be building sophisticated web-enabled Java database applications that incorporate other technologies, such as XML.

This book is ideal for the programmer who has a reasonable grasp of the fundamentals of the Java language, and is now looking to apply and improve these skills in building database-driven Java applications. If you are relatively new to Java, then the explanations in the text should allow you to grasp all of the fundamental issues discussed, especially if you are migrating from another language such as C or C++.

What does this book cover?

  • Using the JDBC API to build database-driven Java applications
  • Introduction to new JDBC 3.0 features
  • SQL and relational database design
  • Object-relational mapping frameworks and techniques
  • Debugging your application and logging its activities
  • Applying Java and JDBC skills in a J2EE environment
  • Integrating XML into you Java database applications

Product Details

ISBN-13:
9781861004376
Publisher:
Apress
Publication date:
08/01/2001
Series:
Beginning Ser.
Edition description:
2001
Pages:
800
Product dimensions:
(w) x (h) x 0.08(d)

Read an Excerpt

Chapter 1: Relational Databases and Java

Data is the foundation of all but the most trivial application development. Whether the application is a video game storing a user's score in memory or an accounting package saving a customer's order, it will need to load, manipulate, and store data in some form or another. The relational database, although not the only option, is currently by far the most common means of storing this data.

In this chapter we will:

  • Give a foundation in the basics of relational databases

  • Introduce the Structured Query language (SQL), the standard programming interface for retrieving and manipulating data in relational databases

  • Introduce the JDBC Application Programming Interface, through which Java supports relational databases (and thus the ability to execute SQL commands from our Java programs)

  • Install the Cloudscape database and the sample Music Store database that we will use throughout this book

Persisting Data

The Java language defines a vast array of data structures with which we can store information in our programs, from the simple variable to arrays, vectors, and nodes. The idea behind persisting that data is simple: your program has knowledge about some kind of information or data at one point in time and you want to be able to access that same information and maybe even update it or delete it at a point of time in the future.

Almost any kind of program you could mention will involve some kind of persistent data - even those you might not immediately associate with the need for data storage, until you start to think about it. Consider, for example, what happens when you request a web page from you browser (say, http://www.wrox.com). Your request will go through a 'naming server' that will convert your URL into an IP address, by performing a database lookup. The data needed to do this conversion database must be persistent and is continuously updated.

If your program handles only small amounts of data, then you may be able to simply store that data in a text file on disk, or make use of Java's object serialization. However, if it handles a significant amount of data, then it is likely to store that data in a relational database. Let's briefly consider the first two of these options before moving on to discuss the need to write Java applications that are driven by a relational database.

Persistence in .lava

There are several ways that we can persist data in Java. Some of the examples include utilizing flat files, serializing objects and using a database. Depending on the amount and type of data that you need to persist, you might choose a different implementation.

Flat Files

We can write to and read from flat files that are stored in the file system on disk. This is a viable option when all you need to store are some configuration settings, a text document to be printed out later, or an XML document for transfer to some other part of the world. For a very small set of data that is not updated very often, the overhead of something as complex as a database might be excessive.

If you have more than a small set of static data, a flat file implementation might be too costly. If the data is going to be queried, then traversing through a file to search for records to read and/or update data can be very expensive in terms of memory and application time. Also, there is not a natural way to relate the information between different files. Of course you could create some sort of key into another file, but trying to query against multiple flat files would be very inefficient.

If you have to deal with updating or deleting the data in files then you need to deal with ways to control access to the files to avoid locking problems. This is a much more difficult problem than it sounds like. If you lock the whole file so that one user can update a record in that file, you can take a very serious performance hit. Trying to figure out which record in a flat file is going to be updated and locking that one only is not an easy task either.

Sometimes, you may want to store more complex data. For example, you may wish to store the state of your application at a given point in time so that a user can return to the application at some future time and find it in the state in which it was left. In such a scenario, a flat file would be very cosily. You would have to go through all of the active objects that contained any data that needed to be persisted and put it in a format that could be written to disk and then read it back into memory later. This also creates a maintenance problem. Any time that you add new functionality into your classes, you need to ensure that the mechanism that you used to persist the data to disk is updated to reflect those changes.

Object Serialization

To handle the case when you need to store the state of your system, Java has the option of making your objects serializable. Making an object serializable simply means that you want to be able to write the object out over some stream and read it back in later. Examples of streaming are sending the output to a file on your disk or over a network connection to another computer. When you define your classes, you simply have your class implement the java.io.Serializable interface.

Once you have implemented the interface, you are now abstracted away from having to know about how the file I/O is going to work or the format of the files. You do not even need to worry about any changes to the structure of the object since that will all be taken care of for you. If you change the structure of your Java class, because you implemented the serializable interface, Java will take care of ensuring that any new variables and methods are added when the object is written out and that they are repopulated when the object is read in.

Java's object is commonly implemented in distributed programming and is very useful when you wish to store an object's state and then recreate a copy of that object in a different application. Instead of having to make several remote method calls to populate the data in the remote object, you can just stream the remote object over the network to another computer where it can be recreated. However, again, it is only really effective for handling small amounts of data - it is not a good implementation in general for overall persistence.

Relational Databases

The persistence mechanism that we use exclusively in this book is the relational database. A database is really a collection of entities, or objects. These objects control how the data is stored and how it is managed.

In a relational database the basic data storage entity is a table. Each entity will have a specific set of attributes (or properties) that define the sort of data that is stored in that entity. In the diagram below, the attributes of our CustomerAddress table are NAME, ADDRESS, and TEL#. In the relational database we refer to these attributes as columns or fields. If we take our object analogy further, then each instance of our entity will have identical attributes but will be uniquely characterized by the values of these attributes. In the database, an instance of our object is analogous to a row, or record, in the table. The following diagram illustrates a table and its associated nomenclature...

Meet the Author

Since becoming a professional software developer, Kevin Mukhar has programmed a client-server based training system in C and C++. He then worked on a large-scale data management system using an Oracle database to manage terabytes of binary data. The front-end was developed as a web application employing C++ and Java. This was followed by an all Java system for dynamically finding and displaying data from distributed data repositories. His latest job is developing Java intranet web applications using J2EE technologies. The web applications provide customer management for a company with millions of customers.

Todd Lauinger is a freelance instructor, mentor, conference speaker, and published author. He is currently employed as a Software Construction Fellow at Best Buy Co., Inc., working there to establish a common infrastructure for all Java-related software development activities in the enterprise. Todd has his Masters degree in Software Engineering, along with over 10 years of experience developing large, mission-critical software systems for engineering and business organizations.

John Carnell is currently working as a Senior Systems Architect for Workscape, a leading provider of HR and Employee Benefits self-service solutions. John's favorite topic of discussion, much to his wife's chagrin, is component based, N-Tier architectures. John has extensive experience with both the Microsoft, Oracle, and Java N-Tier solutions. John can be reached at john_carnell@yahoo.com.

Customer Reviews

Average Review:

Write a Review

and post it to your social network

     

Most Helpful Customer Reviews

See all customer reviews >