Sunday, September 9, 2012

Differentiate between a Statement and a PreparedStatement.

• A standard Statement is used for creating a Java representation for a literal SQL statement and for executing it on the database. 

• A PreparedStatement is a precompiled Statement. 

• A Statement has to verify its metadata in the database every time. 

• But ,the prepared statement has to verify its metadata in the database only once.

• If we execute the SQL statement, it will go to the STATEMENT. 

• But, if we want to execute a single SQL statement for the multiple number of times, it’ll go to the PreparedStatement.

Describe how the JDBC application works.

A JDBC application may be divided into two layers:

• Driver layer

• Application layer

• The Driver layer consists of DriverManager class & the JDBC drivers.

• The Application layer begins after putting a request to the DriverManager for the connection.

• An appropriate driver is chosen and used for establishing the connection. 

• This connection is linked to the application layer.

• The application needs the connection for creating the Statement kind of objects by which the results are obtained.

Explain the life cycle of JDBC.

The life cycle for a servlet comprises of the following phases:

• DriverManager : for managing a list of database drivers. 

• Driver : for communicating with the database. 

• Connection : for interfacing with all the methods for connecting a database.

• Statement : for encapsulating an SQL statement for passing to the database which had been parsed, compiled, planned and executed.

• ResultSet: for representing a set of rows retrieved for the query execution.

Briefly tell about the JDBC Architecture

The JDBC Architecture consists of two layers:

2.The JDBC Driver API

• The JDBC API provides the application-JDBC Manager connection.

• The JDBC Driver API supports the JDBC Manager-to-Driver Connection.

• The JDBC API interacts with a driver manager, database-specific driver for providing transparent connectivity for the heterogeneous databases. 

• The JDBC driver manager authenticates that the correct driver has been used to access each data source.

• The driver manager supports multiple concurrent drivers connected to the multiple heterogeneous databases.

How do Java applications access the database using JDBC?

Java applications access the database using JDBC by : 

• Communicating with the database for Loading the RDBMS specific JDBC driver 

• Opening the connection with database 

• Sending the SQL statements and get the results back.

• Creating JDBC Statement object which contains SQL query.

• Executing statement to return the resultset(s) containing the tuples of database table which is a result of SQL query.

• Processing the result set.

• Closing the connection.

How to Calculate the Future Investment Value of Investment.

Below java program for calculate future investment value. Here calculate s using the formula futureInvestmentValue =investmentAmount x (...