What are different steps used in JDBC? Write down a small program showing all steps.
The fundamental steps involved in the process of connecting to a database and executing a query consist of the following:
- Import JDBC packages.
- Load and register the JDBC driver.
- Open a connection to the database.
- Create a statement object to perform a query.
- Execute the statement object and return a query resultset.
- Process the resultset.
- Close the resultset and statement objects.
- Close the connection.
These steps are described in detail in the sections that follow.
1. Import JDBC Packages
This is for making the JDBC API classes immediately available to the application program. The following import statement should be included in the program irrespective of the JDBC driver being used:
import java.sql.*;
Additionally, depending on the features being used, Oracle-supplied JDBC packages might need to be imported. For example, the following packages might need to be imported while using the Oracle extensions to JDBC such as using advanced data types such as BLOB, and so on.
import oracle.jdbc.driver.*;
import oracle.sql.*;
2. Load and Register the JDBC Driver
This is for establishing a communication between the JDBC program and the Oracle database. This is done by using the static registerDriver() method of the DriverManager class of the JDBC API. The following line of code does this job:
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
JDBC Driver Registration
For the entire Java application, the JDBC driver is registered only once per each database that needs to be accessed. This is true even when there are multiple database connections to the same data server.
Alternatively, the forName() method of the java.lang.Class class can be used to load and register the JDBC driver:
Class.forName("oracle.jdbc.driver.OracleDriver");
However, the forName() method is valid for only JDK-compliant Java Virtual Machines and implicitly creates an instance of the Oracle driver, whereas the registerDriver() method does this explicitly.
3. Connecting to a Database
Once the required packages have been imported and the Oracle JDBC driver has been loaded and registered, a database connection must be established. This is done by using the getConnection() method of the DriverManager class. A call to this method creates an object instance of the java.sql.Connection class. The getConnection() requires three input parameters, namely, a connect string, a username, and a password. The connect string should specify the JDBC driver to be yes and the database instance to connect to.
The getConnection() method is an overloaded method that takes
Three parameters, one each for the URL, username, and password.
Only one parameter for the database URL. In this case, the URL contains the username and password.
The following lines of code illustrate using the getConnection() method:
Connection conn = DriverManager.getConnection(URL, username, passwd);
Connection conn = DriverManager.getConnection(URL);
4. Querying the Database
Querying the database involves two steps: first, creating a statement object to perform a query, and second, executing the query and returning a resultset.
a)Creating a Statement Object
This is to instantiate objects that run the query against the database connected to. This is done by the createStatement() method of the conn Connection object created above. A call to this method creates an object instance of the Statement class. The following line of code illustrates this:
Statement sql_stmt = conn.createStatement();
b) Executing the Query and Returning a ResultSet
Once a Statement object has been constructed, the next step is to execute the query. This is done by using the executeQuery() method of the Statement object. A call to this method takes as parameter a SQL SELECT statement and returns a JDBC ResultSet object. The following line of code illustrates this using the sql_stmt object created above:
ResultSet rset = sql_stmt.executeQuery
("SELECT empno, ename, sal, deptno FROM emp ORDER BY ename");
Alternatively, the SQL statement can be placed in a string and then this string passed to the executeQuery() function. This is shown below.
String sql = "SELECT empno, ename, sal, deptno FROM emp ORDER BY ename";
ResultSet rset = sql_stmt.executeQuery(sql);
Statement and ResultSet Objects
Statement and ResultSet objects open a corresponding cursor in the database for SELECT and other DML statements.
The above statement executes the SELECT statement specified in between the double quotes and stores the resulting rows in an instance of the ResultSet object named rset.
5. Processing the Results of a Database Query That Returns Multiple Rows
Once the query has been executed, there are two steps to be carried out:
a)Processing the output resultset to fetch the rows
b) Retrieving the column values of the current row
The first step is done using the next() method of the ResultSet object. A call to next() is executed in a loop to fetch the rows one row at a time, with each call to next() advancing the control to the next available row. The next() method returns the Boolean value true while rows are still available for fetching and returns false when all the rows have been fetched.
The second step is done by using the getXXX() methods of the JDBC rset object. Here getXXX() corresponds to the getInt(), getString() etc with XXX being replaced by a Java datatype.
Closing the ResultSet and Statement
Once the ResultSet and Statement objects have been used, they must be closed explicitly. This is done by calls to the close() method of the ResultSet and Statement classes. The following code illustrates this:
rset.close();
sql_stmt.close();
If not closed explicitly, there are two disadvantages:
- Memory leaks can occur
- Maximum Open cursors can be exceeded
Closing the ResultSet and Statement objects frees the corresponding cursor in the database.
6. Closing the Connection
The last step is to close the database connection opened in the beginning after importing the packages and loading the JDBC drivers. This is done by a call to the close() method of the Connection class.
The following line of code does this:
conn.close();
Explicitly Close your Connection
Closing the ResultSet and Statement objects does not close the connection. The connection should be closed by explicitly invoking the close() method of the Connection class.
- class.forName("fully qualified classname")
- DriveManager.registerDriver(object of driver class)
- URL
- username
- password
Comments
Post a Comment