Seven Steps to JDBC

      The java program can access the database using JDBC. the seven steps that are needed to access database are

  1. Import the package
  2. Load and register the driver
  3. Establish the Connection
  4. Create a Statement object
  5. Execute a query
  6. Process the result
  7. Close the connection

Step1: Import java.sql package

          The important classes and interfaces of JDBC are available in the java.sql package . So inorder to access the databases it must be imported in our application.

 import java.sql.*;

Step 2: Loading and registering the driver

           In order to access database it must have the particular driver for that database. In Java we have to inform the Drive Manager about the required driver this is done with the help of the method forName(String str) available in  the class named Class

Class.forName(“path with driver name”);

          In an application the user can register more than one driver. The statement used to load and register the JDBC-ODBC bridge driver is

Step 3: Establishing the connection

           Establishing connection means making a connection to access RDBMS through JDBC driver. The connection can be established with the help of getConnection() method available in DriverManager class. The established connection is then set to new connection object.

Connection cn=DriverManager.getConnetion (“jdbc:odbc:DSN”, ”administrator”, “password”);

            Where DSN-Data Source Name

  When the statement is executed the connection is set to the Connection object cn. For example to connect the ODBC data source named mydata via JDBC –ODBC bridge the statement is
          Connection cn= DriverManager.getConnection (“jdbc:odbc:mydata”);

Step 4: Creating the statement

        A Statement object is used for executing a static SQL statement and obtaining the results produced by it. The createStatement() method is used to create statements using the established connection.

The methods used for creating the statements are

  • Statement createStatement()

Returns a new Statement object.Used for general queries

  • PreparedStatement prepareStatement(String sql)

Returns a new PreparedStatement object.For a statement called multiple times with different values (precompiled to reduce parsing time)

  • CallableStatement prepareCall(String sql)

Returns a new CallableStatement object for stored procedures

Step 5: Executing the Query

          After making connection to the database we are ready to execute the SQL statements. The  various methods available in Statement object to execute the query are

  • ResultSet executeQuery(String)

           Execute a SQL statement that returns a single ResultSet. After executing the SQL statements the requested data is stored in the ResultSet object.

  • int executeUpdate(String)

          Execute a SQL INSERT, UPDATE or DELETE statement. Returns the number of rows changed.

  • boolean execute(String)

           Execute a SQL statement that may return multiple results.

Step 6: Retrieving the result.

          A ResultSet provides access to a table of data generated by executing a Statement.Only one ResultSet per Statement can be open at once.The table rows are retrieved in sequence. A ResultSet maintains a cursor pointing to its current row of data. The 'next' method moves the cursor to the next row.


  • public boolean first()

Moves the cursor to the first row in this ResultSet object.

  • public boolean last()

Moves the cursor to the last row in this ResultSet object.

  • public boolean next()

Moves the cursor down one row from its current position.

The methods used to retrieve the values from the current row

  • Type getType(int columnIndex)

Returns the given field as the given type. Fields indexed starting at 1 (not 0)

  • Type getType(String columnName)

Returns the data at the specified column

In the above methods the Type should be replaced by the valid datatypes such as Boolean, String, Int, Long, Float etc as getString(Column name), getInt(column index) etc.

Step 7: Closing the connection and statement

          When the client request is completed we have to close the created objects of Connection and Statement using close() method.



 JDBC ----PowerPoint:




From the Web

Suggestions: (1)

See few results | more results

Search: [Press Enter]

Share |