Home arrow Java arrow Page 2 - Database Programming in Java Using JDBC
JAVA

Database Programming in Java Using JDBC


An application that does not persist its data in a database is a rarity. Programming languages reflect this trend. That's why all languages provide a robust and flexible library for database access. Java is no exception.

Author Info:
By: A.P.Rajshekhar
Rating: 4 stars4 stars4 stars4 stars4 stars / 56
August 14, 2007
TABLE OF CONTENTS:
  1. · Database Programming in Java Using JDBC
  2. · Accessing the Database using JDBC, Step by Step
  3. · Accessing the Database using JDBC continued
  4. · Using JDBC in the Real World

print this article
SEARCH DEVARTICLES

Database Programming in Java Using JDBC - Accessing the Database using JDBC, Step by Step
(Page 2 of 4 )

The best part of using JDBC for database programming is that if one has the required type of driver, regardless of the database server, the steps to connect and query the database remain more or less the same. The steps to access database for a typical relational database server include:

  1. Loading the driver. 
  2. Creating a connection.
  3. Instantiating a statement object.
  4. Retrieving a resultset object.
  5. Accessing the data from the resultset object.

All of these steps are the same for any database, be it Oracle or MySQL. The only change comes in the query to be passed in step four. Here are the details.

 Loading the Driver

The driver, regardless of type, can be loaded in one of two ways: using the Class loader, or explicitly creating the instance. The difference between them, apart from how the driver is instantiated, is whether the Driver has to be registered explicitly or not.

Using the Class loader

A class can be loaded at runtime using the forName() method of the Class class. The method accepts a String having the name of the class to be loaded. Also once the class is loaded, calling the newInstance() method of Class will create a new object of the loaded class. When the forName() method is used, the Driver need not be registered explicitly. For example, to instantiate a driver of Type I using the forName method, the statement would be:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();

Explicitly creating the instance

The second way to load a driver is to instantiate it explicitly using the new operator. This is similar to that of creating a new instance of any class. However, when the driver is being explicitly instantiated, one will have to register the driver with the runtime environment using the register() method of the DriverManager class. For example to load Type I Driver, the statements would be:

Driver driver=new sun.jdbc.odbc.JdbcOdbcDriver();

DriverManager.register(driver);

Or the statements can be merged as:

DriverManager.register(new sun.jdbc.odbc.JdbcOdbcDriver());

Once the driver is loaded and registered, the next step is to get a connection.

Creating a Connection

Once the driver has been loaded and registered, the next step is creating a connection with the database server. The connection is created when one creates an instance of Connection. To get an instance of Connection, the getConnection() method of the DriverManager class has to be called. In reality, Connection is an interface and when getConnection() is called, the DriverManger provides an instance of the proper implementing class to a reference variable of Connection. There are three forms of the getConnection() method which are:

  • getConnection(String url) - the URL contains all the necessary information including the URL of the database server, user name and password.
  • getConnection(String url, Properties info) - the URL contains only the URL to the server. The user name and password are passed as part of the Properties instance. 
  • getConnection(String url, String user, String password) - as with the previous form, this form also contains the URL to the server in the URL parameter. The user name and password are passed as separate parameters.

The URL is of the form jdbc:<subprotocol>:subname where subprotocol refers to the protocol used by the database server and subname refers to the database to which the connection has to be made. For example, an Oracle subname refers to the tablespace within the database server. So in order to create a connection using the ODBC data source name or DSN, the statement would be:

Connection connection = DriverManager.getConnection ("jdbc:odbc:test",

"test1", "test123");

where odbc is the subprotocol and test is the DSN which points to the database to connect to. The next step is to create a statement object.


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