Steps to Write JDBC Program

Google Advertisements

Prev Tutorial Next Tutorial

Steps to write JDBC Program

There are 6 steps to connect any java application with the database using JDBC. They are as follows:

  1. Load the JDBC driver class or register the JDBC driver.
  2. Establish the connection
  3. Create a statement
  4. Execute the sql commands on database and get the result
  5. Print the result
  6. Close the connection

1. Register the driver class

In this step we load the JDBC driver class into JVM. This step is also called as registering the JDBC driver. The forName() method of Class class is used to register the driver class. This method is used to dynamically load the driver class. This step can be completed in two ways.

  • class.forName("fully qualified classname")
  • DriveManager.registerDriver(object of driver class)

Syntax of forName() method

public static void forName(String className)throws ClassNotFoundException  

Sun.Jdbc.Odbc.JdbcOdbcDriver is a driver class provided by Sun MicroSystem and it can be loaded into jvm like the following.




Sun.Jdbc.Odbc.JdbcOdbcDriver jod=new Sun.Jdbc.Odbc.JdbcOdbcDriver();

2. Create the connection object

In this step connection between a java program and a database will be opened. To open the connection, we call getConnection() method of DriverManager class.
For getConnection() method we need to pass three parameters.

  • url
  • username
  • password

url: url is used to select one register JDBC driver among multiple registered driver by DriverManager class.

username and password: username and password are used for authentication purpose.

Syntax of getConnection() method

1) public static Connection getConnection(String url)throws SQLException  
2) public static Connection getConnection(String url,String name,String password)  
throws SQLException  

Example to establish connection with the Oracle database

Connection con=new DriverManager.getConnection(url, username, password);
Connection con=new DriverManager.getConnection(Jdbc:Odbc:< dsn >", "scott","tiger");

3. Create the Statement object

To transfer sql commands from java program to database we need statement object. To create a statement object we call createStatement() method of connection interface. The createStatement() method of Connection interface is used to create statement. The object of statement is responsible to execute queries with the database.

Syntax of createStatement() method

public Statement createStatement()throws SQLException  

Example to create the statement object

Statement stmt=new createStatement();

4. Executing queries

Call any one of the following three methods of Statement interface is used to execute queries to the database and to get the output.

  • executeUpdate(): Used for non-select operations.
  • executequery(): Used for select operation.
  • execute(): Used for both select or non-select operation.
steps to write jdbc program

5. Print the result.



6.Closing connection : Close the connection.

By closing connection object statement and ResultSet will be closed automatically. The close() method of Connection interface is used to close the connection.

Syntax of close() method

Syntax of close() method

public void close()throws SQLException  

Example for close connection



import java.sql.*;
class CreateTable 
public static void main(String[] args) throws Exception
System.out.println("driver is laoded");
Connection con=DriverManager.getConnection("jdbc:odbc:ramadsn","system","system");
System.out.println("connection is established");
Statement stmt=con.createStatement();
System.out.println("statement object is cretaed");
int i=stmt.executeUpdate("create table student(sid number(3),sname varchar2(10),marks number(5))");
System.out.println("Result is="+i);
System.out.println("table is created");
con .close();

Prev Tutorial Next Tutorial

Google Advertisements

We have more than 5 Groups which have 500000+ Members and some Pages have more than 100000+ members. If you want to post your article on our website/FB Group/FB Page please contact on [email protected] or 8076671483

Buy This Ad Space @$50 per Month, Ad Size 600X200 Contact on: [email protected] or 8076671483

Path variable is set for providing path for all Java tools like java, javac, javap, javah, jar, appletviewer which are used in java programming. All these tools are available in bin folders so we set path upto bin folders.

Classpath variable is set for providing a path for predefined Java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar.

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user.

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects.

Download Projects

Adsense Advertisements

Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: [email protected] or 8076671483 Try this Keyword C++ Programs