In this article, we will learn some of the core components of JDBC
JDBC Core components :
- DriverManager
- Driver
- Connection
- Statement
- ResultSet
1. DriverManager :
- DriverManager is a mean through which it connects to databases using appropriate driver supplied from Java application
- DriverManager registers all driver requests comes from Java application and picks up appropriate driver to connect to database
public static void registerDriver(Driver driver);
- DriverManager has number of useful static methods to get connection objects, these are
public static Connection getConnection(String dbUrl);
public static Connection getConnection(String dbUrl, String username,String password);
public static Connection getConnection(String dbUrl, Properties props);
2. JDBC Driver :
- Drivers are the one which actually communicates with the databases
- But user generally never interact with drivers directly
- Instead, user go through DriverManager to register and de-register drivers and then communicate with database
3. Connection :
- After registering drivers, DriverManager has got number of static getConnection() method to return Connection object
- It is only after successfully getting connection object, user can interact with database
- Connection interface also got some useful methods like setAutoCommit(boolean), commit(), rollback() methods to handle transaction
- Using connection object, we can create/prepare all three types of statements i.e.;
createStatement(); // for Statement
prepareStatement(); // for PreparedStatement
prepareCall(); // for CallableStatement
4. Statement :
- Statement interface is the one which is actually used to execute queries like inserting new record or updating an existing record
- executeQuery(String sqlQuery); and executeUpdate(String sqlQuery) are the most commonly used methods from Statement interface
- executeQuery(String sqlQuery); returns object of ResultSet
- There is also executeBatch(); method for batch execution
5. ResultSet :
- ResultSet holds data retrieved from database when performing/executing queries (both SQL and MySQL)
- You can traverse in ResultSet to read data one-by-one, but in forward direction only
- But by setting static variable “TYPE_SCROLL_INSENSITIVE” in createStatement(), we can make it scrollable in both directions
- Similarly, by setting static variable “CONCUR_UPDATABLE” in createStatement(), we can make ResultSet updatable, as it is not updatable by default
Related Articles :
- Java – Introduction to JDBC
- Java – JDBC Driver types
- Java – Core JDBC components
- Java – JDBC Driver list for all leading database
- Java – JDBC connection steps
- Java – An example to connect MySQL database
- Java – An example to connect Oracle database
- Java – An example to connect MS Access database
- Java 8 – An example to connect MS Access database in Java 8
- Java – JDBC program to connect IBM DB2 database running on Mainframe z/OS system
- Java – Creating database using JDBC Statement interface
- Java – Droping database using JDBC Statement interface
- Java – Creating a table using JDBC Statement interface
- Java – Inserting a record using JDBC Statement interface
- Java – Getting all list of records using JDBC Statement interface
- Java – Getting single record using JDBC Statement interface
- Java – Updating a record using JDBC Statement interface
- Java – Deleting a record using JDBC Statement interface
- Java – Dropping a table using JDBC Statement interface
- Java – Batch update using JDBC Statement interface
- Java – Batch insert using JDBC Statement interface
- Java – Creating a table using JDBC PreparedStatement interface
- Java – Inserting a record using JDBC PreparedStatement interface
- Java – Getting all list of records using JDBC PreparedStatement interface
- Java – Getting single record using JDBC PreparedStatement interface
- Java – Updating a record using JDBC PreparedStatement interface
- Java – Deleting a record using JDBC PreparedStatement interface
- Java – Batch update using JDBC PreparedStatement interface
- Java – Batch insert using JDBC PreparedStatement interface
- Java – Calling Stored Procedure using JDBC CallableStatement interface
- Java – Calling Stored Function using JDBC CallableStatement interface
- Java – Calling Stored Procedure using JDBC CallableStatement interface with Batch execution
- Java – Transaction handling using JDBC Statement interface
- Java – Transaction handling using JDBC PreparedStatement interface
- Java – Integration with Spring framework (Spring JDBC)
- Java – Where clause example using JDBC Statement interface
- Java – Like clause example using JDBC Statement interface
- Java – Order by clause example using JDBC Statement interface
- Java – Metadata of database using JDBC DatabaseMetaData interface
- Java – Metadata of Resultset using JDBC ResultSetMetaData interface
- Java – Interview question and answer on JDBC
References :
- https://docs.oracle.com/javase/tutorial/jdbc/basics/gettingstarted.html
- https://docs.oracle.com/cd/E11882_01/java.112/e16548/overvw.htm#JJDBC28025
- http://docs.oracle.com/javase/tutorial/jdbc/basics/index.html
- https://en.wikipedia.org/wiki/JDBC_driver
- http://www.devx.com/tips/Tip/28818
- https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#forName(java.lang.String)
Happy Coding !!
Happy Learning !!