0% found this document useful (0 votes)
72 views18 pages

JDBC (Java Data Base Connectivity)

JDBC (Java Database Connectivity) is a Java API that allows Java programs to connect to databases. It defines interfaces and classes for executing SQL statements and retrieving results. The JDBC API supports basic SQL functionality and allows embedding SQL statements directly in Java code. There are four types of JDBC drivers with varying levels of Java and native code implementation. Type 1 drivers are JDBC-ODBC bridges that translate JDBC calls to ODBC, while Type 4 drivers are fully implemented in Java.

Uploaded by

Varun Dua
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views18 pages

JDBC (Java Data Base Connectivity)

JDBC (Java Database Connectivity) is a Java API that allows Java programs to connect to databases. It defines interfaces and classes for executing SQL statements and retrieving results. The JDBC API supports basic SQL functionality and allows embedding SQL statements directly in Java code. There are four types of JDBC drivers with varying levels of Java and native code implementation. Type 1 drivers are JDBC-ODBC bridges that translate JDBC calls to ODBC, while Type 4 drivers are fully implemented in Java.

Uploaded by

Varun Dua
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

JDBC (Java Data Base Connectivity)

The JDBC ( Java Database Connectivity) API defines interfaces and classes for writing database applications in Java by making database connections. Using JDBC you can send SQL, PL/SQL statements to almost any relational database. JDBC is a Java API for executing SQL statements and supports basic SQL functionality. It provides RDBMS access by allowing you to embed SQL inside Java code.

JDBC Architecture

Java application calls the JDBC library. JDBC loads a driver which talks to the database. We can change database engines without changing database code.

History and implementation


Sun Microsystems released JDBC as part of JDK1.1 on February 19, 1997. It has since formed part of the Java Standard Edition. The JDBC classes are contained in the Java package java.sql and javax.sql.

Functionality
JDBC allows multiple implementations to exist and be used by the same application. The API provides a mechanism for dynamically loading the correct Java packages and registering them with the JDBC Driver Manager. JDBC connections support creating and executing statements. These may be update statements such as SQL's CREATE, INSERT, UPDATE and DELETE, or they may be query statements such as SELECT

JDBC represents statements using one of the following classes:


Statement the statement is sent to the database server each and every time. PreparedStatement the statement is cached and then the execution path is pre determined on the database server allowing it to be executed multiple times in an efficient manner. CallableStatement used for executing stored procedures on the database.

Statement
The object used for executing a static SQL statement and returning the results it produces. By default, only one ResultSet object per Statement object can be open at the same time. All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists.

java.sql Interface Statement All Superinterfaces: Wrapper All Sub interfaces: CallableStatement, PreparedStatement
public interface Statementextends Wrapper

Statements Method Detail


executeQuery
ResultSet executeQuery(String sql) throws SQLException Executes the given SQL statement, which returns a single ResultSet object. Parameters:sql - an SQL statement to be sent to the database, typically a static SQL SELECT statement Returns:a ResultSet object that contains the data produced by the given query; never null Throws:SQLException- if a database access error occurs, this method is called on a closed Statement or the given SQL statement produces anything other than a single ResultSet object.

executeUpdate
int executeUpdate(String sql) throws SQLException Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement. Parameters:sql - an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement. Returns:either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing Throws:SQLException - if a database access error occurs, this method is called on a closed Statement or the given SQL statement produces a ResultSet object

PreparedStatement
An object that represents a precompiled SQL statement. A SQL statement is precompiled and stored in a PreparedStatement object. This object can then be used to efficiently execute this statement multiple times. java.sql Interface PreparedStatement All Superinterfaces: Statement, Wrapper All Known Subinterfaces: CallableStatement

PreparedStatements Method Detail


executeQuery
ResultSet executeQuery() throws SQLException Executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query. Returns:a ResultSet object that contains the data produced by the query; never null Throws:SQLException - if a database access error occurs; this method is called on a closed PreparedStatement or the SQL statement does not return a ResultSet object

executeUpdate
int executeUpdate() throws SQLException Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement. Returns:either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing Throws:SQLException- if a database access error occurs; this method is called on a closed PreparedStatement or the SQL statement returns a ResultSet object

Callable Statement
The interface used to execute SQL stored procedures. The JDBC API provides a stored procedure SQL escape syntax that allows stored procedures to be called in a standard way for all RDBMSs. java.sql Interface CallableStatement All Superinterfaces: PreparedStatement, Statement, Wrapper public interface CallableStatementextends PreparedStatement

JDBC Driver Types


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)

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.

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. 4. Not good for the Web.

You might also like