1 of 6

ADVANCED JAVA PROGRAMMING

Steps for writing a JDBC program

Smt. M.Jeevana Sujitha

Assistant Professor

Department of Computer Science and Engineering

SRKR Engineering College, Bhimavaram, A.P. - 534204

2 of 6

OBJECTIVES

The Objective of this lecture is

  • To learn about steps for writing a JDBC program.

3 of 6

Steps for Writing a JDBC Program

Step-1: Register the driver or loading the driver

  • To load or register the driver we can use any one of the following methods.

Syntax:

Class.forName(“url”); DriverManager.registerDriver(“url”); System.setProperty(“url”);

Step-2: Establishing the connection to a database

  • In this step we establish a connection with a specific database. The connection establishment can be done by Connection Interface.

Syntax:

Connection con=DriverManager.getConnection(“url”);

4 of 6

Steps for Writing a JDBC Program

Step-3: Preparing SQL statement

  • In this step we should create SQL statement in our java program using any one of the interface like Statement or PreparedStatement or CallableStatement.

Syntax:

Statement st=con.createStatement();

PreparedStatement ps=con.prepareStatement(“sql query”);

Step-4: Executing the SQL statement

  • For this purpose we use execute(), executeQuery() and executeUpdate() methods of Statement interface.

Syntax:

Boolean b=st.execute(“sql query”);

int i=st.executeUpdate(“insert or delete or update query”);

ResultSet rs=st.executeQuery(“select query”);

5 of 6

Steps for Writing a JDBC Program

Step-5: Retrieving the Results

  • The results obtained by executing the sql statement can be

stored in an object with the help of ResultSet interface.

Syntax:

ResultSet rs=st.executeQuery(“select * from table name”);

  • ResultSet object maintains a cursor.
  • When ResultSet object is first created, then the cursor is

pointed before the first row of table.

  • In order to move the cursor to next row we need to use next()

method.

Step-6: Close the Connection

  • To close the connection between java program and database we use one method close() of Connection interface.

Syntax:

con.close();

6 of 6

THANK YOU