Home arrow Java arrow Page 5 - Database Programming With Java + JDBC: Part 1/2
JAVA

Database Programming With Java + JDBC: Part 1/2


No Java programmer is complete without knowledge of Java database connectivity, or JDBC for short. In this 2 part article Nitin teaches us JDBC from the inside out.

Author Info:
By: Nitin Patil
Rating: 4 stars4 stars4 stars4 stars4 stars / 44
August 20, 2002
TABLE OF CONTENTS:
  1. · Database Programming With Java + JDBC: Part 1/2
  2. · What is JDBC?
  3. · Before We Start Coding
  4. · Our Address Book App (contd.)
  5. · The Sample Code... Explained
  6. · Conclusion

print this article
SEARCH DEVARTICLES

Database Programming With Java + JDBC: Part 1/2 - The Sample Code... Explained
(Page 5 of 6 )

There are two classes in our sample application:
  1. AddressBookEntry: Represents an entry in the database.
  2. JDBCDemo1: The demo class.
The demo class puts forth several interesting aspects of JDBC programming. Let's take a look at them one by one.

First of all, the main() method creates a database connection to be used later in the database operations. This is done via the getConnection() method, which loads the driver and uses a database URL to connect to the database.

Most JDBC drivers register themselves with java.sql.DriverManager when their class is loaded. Moving on, we then try to obtain a connection using the DriverManager class by supplying a database URL. This URL is in the following format:

jdbc:subprotocol:subname

... where subrotocol lets you specify which driver to use, and rest of the URL provides information to the driver about the database it should connect to. The DriverManager.getConnection() method also lets you specify the user ID and password to connect to the database. For the sake of simplicity, we aren't using any.

The main() method presents a simple text-based menu to the user. Based on the choice (1-4), the method determines, what to do. Initially (when running this application for the first time), we should create the "address_book" table using choice 1. This fires the createTable() method.

It creates a java.sql.Statement object using the connection's createStatement() method. It then uses this statement to fire the SQL statement, using the executeUpdate() method. This simple looking code has actually created a table in the "test_db".

Pay special attention to the finally block of the method. It checks whether the statement object was created. If yes, it closes it. This releases the system resources associated with the statement object. It's extremely important to make sure you release such crucial system resources as soon as possible (unless your program has been designed to defer this). But why do we do this inside of the finally block? Well, this is simply because it's executed in all cases, except when there's an unusual termination of the program, such as when the user hits the "Ctrl+C" key sequence.

Continuing on the same line, choice 2 lets you add an entry to the database. This is done in the add() method, which takes the nick name, name, and email as inputs. Again, the logic is similar to the createStatement() method, except this time we are firing an insert statement.

Choice 3 lets you search an entry in the addressbook with the specified nick name. The lookup() method provides this functionality. Notice that in this case we are firing the executeQuery() method. This facilitates executing a select query and extracting the results.

JDBC provides a nice object view of the fetched rows via a java.sql.ResultSet object. The first next() call provides information about the first row in the result. ResultSet provides numerous getXXX() types of methods to retrieve the value of a column as an appropriate data type. For instance, here we are using getString() to retrieve the values as java.lang.String objects.

Each getXXX() method takes the index of the column in the result whose value is to be retrieved. You can also specify the name of the column instead of its index. Did you notice something unusual in the code here?

Got it? The argument to the first getString() call is 1, not 0. This is one of the most common traps that many Java developers fail to realize initially, so sock it home that the index used as arguments to the JDBC APIs are 1-based.

When you prefer to quit the application, simply choose 4 and the main() function will close the database connection and exit.
blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials