JDBC Interview Questions and answers latest 2022
- What are the different frequent JDBC classes or interfaces used in Java code? There are below interfaces or classes in JDBC used by developers such as Connection, DriverManager, ResultSet, Statement, PreparedStatement, and CallableStatement.
What are different driver types supported in JDBC?
Driver Type | Description |
---|---|
JDBC-ODBC bridge | Acts as a bridge between client code and the database machine |
Type2 driver | Requires client-side installation to connect to the database |
Type3 | Utilizes middleware network to convert JDBC calls to native calls |
Pure JavaDriver | Written entirely in Java, it converts Java code into native database-specific operations |
- How do you load the JDBC driver class?
To load the JDBC driver class into memory, follow these steps.
- Import jdbc classes into your code such
- Use Class.forName(“DriverClass”) to load the driver class.
- Once DriverClass is loaded, create a new instance of DriverClass and register the driver with the DriverManager class.
- After registration, the driver is prepared to create physical connections to a database using the Connection object.
What are the different types of statements in JDBC?
There are three types of JDBC Statements.
Type | Description |
---|---|
Statement | A factory class used to create a statement for executing SQL queries at runtime |
PreparedStatement | Used to execute SQL queries with multiple query parameters, enhancing performance by avoiding recompilation |
CallableStatement | Creates statements to execute native database procedures, which can be run with or without parameters |
What is connection pooling?
Creating a database connection is akin to opening a socket connection to a remote machine, thus this call is costly.
When performing CRUD operations (create, read, update, and delete) on data within the database, the Connection object is utilized. However, establishing a connection for each operation proves to be expensive. To mitigate this, it’s advisable to initiate a pool of connections during the initial state of the application startup.
Whenever an application requires a database operation, a connection from the pool is reused. Once the operation concludes, instead of closing the connection (as would be the case with a normal connection), it is returned to the pool. This practice serves to reduce Database calls and enhance overall performance.
How do you store images and file types in the database?
There are two main types of data: images and files (such as Excel, Word, etc.).
Images are typically stored using BLOB (Binary Large Object), while files are stored using CLOB (Character Large Object).
A CLOB type is used to store character data types in the database.
A BLOB type is utilized to store data in the form of binary data. This includes images, audio, and video files, which are converted into binary format before being stored.
Please write down the steps required to connect to Database
Here are steps load the JDBC driver and make a connection to the database.
- add the required JDBC driver via maven or ant.
- Import java classes
- Load Driver using class.forName() class
- Create a connection object
- with connection object, Create Statement object
- Execute the statement object and returns the result
- Process the result
- Finally, close the database connection.
What are resultSet types in java JDBC?
ResultSet is a result of database queries operation. There are different types provided to navigate the resultset
Type | Description |
---|---|
Forward-only | Allows forward cursor movement only, with no scrolling capability |
Scroll-insensitive | Cursor can be moved in any direction, and changes made do not affect the database |
Scroll-sensitive | Cursor moves both forward and backward, with changes reflected in the database |
What are ResultSet Concurrency types?
Type | Description |
---|---|
ResultSet.CONCUR_READ_ONLY | Allows only read operations, prohibiting updates |
ResultSet.CONCUR_UPDATABLE | Allows both read and update operations |