Reading and Writing Data Using JDBC and XML
(Page 1 of 5 )
For those who want to delve deeply into Java, this article explores Java Database Connectivity (JDBC), a class library that connects Java programs to relational databases. The first of three parts, it is excerpted from chapter 20 of the book
Sams Teach Yourself Java 2 in 21 Days, 4th Edition, written by Rogers Cadenhead and Laura Lemay (Sams; ISBN: 0672326280).
Almost all Java programs deal with data in some way. You have used primitive types, objects, arrays, linked lists, and other data structures up to this point.
Today, you will work with data in a more sophisticated way by exploring Java Database Connectivity (JDBC), a class library that connects Java programs to relational databases, and Extensible Markup Language (XML), a formatting standard that enables data to be completely portable.
You'll explore JDBC and XML in the following ways:
Using JDBC drivers to work with different relational databases
Accessing a database with Structured Query Language (SQL)
Reading records from a database using SQL and JDBC
Adding records to a database using SQL and JDBC
Representing data as XML
Discovering why XML is a useful way to store data
Using XML to create a new data format
Reading and writing XML data
Java Database ConnectivityJava Database Connectivity (JDBC) is a set of classes that can be used to develop client/server applications that work with databases developed by Microsoft, Sybase, Oracle, Informix, and other sources.
With JDBC, you can use the same methods and classes in Java programs to read and write records and perform other kinds of database access. A class called a driver acts as a bridge to the database source—there are drivers for each of the popular databases.
Client/server software connects a user of information with a provider of that information, and it's one of the most commonplace forms of programming. You use it every time you surf the Web: A Web browser client requests pages, image files, and other documents using a Uniform Resource Locator, or URL. Web servers provide the requested information, if it can be found, for the client.
One of the biggest obstacles faced by database programmers is the wide variety of database formats in use, each with its own proprietary method of accessing data.
To simplify using relational database programs, a standard language called SQL (Structured Query Language) has been introduced. This language supplants the need to learn different database-querying languages for each database format.
In database programming, a request for records in a database is called a query. Using SQL, you can send complex queries to a database and get the records you're looking for in any order you specify.
Consider the example of a database programmer at a student loan company who has been asked to prepare a report on the most delinquent loan recipients. The programmer could use SQL to query a database for all records in which the last payment was more than 180 days ago and the amount due is more than $0.00. SQL also can be used to control the order in which records are returned, so the programmer can get the records in the order of Social Security number, recipient name, amount owed, or another field in the loan database.
All this is possible with SQL—the programmer doesn't need any of the proprietary languages associated with popular database formats.
Caution - SQL is strongly supported by many database formats, so in theory you should be able to use the same SQL commands for each database tool that supports the language. However, you still might need to learn some idiosyncrasies of a specific database format when accessing it through SQL.
SQL is the industry-standard approach to accessing relational databases. JDBC supports SQL, enabling developers to use a wide range of database formats without knowing the specifics of the underlying database. JDBC also supports the use of database queries specific to a database format.
The JDBC class library's approach to accessing databases with SQL is comparable to existing database-development techniques, so interacting with an SQL database by using JDBC isn't much different than using traditional database tools. Java programmers who already have some database experience can hit the ground running with JDBC.
The JDBC library includes classes for each of the tasks commonly associated with database usage:
Making a connection to a database
Creating a statement using SQL
Executing that SQL query in the database
Viewing the resulting records
These JDBC classes are all part of the java.sql package in Java 2.
Next: Database Drivers >>
More Java Articles
More By Sams Publishing
|
This article is excerpted from chapter 20 of the book Sams Teach Yourself Java 2 in 21 Days, 4th Edition, written by Rogers Cadenhead and Laura Lemay (Sams; ISBN: 0672326280). Check it out today at your favorite bookstore. Buy this book now.
|
|