How to develop JDBC Application
To develop any JDBC application, we follow the following five steps which are as follows:
- Load and register the driver class
- Creating connection
- Creating statement
- Executing queries
- Closing connection
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 :
Note: Here we are using type-4 (thin) driver.
The getConnection() method of DriverManager class is used to establish connection with the database.
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:
Here by the createStatement() method, we prepare the SQL statement which is assigned in Statement object st.
Executing the SQL statements:
After preparing SQL statements, then we execute the SQL statements by executeQuery() or executeUpdate() or execute() .
executeQuery() returns ResultSet(an interface present in java.sql package) object.
executeUpdate() returns an int value.
execute() returns a Boolean value(true / false).
ResultSet rs = rs.executeQuery("select * from employee");
After executing this statement, the results are stored in rs.
By closing connection object statement and ResultSet will be closed automatically. The close() method of Connection interface is used to close the connection.
Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Projects
SILAN Technology Call to: 9439202111 | 9658317757 | 0674-2361252