Blog Archive

Theme images by Storman. Powered by Blogger.

Tuesday, August 16, 2016

Most Frequently Asked JDBC Interview Questions And Answers.


Most Frequently Asked JDBC Interview Questions And Answers. Latest JDBC Interview Questions And Answers For Freshers And Experienced. JDBC Important Interview Questions.

1. What are the types of statements in JDBC?

JDBC API has 3 Interfaces and their key features are as follows:

Statement: It is used to run simple SQL statements like select and update. Statement interfaces use for general-purpose access to your database. It is useful when you are using static SQL statements at runtime. The Statement interface cannot accept parameters.

PreparedStatement: A SQL statement is pre-compiled and stored in a PreparedStatement object. It is used to run Pre compiled SQL. This object can then be used to efficiently execute this statement multiple times. The object of Prepared Statement class can be created using Connection.prepareStatement() method. This extends Statement interface.

CallableStatement: This interface is used to execute the stored procedures. This extends Prepared Statement interface. The object of Callable Statement class can be created using Connection.prepareCall() method.

2. What causes No suitable driver error?

"No suitable driver" occurs during a call to the DriverManager.getConnection method, may be for any of the following reasons:

Due to failing to load the appropriate JDBC drivers before calling the getConnection method.
It might be specifying an invalid JDBC URL, one that is not recognized by JDBC driver.
This error can occur if one or more the shared libraries needed by the bridge cannot be loaded.

3. What does setAutoCommit do?

setAutoCommit() invoke the commit state query to the database. To perform batch updation we use the setAutoCommit() which enables us to execute more than one statement together, which in result minimizes the database call and sends all statement in one batch.

setAutoCommit() allows us to commit the transaction commit state manually the default values of the setAutoCommit() is true.
Why Prepared Statements are faster?

Prepared execution is faster than direct execution for statements executed more than three or four times because the statement is compiled only once. Prepared statements and JDBC driver are linked with each other. We can bind drivers with columns by triggering the query into the database. When we execute Connection.prepareStatement(), all the columns bindings take place, in order to reduce the time.

4. What restrictions are placed on method overriding?

The restriction on method overloading is the signature of the method.

The signature is the number, type, and order of the arguments passed to a method.
Overridden methods must have the same name, argument list, and return type.
Any method which has the same name cannot have the same signature.
They can have the same return types in the same scope.
The compiler uses the signature to detect which overloaded method to refer when an overloaded method is called.
If two methods have the same name and signature the compiler will throw a runtime error.

5. What are types of JDBC drivers?

There are four types of drivers defined by JDBC as follows:

JDBC/ODBC: These require an ODBC (Open Database Connectivity) driver for the database to be installed. It is used for local connection.

Native API (partly-Java driver): This type of driver uses a database API to interact with the database. It also provides no host redirection.

Network Protocol Driver: It makes use of a middle-tier between the calling program and the database. The client driver communicates with the net server using a database-independent protocol and the net server translates this protocol into database calls.

Native Protocol Drive: This has a same configuration as a type 3 driver but uses a wire protocol specific to a particular vendor and hence can access only that vendor's database.

6. Is it possible to connect to multiple databases simultaneously? Using single statement can one update or extract data from multiple databases?

Yes, it is possible but it depends upon the capabilities of the specific driver implementation, we can connect to multiple databases at the same time. We perform the following steps:

Minimum one driver will be used to handle the commits transaction for multiple connections.
To update and extract data from the different database we use single statement. For this we need special middleware to deal with multiple databases in a single statement or to effectively treat them as one database.

7. What are the differences between setMaxRows(int) and SetFetchSize(int)?

The difference between setFetchSize and setMaxRow are:

setFetchSize(int) defines the number of rows that will be read from the database when the ResultSet needs more rows whereas setMaxRows(int) method of the ResultSet specifies how many rows a ResultSet can contain at a time.

In setFetchSize(int), method in the java.sql.Statement interface will set the 'default' value for all the ResultSet derived from that Statement whereas in setMaxRow(int) default value is 0, i.e. all rows will be included in the ResultSet.

The setMaxRows affects the client side JDBC object while the setFetchSize affects how the database returns the ResultSet data.

8. How can I manage special characters when I execute an INSERT query?

The special characters meaning in SQL can be preceded with a special escape character in strings, e.g. "\". In order to specify the escape character used to quote these characters, include the following syntax on the end of the query:
{escape 'escape-character'}

For example, the query
finds identifier names that begin with an underscore.

9. What is the benefit of having JdbcRowSet implementation? Why do we need a JdbcRowSet like wrapper around ResultSet?

The JdbcRowSet implementation is a wrapper around a ResultSet object has following advantages over ResultSet:

It makes possible to use the ResultSet object as a JavaBeans component.
A JdbcRowSet can be used as a JavaBeans component, thus it can be created and configured at design time and executed at run time.
It can be used to make a ResultSet object scrollable and updatable. All RowSet objects are by default scrollable and updatable.

10. Explain basic steps in writing a Java program using JDBC.

JDBC makes the interaction with RDBMS simple and intuitive. When a Java application needs to access database :

Load the RDBMS specific JDBC driver because this driver actually communicates with the database.
Open the connection to database, for sending SQL statements and get results back.
Create JDBC Statement object containing SQL query.
Execute statement which returns result set. ResultSet contains the tuples of database table as a result of SQL query.
Process the result set.
Close the connection.

11. I have the choice of manipulating database data using a byte[] or a java.sql.Blob. Which has best performance?

We use java.sql.Blob, because of following reason:

It does not extract any data from the database until we trigger a query to the database.
We use byte[] for inserting data in the database when data is not upload in the database till yet.
java.sql.Blob is used when extraction of the data is performed.

12. What are DML and DDL?

Data Manipulation Language (DDL) this portion of the SQL standard is concerned with manipulating the data in a database as opposed to the structure of a database. The DML deals with the SELECT, INSERT, DELETE, UPDATE, COMMIT and ROLLBACK.

Data Definition Language (DDL) this portion of the SQL standard is concerned with the creation, deletion and modification of database objects like tables, indexes and views. The core verbs for DDL are CREATE, ALTER and DROP. While most DBMS engines allow DDL to be used dynamically, it is often not supported in transactions.

13. How can you load the drivers?

It is very simple and involves just one line of code to load the driver or drivers we want to use.

For example, We want to use the JDBC-ODBC Bridge driver, the following code will load it:

Driver documentation will give you the class name to use. For instance, if the class name is jdbc.DriverHELLO, you would load the driver with the following line of code:

14. How do I insert an image file (or other raw data) into a database?

All raw data types should be read and uploaded to the database as an array of bytes, byte[].

Originating from a binary file.
Read all data from the file using a FileInputStream.
Create a byte array from the read data.
Use method setBytes(int index, byte[] data); of java.sql.PreparedStatement to upload the data.

15. Discuss the significances of JDBC.

The significances of JDBC are given below:

JDBC stands for Java Database Connectivity.

Java Database Connectivity (JDBC) is a standard Java API .

Its purpose is to interact with the relational databases in Java.

JDBC is having a set of classes & interfaces which can be used from any Java application.

By using the Database Specific JDBC drivers, it interacts with a database without the applications of RDBMS.

0 on: "Most Frequently Asked JDBC Interview Questions And Answers."