InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
What is difference between JDBC, JNDI and Hibernate? |
Answer»
|
|
| 2. |
How to Connect to an Excel Spreadsheet using JDBC in Java? |
|
Answer» Follow the steps below First setup the new ODBC datasource. Goto Administrative Tools−>Data Sources (ODBC)−>System DSN tab−>Add−>Driver do Microsoft Excel(*.xls)−>Finish. Now give the Data Source Name (SampleExcel) & Description. Next, click Select Workbook and point to your excel sheet. In the code make to following code additions Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");Connection conn = DriverManager.getConnection("jdbcodbcSampleExcel","","");stmt = conn.createStatement();sql = "select * from [Sheet1$]";rs=stmt.executeQuery(sql);Where Sheet1 is the excel sheet name. |
|
| 3. |
What is Metadata and why should you use it? |
|
Answer» JDBC API has two Metadata interfaces DatabaseMetaData & ResultSetMetaData. The meta data provides comprehensive information about the database as a whole. The implementation for these interfaces is implemented by database driver vendors to let users know the capabilities of a Database. |
|
| 4. |
Which isolation level prevents dirty read in JDBC, connection class? |
|
Answer» TRANSACTION_READ_COMMITTED prevents dirty reads. |
|
| 5. |
What is a "dirty read"? |
|
Answer» In typical database transactions, say one transaction reads and changes the value while the second transaction reads the value before committing or rolling back by the first transaction. This reading process is called as 'dirty read'. Because there is always a chance that the first transaction might rollback the change which causes the second transaction reads an invalid value. |
|
| 6. |
What are different types of RowSet objects? |
|
Answer» There are two types of RowSet
|
|
| 7. |
What is a RowSet? |
|
Answer» A JDBC RowSet object holds tabular data in a way that makes it more flexible and easier to use than a result set. A RowSet objects are JavaBeans components. |
|
| 8. |
What we set the attribute Concurrency in ResultSet? |
|
Answer» The ResultSet concurrency determines whether the ResultSet can be updated, or only read. A ResultSet can have one of two concurrency levels
|
|
| 9. |
When you say Class.forName() loads the driver class, does it mean it imports the driver class using import statement? |
|
Answer» No, it doesn't. An import statement tells the compiler which class to look for. Class.forName() instructs the Classclass to find a class-loader and load that particular Class object into the memory used by the JVM. |
|
| 10. |
What does the Class.forName("MyClass") do? |
|
Answer» Class.forName("MyClass")
|
|
| 11. |
Suppose the SELECT returns 1000 rows, then how to retrieve the first 100 rows, then go back and retrieve the next 100 rows? |
|
Answer» Use the Statement.setFetchSize method to indicate the size of each database fetch. |
|
| 12. |
Out of String or a java.sql.Clob, which has best performance when used to manipulate data from database? |
|
Answer» java.sql.Clob has better performance as it does not extract any data from the database until you explicitly ask it to. |
|
| 13. |
Out of byte[] or a java.sql.Blob, which has best performance when used to manipulate data from database? |
|
Answer» java.sql.Blob has better performance as it does not extract any data from the database until you explicitly ask it to. |
|
| 14. |
How do you implement connection pooling? |
|
Answer» If you use an application server like WebLogic, WebSphere, jBoss, Tomcat. , then your application server provides the facilities to configure for connection pooling. If you are not using an application server then components like Apache Commons DBCP Component can be used. |
|
| 15. |
Resultset is an interface, how does it support rs.Next()? |
|
Answer» Every vendor of Database provides implementation of ResultSet & other interfaces, through the Driver. |
|
| 16. |
What is the use of blob, clob datatypes in JDBC? |
|
Answer» These are used to store large amount of data into database like images, movie etc which are extremely large in size. |
|
| 17. |
Why do you have to close database connections in Java? |
|
Answer» You need to close the resultset, the statement and the connection. If the connection has come from a pool, closing it actually sends it back to the pool for reuse. We can do this in the finally{} block, such that if an exception is thrown, you still get the chance to close this. |
|
| 18. |
What is the difference between execute, executeQuery, executeUpdate? |
Answer»
|
|
| 19. |
When will you get the message "No Suitable Driver"? |
|
Answer» When a Connection request is issued, the DriverManager asks each loaded driver if it understands the URL sent. When the URL passed is not properly constructed, then the "No Suitable Driver" message is returned. |
|
| 20. |
How will you insert multiple rows into a database in a single transaction? |
|
Answer» Follow steps as below //turn off the implicit commitConnection.setAutoCommit(false);//..your insert/update/delete goes hereConnection.Commit();//a new transaction is implicitly started. |
|
| 21. |
What is a transaction? |
|
Answer» A transaction is a logical unit of work. To complete a logical unit of work, several actions may need to be taken against a database. Transactions are used to provide data integrity, correct application semantics, and a consistent view of data during concurrent access. |
|
| 22. |
What is JDBC SQL escape syntax? |
|
Answer» The escape syntax gives you the flexibility to use database specific features unavailable to you by using standard JDBC methods and properties. The general SQL escape syntax format is as follows {keyword 'parameters'}.JDBC defines escape sequences that contain the standard syntax for the following language features
|
|
| 23. |
What is a Stored Procedure and how do you call it in JDBC? |
|
Answer» A stored procedure is a group of SQL statements that form a logical unit and perform a particular task. For example operations on an employee database (hire, fire, promote, lookup) could be coded as stored procedures executed by application code. Stored procedures can be called using CallableStatement class in JDBC API. For example the following code demonstrates this CallableStatement cs = con.prepareCall("{call MY_SAMPLE_STORED_PROC}");ResultSet rs = cs.executeQuery();
|
|
| 24. |
What are the steps followed to create a batch process? |
|
Answer» Typical sequences of steps to use Batch Processing with Statement or PrepareStatement Object are
|
|
| 25. |
Why would you use a batch process? |
|
Answer» Batch Processing allows you to group related SQL statements into a batch and submit them with one call to the database. |
|
| 26. |
What are SQL warnings? |
|
Answer» SQLWarning objects are a subclass of SQLException that deal with database access warnings. Warnings do not stop the execution of an application, as exceptions do. They simply alert the user that something did not happen as planned. A warning can be reported on a Connection object, a Statement object (including PreparedStatement and CallableStatement objects), or a ResultSet object. Each of these classes has a getWarnings method. |
|
| 27. |
What is SavePoint? Give an example. |
|
Answer» A savepoint marks a point that the current transaction can roll back to. Instead of rolling all of its changes back, it can choose to roll back only some of them. For example, suppose you
After doing this, the table will contain the first 10 rows you inserted. The other 5 rows will have been deleted by the rollback. A savepoint is just a marker that the current transaction can roll back to. |
|
| 28. |
Why will you set auto commit mode to false? |
|
Answer» Following are the reasons
|
|
| 29. |
How do you handle SQL NULL values in Java? |
|
Answer» SQL's use of NULL values and Java's use of null are different concepts. There are three tactics you can use
|
|
| 30. |
What causes "No suitable driver" error? |
|
Answer» "No suitable driver" is occurs during a call to the DriverManager.getConnection method, may be of any of the following reason
|
|
| 31. |
How does JDBC handle the data types of Java and database? |
|
Answer» The JDBC driver converts the Java data type to the appropriate JDBC type before sending it to the database. It uses a default mapping for most data types. For example, a Java int is converted to an SQL INTEGER. |
|
| 32. |
How do you update a result set? |
|
Answer» ResultSet interface contains a collection of update methods for updating the data of a result set. Each update method has two versions for each data type
These methods change the columns of the current row in the ResultSet object, but not in the underlying database. To update your changes to the row in the database, you need to invoke one of the following methods updateRow(), deleteRow(), refreshRow(), cancelRowUpdates(), insertRow() |
|
| 33. |
How can you view a result set? |
|
Answer» ResultSet interface contains get methods for each of the possible data types, and each get method has two versions
For e.g. getInt(String columnName), getInt(int columnIndex) |
|
| 34. |
How cursor works in scrollable result set? |
|
Answer» There are several methods in the ResultSet interface that involve moving the cursor, like beforeFirst(), afterLast(), first(), last(), absolute(int row), relative(int row), previous(), next(), getRow(), moveToInsertRow(), moveToCurrentRow(). |
|
| 35. |
Is there a practical limit for the number of SQL statements that can be added to an instance of a Statement object? |
|
Answer» The specification makes no mention of any size limitation for Statement.addBatch(), this is dependent, on the driver. |
|
| 36. |
How can I determine whether a Statement and its ResultSet will be closed on a commit or rollback? |
|
Answer» Use the DatabaseMetaData methods supportsOpenStatementsAcrossCommit() and supportsOpenStatementsAcrossRollback() to check. |
|
| 37. |
How do you create a connection object? |
|
Answer» There are 3 overloaded DriverManager.getConnection() methods to create a connection object getConnection(String url, String user, String password)Using a database URL with a username and password. For example String URL = "jdbcoraclethinamrood1521EMP";String USER = "username";String PASS = "password"Connection conn = DriverManager.getConnection(URL, USER, PASS);getConnection(String url)Using only a database URL. For example String URL = "jdbcoraclethinusername/passwordamrood1521EMP";Connection conn = DriverManager.getConnection(URL);getConnection(String url, Properties prop)Using a database URL and a Properties object. For example String URL = "jdbcoraclethinamrood1521EMP";Properties info = new Properties( );info.put( "user", "username" );info.put( "password", "password" ); |
|
| 38. |
In real time project which driver did you use? |
|
Answer» Tell about your real time experience. |
|
| 39. |
What do you mean by fastest type of JDBC driver? |
|
Answer» JDBC driver performance or fastness depends on a number of issues Quality of the driver code, size of the driver code, database server and its load, Network topology, Number of times your request is translated to a different API. |
|
| 40. |
What are the benefits of JDBC 4.0? |
|
Answer» Here are few advantages of JDBC 4.0
|
|
| 41. |
How do you register a driver?There are 2 approaches for registering the Driver |
Answer»
|
|
| 42. |
What is difference between statement and prepared statement? |
|
Answer» Prepared statements offer better performance, as they are pre-compiled. Prepared statements reuse the same execution plan for different arguments rather than creating a new execution plan every time. Prepared statements use bind arguments, which are sent to the database engine. This allows mapping different requests with same prepared statement but different arguments to execute the same execution plan. Prepared statements are more secure because they use bind variables, which can prevent SQL injection attack. |
|
| 43. |
What are the different types of JDBC Statements? |
|
Answer» Types of statements are
|
|
| 44. |
What is the design pattern followed by JDBC? |
|
Answer» JDBC architecture decouples an abstraction from its implementation. Hence JDBC follows a bridge design pattern. The JDBC API provides the abstraction and the JDBC drivers provide the implementation. New drivers can be plugged-in to the JDBC API without changing the client code. |
|
| 45. |
What are the standard isolation levels defined by JDBC? |
|
Answer» The standard isolation levels are
|
|
| 46. |
Does the JDBC-ODBC Bridge support multiple concurrent open statements per connection? |
|
Answer» No. You can open only one Statement object per connection when you are using the JDBC-ODBC Bridge. |
|
| 47. |
Which type of JDBC driver is the fastest one? |
|
Answer» JDBC Net pure Java driver(Type 4) is the fastest driver because it converts the JDBC calls into vendor specific protocol calls and it directly interacts with the database. |
|
| 48. |
When should each of the JDBC driver type be used? |
|
Answer» Following is a list as to when the four types of drivers can be used
|
|
| 49. |
What are JDBC driver types? |
|
Answer» There are four types of JDBC drivers
|
|
| 50. |
What are the basic steps to create a JDBC application? |
|
Answer» Following are the basic steps to create a JDBC application
|
|