Part1 - JDBC Q and A

Q : What is the J DBC?
A: Java Database Connectivity (JDBC) is a standard Java API to interact with relational databases form Java.
JDBC has set of classes and interfaces which can use from Java application and talk to database without learning RDBMS details and using Database Specific JDBC Drivers

Q : What are the basic steps of using JDBC in java?
A: The basic steps are:
Load the RDBMS specific JDBC driver because this driver actually communicates with the database. Open the connection to database which is then used to send SQL statements and g et results back. Create JDBC Statement object. T his object contains SQL query. 
Execute statement which returns resultset(s). Resultset contains the tuples of database table as a result of SQL query. Process the result set. Close the connection.

Q : What are the main component of JDBC ?
A: The main components are: 
DriverManager
Driver
Connection
Statement
Resultset

Q : What is DriverManager?
A: DriverManag er is a static class. It manages a list of database drivers. Matches connection requests from thejava application with the proper database driver using communication sub protocol. The first driver that recognizes a certain sub protocol under JDBC will be used to establish a database Connection.

Q : What is Driver?
A: T he JDBC API defines the Java interfaces and classes that programmers use to connect to databases and send queries. A JDBC driver implements these interfaces and classes for a particular DBMS vendor.database communications link, handling all communication with the database. Normally, once the driver is loaded, the developer need not call it explicitly.

Q : What is the connection?
A: Interface with all methods for contacting a database. T he connection object represents communication context, i.e., all communication with database is through connection object only.

Q : What is the statement?
A: Encapsulates an SQL statement which is passed to the database.

Q : What is the resultset?
A: T he Resultset represents set of rows retrieved due to query execution.

Q : How we load a database driver with JDBC?
A: Provided the JAR file containing the driver is properly config ured, just place the JAR file in the classpath. Java developers NO long er need to explicitly load JDBC drivers using code like Class.forName() to register a JDBC driver.
The DriverManag er class takes care of this by automatically locating a suitable driver when the DriverManag er.g etConnection() method is called. T his feature is backward-compatible, so no chang es are needed to the existing JDBC code.

Q : What is the J DBC Driver interfac e?
A: T he JDBC Driver interface provides vendor-specific implementations of the abstract classes provided by the JDBC API. Each vendor driver must provide implementations of the
java.sql.Connection,Statement,PreparedStatement, CallableStatement, ResultSet and Driver

Q : What does the connection objects Represents?

A: T he connection object represents communication context, i.e., all communication with database is through connection object only.

Q : What is the statement?
A: Statement acts like a vehicle through which SQL commands can be sent. Through the connection object we create statement kind of objects.

Q : What is the prepared statement?
A: A prepared statement is an SQL statement that is precompiled by the database. Through precompilation, prepared statements improve the performance of SQL commands that are executed multiple times. Once compiled, prepared statements can be customized prior to each execution by altering predefined SQL parameters.

Q : What is the difference between statement and PreparedStatement?
A: T he difference is: A standard Statement is used to create a Java representation of a literal SQL statement and execute it on the database. A PreparedStatement is a precompiled statement. T his means that when the PreparedStatement is executed, the RDBMS can just run the PreparedStatement SQL statement without having to compile it first. Statement has to verify its metadata ag ainst the database every time. While a prepared statement has to verify its metadata ag ainst the database only once.If you want to execute the SQL statement once g o for ST AT EMENT . If you want to execute a sing le SQL statement multiple number of times, then g o for PREPAREDST AT EMENT . PreparedStatement objects can be reused with passing different values to the queries

Q : What is the callable statement?
A: Callable statements are used from JDBC application to invoke stored procedures and functions.

Q : How to c all a stored proc edure from JDBC ?
A: PL/SQL stored procedures are called from within JDBC prog rams by means of the prepareCall() method of the Connection object created. A call to this method takes variable bind parameters as input parameters as well as output variables and creates an object instance of the CallableStatement class.

Q : What are the types of JDBC Driver?
A: The types are:
Type1: JDBC/ODBC
Type2: Native API (partly-Java driver)
Type3: Open Protocol-Net
Type4: Proprietary Protocol-Net(pure Java driver)

Q : Which type of jdbc driver is the faster one?
A: JDBC Net pure Java driver(T ype IV) is the fastest driver because it converts the JDBC calls into vendor specific protocol calls and it directly interacts with the database.

Q : Does the JDBC-ODBC Bridge support multiple concurrent open statements per
connection?
A: No, You can open only one Statement object per connection when you are using the JDBC-ODBC Bridge.

Q : What are the standard isolation levels defined by the jdbc ?
A: T he standard isolation levels are:
TRANSACT ION_NONE
TRANSACT ION_READ_COMMIT T ED
TRANSACT ION_READ_UNCOMMIT T ED
TRANSACT ION_REPEAT ABLE_READ
TRANSACT ION_SERIALIZ ABLE

Q : What is the resultset?
A: T he ResultSet represents set of rows retrieved due to query execution. Example: ResultSet rs =stmt.executeQuery(sqlQuery);

Q : What are the types of resultset?
A: The types are:

TYPE_FORWARD_ONLY specifies that a resultset is not scrollable, that is, rows within it can be advanced only in the forward direction.

TYPE_SCROLL_INSENSITIVE specifies that a resultset is scrollable in either direction but is insensitive to changes committed by other transactions or other statements in the same transaction.

TYPE_SCROLL_SENSITIVE specifies that a resultset is scrollable in either direction and is affected by changes committed by other transactions or statements within the same transaction.

Q : What is the difference between TYPE_SCROLL_INSENSITIVE and
TYPE_SCOLL_SENSITIVE?
A: An insensitive resultset is like the snapshot of the data in the database when query was executed. A sensitive resultset does NOT represent a snapshot of data; rather it contains points to those rows which satisfy the query condition.
After we get the resultset the changes made to data are not visible through the resultset, and hence they are known as insensitive. After we obtain the resultset if the data is modified then such modifications are visible through resultset.

Q : What is the RowSet?
A: A RowSet is an object that encapsulates a set of rows from either Java Database Connectivity (JDBC) result sets or tabular data sources like a file or spreadsheet. RowSets support component-based development models like JavaBeans, with a standard set of properties and an event notification mechanism.

Q : What are the different types of RowSet?
A:The different types are:
Connected - A connected RowSet object connects to the database once and remains connected until the application terminates.
Disconnected - A disconnected RowSet object connects to the database, executes a query to retrieve the data from the database and then closes the connection. A prog ram may chang e the data in a disconnected
RowSet while it is disconnected. Modified data can be updated in the database after a disconnected RowSet re-establishes the connection with the database.

Q : What is the need of BatchUpdates?
A: T he BatchUpdates feature allows us to group SQL statements tog ether and send to database server in one single trip.

Q : What is the data source?
A: A DataSource object is the representation of a data source in the Java prog ramming language. In basic terms, A DataSource is a facility for storing data.
DataSource can be referenced by JNDI. Data Source may point to RDBMS; file System, any DBMS etc.

Q : What are the advantages of datasource?
A: The advantages are:
An application does not need to hardcode driver information, as it does with the DriverManager. The DataSource implementations can easily change the properties of data sources. The DataSource facility allows developers to implement a DataSource class to take advantage of features like connection pooling and distributed transactions.

Q : What is the main advantage of connection pooling ?
A: A connection pool is a mechanism to reuse connections created. Connection pooling can increase performance dramatically by reusing connections rather than creating a new physical connection each time a connection is requested.

Q : What is the multi programming ?
A: Multi programming is a rapid switching of the CPU back and forth between processes.