You are on page 1of 17

JDBC AND JAVA Que :- What is JDBC ?

Ans :- Java database connectivity (JDBC) is the JavaSoft specification of a standard application programming interface (API) that allows Java programs to access database management systems. The JDBC API consists of a set of interfaces and classes written in the Java programming language. Using these standard interfaces and classes, programmers can write applications that connect to databases, send queries written in structured query language (SQL), and process the results. The JDBC API is consistent with the style of the core Java interfaces and classes, In short JDBC helps the programmers to write java applications that manage the three programming activities: 1. It helps us to connect to a data source, like a database. 2. It helps us in sending queries and updating statements to the database and 3. Retrieving and processing the results received from the database in terms of answering to your query. Que :- Explain the difference between JDBC and ODBC Ans :- 1 ODBC is for Microsoft and JDBC is for java applications.
Edited and Compiled by Sanjay Wankhade VESP Chembur
1

2 . ODBC can't be directly used with Java because it uses a C interface. 3. ODBC makes use of pointers which have been removed totally from java. 4. ODBC mixes simple and advanced features together and has complex options for simple queries,But JDBC is designed to keep things simple while allowing advanced capabilities when required. 5 .ODBC requires manual installation of the ODBC driver manager and driver on all client machines.JDBC drivers are written in java and JDBC code is automatically installable,secure,and portable on all platforms. 6 . JDBC API is a natural Java Interface and is built on ODBC. JDBC retains some of the basic feature of ODBC

Que:- Explain JDBC architecture in detail. Ans :- The JDBC API supports both two-tier and three-tier processing models for database access. Figure 1: Two-tier Architecture for Data Access.
Edited and Compiled by Sanjay Wankhade VESP Chembur
2

In the two-tier model, a Java application talks directly to the data source. This requires a JDBC driver that can communicate with the particular data source being accessed. A user's commands are delivered to the database or other data source, and the results of those statements are sent back to the user. The data source may be located on another machine to which the user is connected via a network. This is referred to as a client/server configuration, with the user's machine as the client, and the machine housing the data source as the server. The network can be an intranet, which, for example, connects employees within a corporation, or it can be the Internet. In the three-tier model, commands are sent to a "middle tier" of services, which then sends the commands to the data source. The data source processes the commands and sends the results back to the middle tier, which then sends them to the user. MIS directors find the three-tier model very attractive because the middle tier makes it possible to maintain control over access and the kinds of updates that can be made to corporate data. Another advantage is that it simplifies the deployment of applications. Finally, in many cases, the three-tier architecture can provide performance advantages.

Edited and Compiled by Sanjay Wankhade VESP Chembur

Figure 2: Three-tier Architecture for Data Access.

Until recently, the middle tier has often been written in languages such as C or C++, which offer fast performance. However, with the introduction of optimizing compilers that translate Java bytecode into efficient machine-specific code and technologies such as Enterprise JavaBeans, the Java platform is fast becoming the standard platform for middle-tier development. This is a big plus, making it possible to take advantage of Java's robustness, multithreading, and security features. With enterprises increasingly using the Java programming language for writing server code, the JDBC API is being used more and more in the middle tier of a three-tier architecture. Some of the features that make JDBC a server technology are its support for connection pooling, distributed transactions, and disconnected rowsets. The JDBC API is also what allows access to a data source from a Java middle tier. Que :- What are various types of JDBC divers ? Explain in short.
Edited and Compiled by Sanjay Wankhade VESP Chembur
4

Ans :- JDBC drivers are divided into four types or levels. The different types of jdbc drivers are: Type 1: JDBC-ODBC Bridge driver (Bridge) Type 2: Native-API/partly Java driver (Native) Type 3: AllJava/Net-protocol driver (Middleware) Type 4: All Java/Native-protocol driver (Pure)

JDBC Driver Types


JDBC drivers are divided into four types or levels. The different types of jdbc drivers are: Type 1: JDBC-ODBC Bridge driver (Bridge) Type 2: Native-API/partly Java driver (Native) Type 3: AllJava/Net-protocol driver (Middleware) Type 4: All Java/Native-protocol driver (Pure) 4 types of jdbc drivers are elaborated in detail as shown below: Type 1 JDBC Driver JDBC-ODBC Bridge driver The Type 1 driver translates all JDBC calls into ODBC calls and sends them to the ODBC driver. ODBC is a generic API. The JDBC-ODBC Bridge driver is recommended only for experimental use or when no other alternative is available.

Edited and Compiled by Sanjay Wankhade VESP Chembur

Type 1: JDBC-ODBC Bridge Advantage The JDBC-ODBC Bridge allows access to almost any database, since the database's ODBC drivers are already available. Disadvantages 1. Since the Bridge driver is not written fully in Java, Type 1 drivers are not portable. 2. A performance issue is seen as a JDBC call goes through the bridge to the ODBC driver, then to the database, and this applies even in the reverse process. They are the slowest of all driver types. 3. The client system requires the ODBC Installation to use the driver.
Edited and Compiled by Sanjay Wankhade VESP Chembur
6

4. Not good for the Web. Type 2 JDBC Driver Native-API/partly Java driver The distinctive characteristic of type 2 jdbc drivers are that Type 2 drivers convert JDBC calls into database-specific calls i.e. this driver is specific to a particular database. Some distinctive characteristic of type 2 jdbc drivers are shown below. Example: Oracle will have oracle native api.

Type 2: Native api/ Partly Java Driver Advantage The distinctive characteristic of type 2 jdbc drivers are that they are typically offer better performance than the JDBC-ODBC Bridge as the layers of communication (tiers) are less than that of Type
Edited and Compiled by Sanjay Wankhade VESP Chembur
7

1 and also it uses Native api which is Database specific. Disadvantage 1. Native API must be installed in the Client System and hence type 2 drivers cannot be used for the Internet. 2. Like Type 1 drivers, its not written in Java Language which forms a portability issue. 3. If we change the Database we have to change the native api as it is specific to a database 4. Mostly obsolete now 5. Usually not thread safe. Type 3 JDBC Driver All Java/Net-protocol driver Type 3 database requests are passed through the network to the middle-tier server. The middle-tier then translates the request to the database. If the middle-tier server can in turn use Type1, Type 2 or Type 4 drivers.

Edited and Compiled by Sanjay Wankhade VESP Chembur

Type 3: All Java/ Net-Protocol Driver Advantage 1. This driver is server-based, so there is no need for any vendor database library to be present on client machines. 2. This driver is fully written in Java and hence Portable. It is suitable for the web. 3. There are many opportunities to optimize portability, performance, and scalability. 4. The net protocol can be designed to make the client JDBC driver very small and fast to load. 5. The type 3 driver typically provides support for features such as caching (connections, query results, and so on), load balancing, and advanced system administration such as logging and auditing. 6. This driver is very flexible allows access to multiple databases using one driver. 7. They are the most efficient amongst all driver types. Disadvantage It requires another server application to install and maintain. Traversing the recordset may take longer, since the data comes through the backend server. Type 4 JDBC Driver Native-protocol/all-Java driver The Type 4 uses java networking libraries to communicate directly
Edited and Compiled by Sanjay Wankhade VESP Chembur
9

with the database server.

Type 4: Native-protocol/all-Java driver Advantage 1. The major benefit of using a type 4 jdbc drivers are that they are completely written in Java to achieve platform independence and eliminate deployment administration issues. It is most suitable for the web. 2. Number of translation layers is very less i.e. type 4 JDBC drivers don't have to translate database requests to ODBC or a native connectivity interface or to pass the request on to another server, performance is typically quite good. 3. You dont need to install special software on the client or server. Further, these drivers can be downloaded dynamically. Disadvantage With type 4 drivers, the user needs a different driver for each database.

Edited and Compiled by Sanjay Wankhade VESP Chembur

10

Que :-Explain Driver manager class in detail. Ans :The JDBC Driver Manager is a very important class that defines objects which connect Java applications to a JDBC driver. Usually Driver Manager is the backbone of the JDBC architecture. It's very simple and small that is used to provide a means of managing the different types of JDBC database driver running on an application. The main responsibility of JDBC database driver is to load all the drivers found in the system properly as well as to select the most appropriate driver from opening a connection to a database. The Driver Manager also helps to select the most appropriate driver from the previously loaded drivers when a new open database is connected. The DriverManager class works between the user and the drivers. The task of the DriverManager class is to keep track of the drivers that are available and handles establishing a connection between a database and the appropriate driver. It even keeps track of the driver login time limits and printing of log and tracing messages. This class is mainly useful for the simple application, the most frequently used method of this class is DriverManager.getConnetion(). We can know by the name of the method that this method establishes a connection to a database. The DriverManager class maintains the list of the Driver classes. Each driver has to be get registered in the

Edited and Compiled by Sanjay Wankhade VESP Chembur

11

DriverManager class by calling the method DriverManager.registerDriver(). By calling the Class.forName() method the driver class get automatically loaded. The driver is loaded by calling the Class.forName() method. JDBC drivers are designed to tell the DriverManager about themselves automatically when their driver implementation class get loads. This class has many methods. Some of the commonly used methods are given below: 1. deregisterDriver(Driver driver) : It drops the driver from the list of drivers registered in the DriverManager class. 2. registerDriver(Driver driver) : It registers the driver with the DriverManager class. 3. getConnection(String url) : It tries to establish the connection to a given database URL. 4. getConnection(String url, Sting user, String password) : It tries to establish the connection to a given database URL. 5. getConnection(String url, Properties info) : It tries to establish the connection to a given database URL. 6. getDriver(String url) : It attempts to locate the driver by the given string. 7. getDrivers() : It retrieves the enumeration of the drivers which has been registered with the DriverManager class.

Que : What is ResultSet Explain in role of ResultSet in JDBC in detail.


Edited and Compiled by Sanjay Wankhade VESP Chembur
12

Ans : A ResultSet is a Java object that contains the results of executing an SQL query. In other words, it contains the rows that satisfy the conditions of the query. The data stored in a ResultSet object is retrieved through a set of get methods that allows access to the various columns of the current row. The ResultSet.next method is used to move to the next row of the ResultSet, making it the current row. The general form of a result set is a table with column headings and the corresponding values returned by a query. For example, if your query is SELECT a, b, c FROM Table1, your result set will have the following form: a b c ---------- ------------ ----------12345 Cupertino 2459723.495 83472 Redmond 1.0 83492 Boston 35069473.43 The following code fragment is an example of executing an SQL statement that will return a collection of rows, with column a as an int, column b as a String, and column c as a float: java.sql.Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1"); while (rs.next()) { // retrieve and print the values for the current row int i = rs.getInt("a"); String s = rs.getString("b");
Edited and Compiled by Sanjay Wankhade VESP Chembur
13

float f = rs.getFloat("c"); System.out.println("ROW = " + i + " " + s + " " + f); } Rows and Columns A relational database is made up of tables, with each table consisting of rows and columns. A result set is also a table with rows and columns, but it contains only the column values from a database table that satisfy the conditions of a query. Cursors A ResultSet object maintains a cursor, which points to its current row of data. The cursor moves down one row each time the method next is called. When a ResultSet object is first created, the cursor is positioned before the first row, so the first call to the next method puts the cursor on the first row, making it the current row. ResultSet rows can be retrieved in sequence from top to bottom as the cursor moves down one row with each successive call to the method next. Que :-What are various types of ResultSet ? Explain in short . Ans :- Results sets may have different levels of functionality. For example, they may be scrollable or nonscrollable. A scrollable result set has a cursor that moves both forward and backward and can be moved to a particular row. Also, result sets may be sensitive or
Edited and Compiled by Sanjay Wankhade VESP Chembur
14

insensitive to changes made while they are open; that is, they may or may not reflect changes to column values that are modified in the database. Based on the capabilities of scrollability and sensitivity to changes, there are three types of result sets available with the JDBC The following constants, defined in the ResultSet interface, are used to specify these three types of result sets: 1. TYPE_FORWARD_ONLY o The result set is nonscrollable; its cursor moves forward only, from top to bottom. o The view of the data in the result set depends on whether the DBMS materializes results incrementally. 2. TYPE_SCROLL_INSENSITIVE o The result set is scrollable: Its cursor can move forward or backward and can be moved to a particular row or to a row whose position is relative to its current position. o The result set generally does not show changes to the underlying database that are made while it is open. The membership, order, and column values of rows are typically fixed when the result set is created. 3. TYPE_SCROLL_SENSITIVE o The result set is scrollable; its cursor can move forward or backward and can be moved to a particular row or to a row whose position is relative to its current position.
Edited and Compiled by Sanjay Wankhade VESP Chembur
15

The result set is sensitive to changes made while it is open. If the underlying column values are modified, the new values are visible, thus providing a dynamic view of the underlying data. The membership and ordering of rows in the result set may be fixed or not, depending on the implementation.

To allow a higher level of concurrency, an updatable result set may be implemented so that it uses an optimistic concurrency control scheme. Que :- What is the use of Callable statement(Interface)? Explain. Ans :- The interface used to execute SQL stored procedures. JDBC provides a stored procedure SQL escape syntax that allows stored procedures to be called in a standard way for all RDBMSs. A CallableStatement can return one ResultSet or multiple ResultSet objects. Multiple ResultSet objects are handled using operations inherited from Statement. A CallableStatement object provides a way to call stored procedures in a standard way for all DBMSs. A stored procedure is stored in a database; the call to the stored procedure is what a CallableStatement object contains. This call is written in an escape syntax that may take one of two forms: one form with a result parameter, and the other without one A result parameter, a kind of OUT parameter, is the return value for the stored procedure. Both forms
Edited and Compiled by Sanjay Wankhade VESP Chembur
16

may have a variable number of parameters used for input (IN parameters), output (OUT parameters), or both (INOUT parameters The syntax for invoking a stored procedure in JDBC is shown below. {call procedure_name[(?, ?, ...)]} The syntax for a procedure that returns a result parameter is: {? = call procedure_name[(?, ?, ...)]} The syntax for a stored procedure with no parameters would look like this: {call procedure_name}

Creating a CallableStatement Object CallableStatement objects are created with the Connection method prepareCall. The example below creates an instance of CallableStatement that contains a call to the stored procedure getTestData, which has two arguments and no result parameter: CallableStatement cstmt = con.prepareCall( "{call getTestData(?, ?)}"); Whether the ? placeholders are IN, OUT, or INOUT parameters depends on the stored procedure getTestData.

Edited and Compiled by Sanjay Wankhade VESP Chembur

17

You might also like