JDBC Steps ? Basic steps in writing a JDBC Application

This section gives you brief description of JDBC Steps for making connection with the database, executing the query and showing the data to the user. In this application we have connected to the MySQL database and retrieved the employee names from the dat

This section gives you brief description of JDBC Steps for making connection with the database, executing the query and showing the data to the user. In this application we have connected to the MySQL database and retrieved the employee names from the dat

JDBC Steps – Basic steps in writing a JDBC Application

This section gives you brief description of JDBC Steps for making connection with the database, executing the query and showing the data to the user. In this application we have connected to the MySQL database and retrieved the employee names from the database. Here are the JDBC Steps to be followed while writing JDBC program:

Loading Driver

Establishing Connection

Executing Statements

Getting Results

Closing Database Connection

Before explaining you the JDBC Steps for making connection to the database and retrieving the employee from the tables, we will provide you the structure of the database and sample data.

Here is the sql script to create table and populate the table with data:

Loading Driver
Loading Database driver is very first step towards making JDBC connectivity with the database. It is necessary to load the JDBC drivers before attempting to connect to the database. The JDBC drivers automatically register themselves with the JDBC system when loaded. Here is the code for loading the JDBC driver:
Class.forName(driver).newInstance();

Establishing Connection
In the above step we have loaded the database driver to be used. Now its time to make the connection with the database server. In the Establishing Connection step we will logon to the database with user name and password. Following code we have used to make the connection with the database:
con = DriverManager.getConnection(url+db, user, pass);

Executing Statements
In the previous step we established the connection with the database, now its time to execute query against database. You can run any type of query against database to perform database operations. In this example we will select all the rows from employee table. Here is the code that actually execute the statements against database: ResultSet res = st.executeQuery( "SELECT * FROM employee" );

Getting Results
In this step we receives the result of execute statement. In this case we will fetch the employees records from the recordset object and show on the console. Here is the code: while (res.next()) { String employeeName = res.getInt( " employee_name " );
System.out.println( employeeName );
}

Closing Database Connection
Finally it is necessary to disconnect from the database and release resources being used. If you don’t close the connection then in the production environment your application will fail due to hanging database connections. Here is the code for disconnecting the application from database: con.close();

In this section you learnt about the JDBC Steps necessary for performing database operations.