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.
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:
Loading the driver.
Creating a connection.
Instantiating a statement object.
Retrieving a resultset object.
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:
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:
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: