PCMC: 9960935965, KOTHRUD: 9960935600

Java Database Connectivity (JDBC) Interview Questions

Q1.What is the JDBC?
ANS: JDBC stands for Java Database Connectivity. JDBC is a Java API that communicates with the database and executes SQL query.


Q2. What is a JDBC driver and how many JDBC drivers are available?
ANS: JDBC driver contains classes and interfaces that help Java applications and database. There are 4 types of JDBC drivers.
Type 1 driver or JDBC-ODBC bridge driver.
Type 2 driver or Native-API, partly Java driver.
Type 3 driver or Network Protocol, pure Java driver.
Type 4 driver or Native-protocol, pure Java driver.


Q3. Which JDBC driver is the fastest driver?
ANS: Type 4 driver or Native-protocol, pure Java driver, is the fastest driver.


Q4. What are the JDBC API components?
ANS: There are four types of components
JDBC API
JDBC Driver Manager
JDBC Test Suite
JDBC-ODBC Bridge


Q5. What are the JDBC statements?
ANS: There are 3 types of JDBC Statements, as given below:
Statement: It will execute SQL query (static SQL query) against the database.
Prepared Statement: Used when we want to execute SQL statements repeatedly. Input data is dynamic and taken input at the run time.
Callable Statement: Used when we want to execute stored procedures.


Q6. What are the advantages of using PreparedStatement in Java?
ANS: A prepared Statement is used to execute the same SQL statements repeatedly. The prepared statement is compiled only once even though it used “n” a number of times.


Q7. What is ResultSet?
ANS: The java.sql.ResultSet interface means the result set of a SQL query. It means a cursor is pointing to a row of a table; it points to before the first row.


Q8. What are the types of ResultSet?
ANS: There are three types of ResultSet available. If we do not declare any ResultSet that means we are calling TYPE_FORWARD_ONLY
ATYPE_FORWARD_ONLY: The cursor can move only forward.
TYPE_SCROLL_INSENSITIVE: The cursor can move forward and backward but is not sensitive.
TYPE_SCROLL_SENSITIVE: The cursor can move forward and backward, but it is sensitive


Q9. Explain the difference between RowSet vs. ResultSet in JDBC.
ANS: In a ResultSet handle connection to a DB, we cannot make Result as a serialized object. Because of the above issue, we cannot pass Resultset across the network.RowSet extends the ResultSet interface, so it holds all methods from ResultSet. RowSet is serialized. So, we can pass Rowset from one class to another class because it has no connection with the database.


Q10. Why would you use setAutoCommit(false) in JDBC?
ANS: If you want to turn Off the Auto Commit then set connection.setAutoCommit(false)


Q11. What are database warnings in JDBC and how can we handle database warnings in JDBC?
ANS: SQL warning or Database warning is the subclass of SQLException class. We can handle it by using getWarnings() method on Connection, Statement, and ResultSet


Q12. Can I get a null ResultSet?
ANS: No, we cannot get a null Resultset. ResultSet.next() can return null if the next record does not contain a row.


Q13. What do you mean by Metadata and why we are using it?
ANS: Metadata means data or information about other data. We use metadata to get database product version, driver name, the total number of tables, and views.


Q14. What is the difference between executing, executeQuery, and executeUpdate in JDBC?
ANS: execute(): it can be used for any kind of SQL Query.
executeQuery(): it can be used for select query.
executeUpdate(): it can be used to change/update the table.


Q15. What is database connection pooling? Advantages of using a connection pool?
ANS: Connection pooling means connections will be stored in the cache and we can reuse them in the future.
Advantage: It is faster
Connection pooling makes it easier to diagnose and analyze database connections.


Q16. What is the function of the DriverManager class?
ANS: It is an interface between user and drivers. DriverManager tracks all the activity between a database and the appropriate driver. 


Q17. What is the meaning of batch updates?
ANS: Batch updates mean executing a set/group of SQL queries all at once. Batch updates can be used only for insert, update, and delete but not for select query. 


Q18. How many packages are available in JDBC API?
ANS: Two types of packages are available in JDBC API
java.sql, and javax.sql.

Browse our course links: Java Training in Pune

To Join our FREE DEMO Session: Click Here


Q19. What is the return type of execute, executeQuery and executeUpdate?
ANS: Return type of execute is Boolean
Return type of executeQuery is ResultSet object
Return type of executeUpdate is int.


Q20. Result Set’s index Starts with 0 or 1?
ANS: Result Set’s index starts with 1.


Q21. What is the role of Class.forName while loading drivers?
ANS: Class.forName creates an instance of JDBC driver and registers with DriverManager.


Q22. JDBC-ODBC Bridge is multi-threaded or not?
ANS: No, JDBC-ODBC Bridge uses synchronized methods to serialize all the calls made to ODBC.


Q23. Which interface handles transaction management in JDBC?
ANS: Connection interface handles transaction management in JDBC. It provides a method for commit (), rollback (), etc.


Q24. Why “No suitable driver” error occur?
ANS: No suitable driver” occurs when we are calling the DriverManager.getConnection method, it may occur because of the following reasons:
unable to load exact JDBC drivers before calling the getConnection method.
It may be an invalid or wrong JDBC URL.


Q25. Prepared Statements are faster. Why?
ANS: Prepared statement execution is faster than direct execution because the statement is compiled only once. Prepared statements & JDBC driver are connected with each other during execution, and there are no connection overheads. 


Q26. Is it possible to connect to multiple databases? Using a single statement can we update or extract data from two or three or many databases?
ANS: Yes, it is possible to connect to multiple databases, at the same time, but it depends on the specific driver. To update and extract data from the different database we can use the single statement. But we need middleware to deal with multiple databases or a single database. 


Q27. What are the exceptions in JDBC?
ANS: There are four types of exceptions in JDBC.
batchUpdate Exception
Data Truncation
SQL Exception
SQL Warning


Q28. What are the new features available in JDBC 4.0?
ANS: The new features are
Auto loading by JDBC driver class
Enhanced Connection Management
RowId SQL enabled
Dataset implemented by SQL by using Annotations
Enhancements of SQL exception handling
Supporting SQL XML files


Q29. What are the packages are used in JDBC?
ANS: 8 packages are used in JDBC –sql.Driver
Connection
Statement
PreparedStatement
CallableStatement
ResultSet
ResultSetMetaData
DatabaseMetaData


Q30. How many RowSet are available in JDBC?
ANS: There are two types of row sets are available:
Connected – A connected RowSet object connects to the database instantaneously. If the application terminates then the connected RowSet object also terminates.
Disconnected – A disconnected RowSet object connects to the database after execution of the required query. 


Q31. What is the meaning of connection?
ANS: The connection interface consists of methods for interaction with the database.


Q32. What is the reason why we need a JdbcRowSet like the wrapper around ResultSet?
ANS: We can use the ResultSet object as a JavaBeans component.
A JdbcRowSet also can be used as a JavaBeans component. That’s why it can be created and configured at design or compile time and executed at run time.
All jdbcRowSet objects are scrollable and updatable.


Q33. How many ways that we can view a result set?
ANS: There are 2 ways to view ResultSet
Column, column index.
Example: getInt(String columnName), getInt(int columnIndex)


Q34. How many ways can you update a result set?
ANS: Following methods helps you to update the result set
updateRow()
deleteRow()
refreshRow()
cancelRowUpdates()
insertRow()


Q35. Why should we close database connections in Java?
ANS: As a best practice, we must close the resultset, the statement, and the connection. If the connection is coming from a pool, on closure, the connection is sent back to the pool for reuse. We are doing this in the finally{} block, because if any exception occurs, then we still get the chance to close this.

Browse our course links: Java Training in Pune

To Join our FREE DEMO Session: Click Here

Get More Information