Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Develop JDBC Application

To develop any JDBC application, we follow the following five steps which are as follows:

  • 1. Load and register the driver class
  • 2. Establish the connection
  • 3. Create the statement object
  • 4. Execute the sql statements
  • 5. Close the connection

Let’s analyze the above steps:

Load and Register the driver class:

We know database driver is a software which is written according to the JDBC API. There are different ways to register a driver. Here I have presented one way by considering Oracle database that is to send the driver class name directly to forName() method as :

Class.forName(“oracle.jdbc.driver.OracleDriver”);

Note: Here we are using type-4 (thin) driver.


Establish the connection:

The getConnection() method of DriverManager class is used to establish connection with the database.
Connectioncon=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:xe”,”system”,”password”);


Create the statement object:

After connecting to the database, we prepare SQL statements by creating an object of type Statement or PreparedStatement or CallableStatement (an interface available in java.sql package) for performing different operations on the database. For example:

Example:

Statement st=con.createStatement();

Here by the createStatement() method, we prepare the SQL statement which is assigned in Statement object st.


Execute the SQL statements:

After preparing SQL statements, then we execute the SQL statements by executeQuery()or executeUpdate() or execute().

  • • executeQuery() return ResultSet(an interface present in java.sql package) object.
  • • executeUpdate() returns an int value.
  • • execute() returns a Boolean value(true / false).

Example:

ResultSet rs = rs.executeQuery(“select * from employee”);

After executing this statement, the results are stored in rs.


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.

Example:

con.close();


About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc







 PreviousNext