Q1. What is JDBC in Java? or Java Database Connectivity ?
Ans. JDBC stands for Java Database Connectivity, which is a standard Java API for database independent connectivity between the Java programming language and a wide range of databases. It is an interface which allows Java code to execute SOL statements inside relational databases. The JDBC library includes APIs for,(a) Making a connection to a database.
(b) Creating SQL or MySQL statements.
(c) Executing that SQL or MySQL queries in the database.
(d) Viewing & Modifying the resulting records.
Q2. What are JDBC drivers? Or List the drivers in JDBC(Java Batabase Connectivity)?
Ans. JDBC drivers implement the defined interfaces in the JDBC API for interacting with your database server. It is a software component that enables Java application to interact with the database.For example, using JDBC drivers enable you to open database connections and to interact with it by sending SQL or database commands then receiving results with Java.
Basically, JDBC drivers are of four types :
Type 1: JDBC-ODBC Bridge Driver.
Type 2: JDBC-Native API Driver.
Type 3: JDBC-Network Protocol Driver.
Type 4: Database Protocol Driver.
Q3. How do we create the JDBC(Java Database Connectivity) application?
Ans. To connect any Java application with the database using JDBC, we need to follow some steps as discussed below :(a) Import the Packages: This step is required to import the statements to your Java code.
(b) Register JDBC Driver : This method is required to dynamically load the driver class using forName () method.
(c) Create Connection : To establish the connection with the database, we required getconnection () method of Driven Manager.
(d) Create Query: To generate a query with the database we required createStatement () method.
(e) Execute Query : The executeQuery() method of statement interface is used to execute queries to the database.
(f) Close Connection: To close the connection with the database, we required close () method.
Q4. Discuss the various types of JDBC drivers in detail ?
Or What are different types of JDBC driver available in Java? Explain their advantages and disadvantages ?
Ans. Types of JDBC Drivers: JDBC is a Java API for executing SQL statements. JDBC drivers are of following types:Type 1: JDBC-ODBC Bridge Driver
This JDBC-ODBC drivers translate calls to JDBC methods into calls to Open Database Connectivity (ODBC) functions. It allow JDBC applications immediate access to database connectivity provided by the existing array of ODBC drivers.This type is recommended only for experimental use or when no other alternative is available. For example: JDK 1.2.
Advantage
1. 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, that's why this is 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.
4. Not good for the web.
Type 2: JDBC-Native API
In a type 2 driver, JDBC API calls are converted into native C/C++ API calls which are unique to the database. These drivers typically provided by the database vendors and used in the same manner as the JDBC-ODBC bridge, the vendor-specific driver must be installed on each client or server machine.If we change the database we have to change the native API as it is specific to a database. A typical use of native-method drivers is on application servers. For example: Oracle Call Interface (OCT) driver.
Advantage
1. This JDBC drivers are typically offer better performance than the JDBC-ODBC bridge as the lavers of communication (tiers) and also it uses native there API which is database specific.
Disadvantages
1. Native API must be installed in the client system and there drivers cannot be used for the Internet.
2. Like type 1 drivers, it's 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 and usually not thread safe.
Type 3: JDBC-Network Protocol Java
In a type 3 driver, a three-tier approach is used to accessing databases. The JDBC clients use standard network sockets to communicate with an middleware application server. The socket information is then translated by the middleware application server into the call format required by the DBMS and forwarded to the database server. This type of drive can connect many Java clients to many different databases.This kind of driver is extremely flexible, since it requires no code installed on the client and a single driver can actually provide access to multiple databases.
Advantages
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.
3. The net protocol can be designed to make the client JDBC driver very small and fast to load.
4. This driver typically support features such as caching load balancing and advanced system administration such as logging and auditing.
5. This driver is very flexible allows access to multiple databases using one driver.
Disadvantage
1. It requires another server application to install and maintain. Traversing the record set may take longer, since the data comes through the back-end server.
Type 4: 100% Pure Java Printed
In a type 4 driver, a pure Java-based driver that communicates directly with vendor's database through socket connection. This is the highest performance driver available for the database and is usually provided by the vendor itself. They convert JDBC calls directly into the network protocol used by a particular database. It allows a direct call from the client system to the database server.This kind of driver is extremely flexible, you don't need to install special software on the client or server. Further, these drivers can be downloaded dynamically.
Advantages
1. The major benefit of that they are completely written in Java to achieve platform independence and eliminate deployment administration issues.
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 don't need to install special software on the client or server. Further, these drivers can be downloaded dynamically.
Disadvantage
1. With this 4 drivers, the user needs a different driver for each database.
Join us on Facebook and Twitter to get the latest study material. You can also ask us any questions.
Facebook = @ studywow
(click on it or search "studywow" on facebook)
Twitter = @ studywow
(click on it or search "studywow" on Twitter)
Email= studywow.com@gmail.com
Send us your query anytime!