Creating JDBC Application:
There are six steps involved in building a JDBC application which I'm going to brief in this tutorial:
Import the packages:
This requires that you include the packages containing the JDBC classes needed for database programming. The package needed for JDBC is java.sql.*
Register the JDBC driver:
This requires that you initialize a driver so you can open a communications channel with the database.
The driver for MySql is "com.mysql.jdbc.Driver"
The driver for Oracle is "oracle.jdbc.driver.OracleDriver"
Open a connection:
This requires using the DriverManager.getConnection() method to create a Connection object, which represents a physical connection with the database. This method requires Database URL, username, Password.
Execute a query:
This requires using an object of type Statement or PreparedStatement for building and submitting an SQL statement to the database
Extract data from result set:
This step is required in case you are fetching data from the database. You can use the appropriate ResultSet.getXXX() method to retrieve the data from the result set
Clean up the environment:
You should explicitly close all database resources versus relying on the JVM's garbage collection
No comments:
Post a Comment