Java Database Connectivity or JDBC is a Java API to hyperlink and execute a question with the database. In different phrases, it’s an Utility Programming Interface (API) for the Java platform, which is helping with interplay and for executing the SQL question. In case you are on the lookout for a profession in device programming, we suggest going thru our listing of JDBC Interview Questions.
JDBC drivers are used for setting up a reference to the database. The JDBC API is helping to get admission to tabular information saved in relational databases reminiscent of MySQL, Oracle, MS Get admission to, and so forth. JDBC was once launched as a part of JDK 1.1 in 1997, making it some of the earliest libraries that has been evolved for the Java language.
Best JDBC Interview Questions and Solutions for 2022
JDBC being key to java construction, you will have to be smartly ready with JDBC Interview Questions in case you are fascinated with a profession in writing Java programs. Following are the highest 50 questions that you just must be ready with ahead of you attend your interview:
1. Provide an explanation for what’s JDBC?
This is among the first and maximum often requested questions in lots of the interviews. JDBC stands for Java Database Connectivity. As its title implies, this is a Java API used for interacting with relational databases to get admission to, regulate and procedure information the usage of SQL. It makes use of JDBC drivers for interacting with the database. By way of the usage of JDBC, one can get admission to tabular information from various kinds of relational databases reminiscent of MySQL, Oracle, MS Get admission to, and so forth.
2. What’s ResultSet?
The java.sql.ResultSet interface is used to constitute the database outcome set, which is a results of the execution of SQL queries the usage of observation gadgets. The thing of ResultSet incorporates a cursor that issues to the current row of information within the outcome set. In the beginning, the cursor is displayed ahead of the primary row. Then the cursor strikes to the following row by way of executing the following() way. The following() way iterates in the course of the outcome set with the toughen of some time loop. If there are not any different rows left, the following() way returns a false. Following is an instance for the advent of ResultSet:
ResultSet rs = con.executeQuery(sqlQuery);
3. Are you able to get a null ResultSet?
No, you’ll by no means get null Resultset(). The ResultSet.subsequent() can go back null provided that the following file does no longer include a row.
4. Provide an explanation for the variation between ResultSet Vs. RowSet vs in JDBC?
In a ResultSet() care for this is connected to a database, you can’t make the Outcome a serialized object. Thus, you can’t cross Resultset() around the community. The RowSet() spreads the ResultSet() interface, so it incorporates all strategies from ResultSet().
A RowSet() is all the time serialized. Thus, we will cross a Rowset() from one elegance to every other elegance as it’s not attached to the database.
5. What’s a JDBC driving force?
A JDBC driving force is a device aspect having more than one interfaces and categories that permits a Java utility to be in contact with a database. To determine a hyperlink with person databases, JDBC wishes particular drivers for every database. Those drivers are to be had with the database seller together with the database.
A JDBC driving force establishes a connection to the database. It additionally executes the protocol for sending the question and outcome between the database and the customer.
6. What’s DriverManager in JDBC?
The JDBC DriverManager is a static elegance in Java, the usage of which you perform at the set of JDBC drivers to be had for an utility to make use of. Other JDBC drivers can be utilized concurrently by way of one utility if required. Each and every utility specifies a JDBC driving force by way of the usage of a Uniform Useful resource Locator (URL). On loading the JDBC Driving force elegance into an utility, it registers itself to the DriverManager by way of the usage of DriverManager.registerDriver() or Magnificence.forName(). To substantiate this, you’ll view the supply code of JDBC Driving force categories. On calling the DriverManager.getConnection() way by way of passing main points associated with the database configuration, the DriverManager makes use of the registered drivers to obtain the relationship and go back it to the caller program.
7. Provide an explanation for the other drivers of JDBC.
There are 4 other drivers in JDBC. They’re:
- JDBC-ODBC Bridge: JDBC-ODBC bridge acts as an interface between the DB server and the customer.
- Local API: Part Java Driving force: This driving force virtually behaves as a JDBC-ODBC driving force. Then again, as an alternative of an ODBC driving force, you utilize local APIs and a client-side database.
- Community Protocol: This driving force works like a 3-tier manner for having access to the database. An intermediate server connects to the database. The JDBC way calls ship information to a transitional server after which the server communicates with the database.
- Skinny Driving force: It’s totally written in Java. This driving force explicitly adjustments JDBC way calls to the vendor-specific database protocol.
8. Provide an explanation for which is essentially the most repeatedly used and quickest JDBC driving force.
The JDBC Web natural Java driving force (Kind 4 driving force) is the quickest and maximum repeatedly used driving force for faraway and localhost connections. It interacts immediately with the database by way of converting the JDBC calls into vendor-specific protocol calls.
9. What are the information sorts used for storing photographs and information within the database desk?
- The BLOB information sort retail outlets photographs within the database. You’ll be able to additionally retailer audio and movies the usage of this information sort. This knowledge sort retail outlets a binary form of information.
- The CLOB information sort retail outlets information within the database. This knowledge sort retail outlets the nature form of information.
10. Provide an explanation for what DatabaseMetaData is and why would you utilize it?
The DatabaseMetaData is an interface that gives how you can achieve details about the database. This interface is helping in acquiring database-related data, reminiscent of database title, driving force title, database model, the entire choice of tables or perspectives, and so forth.
11. Provide an explanation for the diversities between JDBC and ODBC?
JDBC (Java Database Connectivity) |
ODBC (Open Database Connectivity) |
|
JDBC is used for the Java language. |
ODBC can be utilized for issues reminiscent of C, C++, Java, and so forth. |
|
JDBC can be utilized on any platform; thus, making it platform-independent. |
ODBC can be utilized just for the Home windows platform; thus, making it platform-dependent. |
|
JDBC drivers are evolved the usage of the Java language. |
Many of the ODBC Drivers were evolved the usage of local languages reminiscent of C, C++ |
|
JDBC is perfect for Java programs as there are not any efficiency problems. |
ODBC isn’t advisable for Java programs as its efficiency isn’t nice because of inside conversion. |
|
JDBC is Object Orientated. |
ODBC is procedural. |
12. What’s Rowset?
A RowSet is not anything however an object that captures a row set from both tabular information resources or JDBC outcome units reminiscent of spreadsheets or information. It helps component-based construction fashions reminiscent of JavaBeans, with the assistance of tournament notifications and a typical set of houses.
13. What’s ResultSet?
The java.sql.ResultSet interface is not anything however the outcome set of a SQL question. It signifies {that a} cursor is pointing to a row of a desk.
14. What are the kinds of ResultSet?
There are 3 kinds of ResultSet:
- TYPE_FORWARD_ONLY: The cursor best strikes ahead.
- TYPE_SCROLL_INSENSITIVE: The cursor can transfer both backward or ahead, however the cursor isn’t delicate to adjustments to the information that underlies the ResultSet.
- TYPE_SCROLL_SENSITIVE: The cursor can transfer back and forth, however it’s delicate to the information that underlies the ResultSet.
Be aware: If we don’t claim any ResultSet then which means that we’re calling the TYPE_FORWARD_ONLY ResultSet.
15. Provide an explanation for some great benefits of a Rowset.
Some great benefits of the usage of RowSet are:
- It’s versatile and more uncomplicated to make use of.
- It’s Updatable and Scrollable by way of default.
16. Provide an explanation for what are the other JDBC statements:
Following are the 3 kinds of JDBC statements:
- Remark: Executes an SQL question (static SQL question) towards the database.
- Ready Remark: Executes an SQL observation time and again. The enter information is dynamic and takes the enter on the run time.
- Callable Remark: Executes saved procedures.
17. Are there any benefits of the usage of a Ready Remark in Java?
A Ready Remark executes sure SQL statements repetitively. This observation is compiled best as soon as even if it’s finished “n” choice of instances.
18. Provide an explanation for the time period connection pooling.
That is every other some of the widespread JDBC interview questions. Connection pooling is the methodology wherein we reuse the resource-like connection gadgets which might be required to ascertain a reference to the database. This is a methodology of managing and making a pool of connections that can be utilized by way of any thread that calls for them. Every time an utility tries to get admission to a backend retailer (reminiscent of a database), it wishes sources to expand, deal with, and unlock a connection to that datastore. Thus, connection pooling can a great deal build up the efficiency of a Java utility whilst lowering the full useful resource utilization.
19. Provide an explanation for the kinds of JDBC Structure?
There are two forms of structure fashions to get admission to the database whilst the usage of JDBC.
They’re:
- Two-tier Structure: On this fashion, Java systems completely connect to the database and there’s no requirement of any mediator like programs servers to connect to the database. This fashion is often referred to as the client-server structure.
- 3-tier Structure: This fashion is the exact opposite of two-tier structure. An utility server is used as a mediator between the Java program or JDBC driving force and the database. The Java program sends a request to the applying server. The server then forwards this request to the database and waits for a reaction from the database.
20. What are the primary parts of JDBC?
Following are the 4 main parts to be had in JDBC:
- JDBC Driving force Supervisor
- JDBC API
- JDBC Take a look at Suite
- JDBC-ODBC Bridge
21. What are the stairs to connect to JDBC?
Following are the 6 fundamental steps to connect to the database in Java.
- Import the JDBC programs.
- Load and check in the JDBC driving force.
- Open and determine a connection to the database.
- Advent a observation object to accomplish a question.
- Execute the observation object and go back a question ResultSet(). Then procedure the ResultSet().
- Shut the ResultSet() and observation gadgets.
- Shut the relationship.
22. Provide an explanation for the that means of sizzling backup and chilly backup.
Whilst this query isn’t associated with JDBC immediately, it will nonetheless be requested in an interview. The chilly again is the backup procedure by which the backup of information is taken ahead of restarting the database. Within the sizzling again, backup procedure the backup of tables and information is taken on the time when the database is operating.
23. What are the stairs to connect to JDBC?
There are 2 kinds of locking to be had in JDBC wherein we will set up quite a lot of person problems. If two customers are looking to get admission to the similar file then there’s no factor however the issue arises when more than one customers are looking to replace the similar file concurrently. To handle this factor, there are two locking modes to be had in JDBC. They’re:
- Positive Locking: Locks the file best when an replace is happening. This sort of locking does no longer use unique locks whilst studying.
- Pessimistic Locking: On this gadget, the data are locked because the row to replace is chosen.
24. Provide an explanation for if the JDBC-ODBC Bridge helps other concurrent open statements in step with connection?
No, when the usage of the JDBC-ODBC Bridge, you’ll open just one Remark object.
25. What are database warnings in JDBC and provide an explanation for how we will set up database warnings in JDBC?
The database caution or SQL caution is not anything however the subclass of the SQLException elegance. You’ll be able to set up or care for this caution by way of the usage of the getWarnings() way on a observation, connection, and ResultSet.
26. Provide an explanation for the variation between executeQuery, executing, and executeUpdate in JDBC?
- executeQuery(): The executeQuery() can be utilized for make a selection question.
- execute(): The execute() can be utilized for any form of SQL Question.
- executeUpdate(): The executeUpdate() can be utilized to switch/replace tables.
27. Provide an explanation for what you imply by way of Metadata and in addition provide an explanation for why we use it.
Metadata approach details about different information. You utilize metadata to get the database product model, driving force title, the entire choice of perspectives and tables.
28. Provide an explanation for why you may use setAutoCommit(false) in JDBC?
The setAutoCommit(false) is used to show off the Auto Devote.
29. Provide an explanation for the position of Magnificence.forName whilst loading drivers in short?
The Magnificence.forName generates an example of JDBC driving force and a check in with DriverManager.
30. Why are ready statements quicker?
The Ready observation execution is way quicker than direct execution because the observation is compiled best as soon as. JDBC drivers and Ready statements are attached with every different throughout execution, and there are not any connection overheads.
31. What are the JDBC API parts?
The interfaces and the categories of JDBC API are discussed underneath:
Other Interfaces
- Connection: The usage of the getConnection()way, a connection object is created.
- Remark: The usage of the createStatement() way of the relationship elegance, the observation object is created.
- PrepareStatement: The prepareStatement() way of connection elegance, the prepareStatement object is created.
- DatabaseMetaData: The Connection interface’s getMetaData() way returns an object of DatabaseMetaData.
- CallableStatement: The saved procedures and purposes are known as the usage of the CallableStatement interface.
Other Categories
- DriverManager: The category’s serve as is to function a gateway between customers and drivers.
- Blob: The binary time period for an enormous object. It stands for a collection of binary information this is stored in combination within the database control gadget as a unmarried entity.
- Clob: Persona Huge Object is known as Clob. Other database control techniques put it to use as an information sort to retailer persona information.
- SQLException: It’s an exception elegance that provides main points on problematic database get admission to.
32. What are the JDBC statements?
Statements are utilized in JDBC to be in contact SQL instructions to databases and obtain information from them. 3 classes of statements exist:
- Statements
- Ready Statements
- Collable Remark
33. Point out the go back form of Magnificence.forName() way?
The thing of java.lang.Magnificence Object is the go back form of the Magnificence.forName() way.
34. Provide an explanation for the diversities between Remark and PreparedStatement interface?
|
Remark |
Ready observation |
|
The Remark interface supplies methods for the database to run queries. The observation interface serves as a manufacturing unit for ResultSet, providing the manufacturing unit serve as to procure a ResultSet object. |
This is a subinterface of a Remark. It’s used for parameterized question execution. |
|
When the usage of Remark, the question is compiled every time when the applying is finished. |
The question is best compiled as soon as on the subject of PreparedStatement. |
|
The Remark is most commonly hired when a static question needs to be finished at runtime. |
We use ready statements when the requirement for enter parameters happens on the runtime question. |
35. How are we able to set a null worth in JDBC PreparedStatement?
We will set a null worth to an index the usage of the setNull()way. Beneath given is the syntax for the given way:
void setNull(int parameterIndex, int sqlType) throws SQLException
36. Provide an explanation for some great benefits of Ready Remark over Remark?
- The Ready Remark outperforms the observation because the observation will have to be compiled every time the code is administered, however the PreparedStatement is compiled as soon as after which finished best at runtime.
- Remark can best run static queries, however Ready Remark can run parameterized queries.
- Each and every time, the question utilized in PreparedStatement seemed to be the similar. Consequently, the database can reuse the former get admission to plan, while observation inlines the parameters into the string, so the question does no longer seem to be the similar each time, fighting cache usage.
37. Provide an explanation for the execution of saved procedures the usage of Callable Remark?
The stairs to jot down and run saved procedures are indexed underneath.
Create the database process: You will have to first construct the saved process within the database ahead of you’ll use it.
Make a community connection.
Make the Callable Remark Object.
Give the values, then run the question.
The values will also be situated within the database, so test there.
38. Describe the position of the JDBC DriverManager elegance.
The DriverManager elegance is used as an interface between the customers and drivers. It maintains observe of the to be had drivers and plays the method of connecting a database to the correct driving force.
39. Point out the purposes of the JDBC Connection interface?
The database consultation is maintained by way of the Connection interface. The control of transactions is conceivable with it. To go back the example of observation, Ready Remark, Callable Remark, and DatabaseMetaData, the manufacturing unit way is equipped by way of the JDBC connection interface.
40. What’s the JDBC ResultSet interface?
A desk row is represented by way of the ResultSet object. It may be used to retrieve information from the database and transfer the cursor pointer. The ResultSet object can best transfer ahead by way of default and can’t be up to date.
41. What’s the JDBC ResultSetMetaData interface?
The desk’s data, together with the entire choice of columns, column names, column sorts, and so forth., is returned by way of the ResultSetMetaData interface.
42. Point out the interface which allows transaction control in JDBC.
The Connection interface gives transaction control strategies like dedicate(), rollback(), and others.
43. Provide an explanation for batch processing and solution to carry out batch processing in JDBC?
We will run many queries through the use of JDBC’s batch processing way. It accelerates the efficiency. Batch processing is supported thru java.sql.Remark and java.sql.Ready Remark interfaces. The movements listed here are important for batch processing in JDBC.
Load the motive force elegance
Create Connection
Create Remark
Upload question within the batch
Execute the Batch
Shut Connection
44. Provide an explanation for CLOB and BLOB information sorts in JDBC?
BLOB: A BLOB is a huge, variable-length binary object this is used to retailer combined media, voice, and different binary information sorts. On a MySQL database, it might probably cling as much as 2GB of information, and on an Oracle database, as much as 128 GB. Many databases, together with MySQL, Oracle, and DB2, allow BLOB to retailer binary information (photographs, video, audio, and combined media).
CLOB: A CLOB is a character-large object of variable size this is used to retailer character-based information, reminiscent of information in quite a lot of databases. On an Oracle Database, it might probably cling 128 GB, whilst a MySQL database can care for as much as 2 GB. Thought to be to be a personality string is a CLOB.
45. What are the various kinds of lockings in JDBC?
The 4 various kinds of locks presented by way of JDBC are mentioned underneath.
- Row and Key Locks: We make the most of this type of lock when updating the rows.
- Web page locks: A web page may have this sort of locks implemented to it.
- Desk locks: The desk is secure by way of desk locks.
- Database locks: When a database is open, a lock is used to prevent different transactions from studying or updating information.
46. How are we able to retailer and retrieve photographs from the database?
We might retailer and retrieve pictures the usage of the Ready Remark interface. A desk with the columns NAME and PHOTO must be created.
47. How are we able to retailer the document within the Oracle database?
CLOB datatype is used within the desk to retailer information within the Oracle database.
48. How are we able to retrieve the document within the Oracle database?
To retrieve the document within the Oracle database we use the getClob() way of the ready observation.
49. Point out the diversities between saved procedures and purposes?
|
Saved procedures |
Purposes |
|
Plays trade common sense |
Plays the calculation |
|
Is not going to have the go back sort |
Can have the go back sort. |
|
Must go back 0 or extra values |
Must go back just one worth |
|
It helps each enter and output parameters |
It helps just one parameter. |
50. Point out techniques to deal with the integrity of a database by way of the usage of JDBC?
We will have to be certain that the ACID houses to deal with the integrity of a database. Atomicity, consistency, isolation, and sturdiness are known as ACID houses.
51. What’s the position of the JDBC setMaxRows way?
The usage of setMaxRows(int i), we will prohibit the choice of rows returned by way of a database the usage of the question. Since MySQL permits us to make use of the prohibit motive, this can be achieved throughout the question.
In case you are keen to achieve the abilities required to paintings in a difficult, rewarding, and dynamic IT position – now we have were given your again! Uncover the never-ending alternatives thru this leading edge Put up Graduate Program in Complete Stack Internet Construction direction designed by way of our companions at Caltech CTME. Sign up lately!
supply: www.simplilearn.com






