Your Ad Here

Saturday, January 3, 2009

JDBC Interview Questions & Answers

Q : How can you make the connection?
A : To establish a connection you need to have the appropriate driver connect to the DBMS.
The following line of code illustrates the general idea is :

String url = “jdbc:odbc:Fred”;
Connection con = DriverManager.getConnection(url, “Fernanda”, “J8?);

Q : What are the different types of Statements?
A : There are three types of statements
• Regular statement (use to create Statement method),
• prepared statement (use prepare Statement method)
• callable statement (use to prepareCall)

Q : What are the two major components of JDBC?
A : The two major components of JDBC are Connection Pooling and Data Sources.

Q : What is a metadata?
A : Metadata is data about data.
In context of JDBC, there are two types of metadata.
1. Describing information about the Result set object. i.e, if u have applied a query that fetches some result. This metadata describes information about how many columns have been fetched, their names etc.
2. Second One describes about the database connection

Q : What is the difference between local and global transaction?
A :A transaction is atomic unit of Work. The tasks which are made into the transaction act as a unit which can be executed successfully all, or if at least one task fails to its promise ,then the effect of all the tasks are to be rollbacked. Thus transaction is committed or rolled backed.

Transactions can be divided into two categories.

1.Local Transactions: These transactions are confined to the objects which reside inside one particular JVM.Local transactions in java can be implemented using the JTA api.

2.Global Transactions:These transactions may encapsulate objects which are distributed on various JVM's.Global transactions are implemented through the TWO-PHASE-COMMIT design implementation.

Q : How to call a Stored Procedure from JDBC?

A : The first step is to create a CallableStatement object. As with as a Statement an and PreparedStatement objects, this is done with an open Connection object. A CallableStatement object contains a call to a stored procedure.

E.g.

CallableStatement cs = con.prepareCall("{call SHOW_SUPPLIERS}");
ResultSet rs = cs.executeQuery();

Q : What is JDBC objects generate SQLWarnings?
A : Connections, Statements and ResultSets all have a getWarnings method that allows retrieval. Keep in mind that the prior ResultSet warnings are cleared on each new read and prior Statement warnings are cleared with each new execution. getWarnings() itself does not clear existing warnings, but each object has a clearWarnings method.

Q : What are the two major components of JDBC?
A :The two major components of JDBC is One implementation interface for the database manufacturers, the other implementation interface for application and applet writers.

Q : Which is the following statements is true regarding the two tier model of the JDBC driver model?

A : A JDBC driver is required to the communicate with the particular database management system that is being accessed

Q : Which is the statement below does not correctly defines the difference between ODBC and JDBC?
A : ODBC requires manual installation of the ODBC driver manager and driver on all client machines. While for JDBC driver is written in Java and JDBC code is automatically installable, secure and portable on all platforms.
Explanation: Because ODBC being a C based interface cannot be directly used with java.

Q : How web server is connected with appliction server?
A : All the application server needs to the information about the installed web server's port and name, through these entries it creates a connector and stored. This connector is used to communicate with Application server.

Q : What’s the difference between Rowset and Resultset?
A : RowSets are a part of JDBC 2.0 API, and there are quite a few implementations to suit your needs (CachedRowSet, JDBCRowSet, WebRowSet). Essentially a row set is a JavaBean that contains database data. The implementation is store the data offline, or it can simply wrap a connection to make a result set look like a JavaBean. You could even use a row set to access data communicating over HTTP to a servlet, which provides the data for the bean.

Q : How can I insert images into a Mysql database?
A : This code is snippet shows the basics:

File file = new File(fPICTURE);
FileInputStream fis = new FileInputStream(file);
PreparedStatement ps =
ConrsIn.prepareStatement("insert into dbPICTURE values (?,?)");

ps.setString(1,file.getName());
ps.setBinaryStream(2,fis,(int)file.length());
ps.close();
fis.close();

Q : What is the advantage of using a PreparedStatement?
A : For SQL statements that are executed repeatedly, using a PreparedStatement object would almost always be s faster than using a Statement object. This is because creating a PreparedStatement object by explicitly giving the SQL statement causes the statement to be precompiled within the database immediately. Thus, when the PreparedStatement is later executed, the DBMS does not have to recompile the SQL statement and prepared an execution plan - it simply runs the statement.
Typically, PreparedStatement objects are used for the SQL statements that take parameters. However, they can also be used with repeatedly executed SQL statements that do not accept parameters.

Q : How does the JDBC work?
A : The Java Database Connectivity (JDBC) is used to whenever a Java application should communicate with a relational database for which a JDBC driver exists. JDBC is part of the Java platform standard; all visible classes used in the Java/database communication are placed in package java. SQL

Main JDBC classes:
• DriverManager. Manages a list of database drivers. Matches connection requests from the java application with the proper database driver using communication subprotocol. The first driver that recognizes a certain subprotocol under jdbc (such as odbc or dbAnywhere/dbaw) will be used to establish a database Connection.
• Driver. The database communications link, handling all communication with the database. Normally, once the driver is loaded, the developer need not call it explicitly.
• Connection. Interface with all the methods for contacting a database
• Statement. Encapsulates an SQL statement which is passed to the database to be parsed, compiled, planned and executed.
• ResultSet. The answer/result from a statement. A ResultSet is a fancy 2D list which encapsulates all outgoing results from a given SQL query.

0 comments:

Your Ad Here