This document provides an overview of Java Database Connectivity (JDBC) and the steps to connect to a database using JDBC. It discusses the JDBC architecture and the four types of JDBC drivers. It then outlines the steps to connect to a database, which include defining the connection URL, establishing a connection, creating a statement object, executing queries, processing results, and closing the connection. The document also briefly discusses the different types of statements and JDBC data sources as an alternative to the DriverManager class.
1. A presentation about Java Database
Connectivity
Name: Md. Easin Arafat Chowdhury
Department: CSE
Student Id: 162015040
Green University of Bangladesh
Name: Md. Easin Arafat Chowdhury
Department: CSE
Student Id: 162015040
Green University of Bangladesh
2. Overview of JDBC
Java Database Connectivity (JDBC) is an application
programming interface (API) for the programming
language Java, which defines how a client may access a
database. It is part of the Java Standard Edition
platform, from Oracle Corporation. It provides methods
to query and update data in a database, and is oriented
towards relational databases.
2
4. Types of JDBC drivers
• Type-1: jdbc-odbc bridge drivers.
• Type-2: native API partly java driver.
• Type-3: net protocols all java drivers.
• Type-4: native protocols all java drivers.
4
5. Steps to Connect ?
• Define the connection URL.
• Established the connection.
• Create the statement object.
• Execute a query.
• Process the results.
• Close the connection.
5
6. Steps to Connect ?
• Define the connection URL :
Class.for Name();
• For jdbc-odbc bridge driver:-
Class.for Name(“ sun.jdbc.odbc.JdbcOdbcDriver”);
• For Oracle driver:-
Class.for Name(“oracle.jdbc.driver.OracleDriver”);
• For My SQL driver:-
Class.for Name(“com.mysql.jdbcDriver”);
6
7. Steps to Connect ?
• Established the connection:
Connection con
= DriverManager.getConnection(“ url”, “user_name”,
“pass”)
• Create the statement object:
Statement stmt = con.createStatement();
7
8. Steps to Connect ?
• Execute the query:-
For the SELECT query-
String sql = “ SELECT * FROM EMP”;
Stmt.executeQuery(sql);
For the INSERT and UPDATE query-
String sql = “INSERT INTO EMP VALUES(47, “
TEDDY”)”;
Stmt.executeUpdate(sql);
8
9. Steps to Connect ?
• Close the connection and release all the resources that
the connection is holding.
Stmt.close();
Con.close();
9
10. Types of Statement available
• There are three types of statement are available in statement
class:-
Statement
Prepared Statement
Callable Statement
10
11. JDBC Data Source
• The JDBS data source interface is an alternative to
DriverManager class and conventional JDBC url. All the
database information is present in the naming service and
retrieved using the JNDI API. The Data Source object
contrains the connection information which will make the
actual connection and execute the JDBC commands.
11
12. JDBC Data Source
• Each Data Source is assigned a logical name by
conventional beginning with jdbc /.
• Advantage :-
Make the code portable and reuse.
The application does not have to remember the hardcore
driver information.
Connection pooling and distributed transaction
advantages is facilitate.
12
13. JDBC Data Source
• Syntax:-
Context ctx = new initial Context();
Data Source:-
ds = (Data Source)ctx.lookup(“ jdbc/teddyDB”);
Connection:-
Con = ds.getConnection(“ user_name”, “pass”);
13