2. What is JDBC?
JDBC API allows Java programs to
connect to DBs
Provides cross-vendor connectivity and
data access across relational databases
from different vendors
Classes and interfaces allow users to
access the database in a standard way
The JVM uses the JDBC driver to
translate generalized JDBC calls into
vendor specific database calls
3. What Does JDBC Do ?
JDBC makes it possible to do three things:
Establish a connection with a database
Send SQL statements
Process the results.
4. JDBC Classes for DB
Connection
java.sql.Driver
◦ Unless creating custom JDBC implementation, never
have to deal with it. It gives JDBC a launching point
for DB connectivity by responding to
DriverManager connection requests
java.sql.DriverManager
◦ Maintains a list of Driver implementations and
presents an application with one that matches a
requested URL.
◦ getConnection(url, uid, password)
◦ getDrivers(), registerDriver()
java.sql.Connection
◦ Represents a single logical DB connection; used for
sending SQL statements
5. JDBC Drivers
JDBC-ODBC bridge
Part Java, Part Native Driver
Intermediate DAccess Server
Pure Java Drivers
6. Driver Manager
The purpose of the
java.sql.DriverManger class in JDBC is
to provide a common access layer on top
of different database drivers used in an
application
DriverManager requires that each driver
required by the application must be
registered before use
Load the database driver using
ClassLoader
Class.forName(“oracle.jdbc.driver.Oracle
Driver”);
7. Registering a JDBC Driver
Driver must be registered before it is
used.
Drivers can be registered in three
ways.
8. Most Common Approach is
To use Java’s Class.forName()
Dynamically loads the driver’s class
file into memory
Preferable because
It allows driver registration
configurable and portable.
9. The second approach you can use to
register a driver is to use the static
DriverManager.registerDriver( )
method.
Use the registerDriver( ) method if
you
are using a non-JDK compliant JVM.
For example:
DriverManager.registerDriver(new
oracle.jdbc.driver.OracleDriver( ));
10. The third approach is to use a
combination of Class.forName( ) to
dynamically load the Oracle driver and
then the driver classes' getInstance( )
method to work around noncompliant
JVMs.
For example:
Class.forName("oracle.jdbc.driver.Oracle
Driver").newInstance();
11. Basic steps to use
a database in Java
Establish a connection
Create JDBC Statements
Execute SQL Statements
GET ResultSet
Close connections
12. 1. Establish a connection
import java.sql.*;
Load the vendor specific driver
◦ Class.forName("oracle.jdbc.driver.OracleDriver");
What do you think this statement does, and how?
Dynamically loads a driver class, for Oracle database
Make the connection
◦ Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@oracle-prod:1521:OPROD", username,
passwd);
What do you think this statement does?
Establishes connection to database by obtaining
a Connection object
13. 2. Create JDBC statement(s)
Statement stmt = con.createStatement()
;
Creates a Statement object for
sending SQL statements to the
database
14. 3.Executing SQL Statements
String createStudent = "Create table
Student " +
"(SSN Integer not null, Name
VARCHAR(32), " + "Marks Integer)";
stmt.executeUpdate(createStudent);
//What does this statement do?
String insertStudent = "Insert into
Student values“ +
"(123456789,abc,100)";
stmt.executeUpdate(insertStudent);
15. Execute Statements
This uses executeUpdate because the
SQL statement contained in
createTableCoffees is a data definition
language ( DDL ) statement
DDL statements are executed with
executeUpdate
– Create a table
– Alter a table
– Drop a table
executeUpdate is also used to execute
SQL statements that update a table
16. Execute Statements
executeUpdate is used far more often
to update tables than to create them–
We create a table once but update it
many times
The method used most often for
executing SQL statements is
executeQuery
executeQuery is used to execute
SELECT statements – SELECT
statements are the most common SQL
statements
17. Entering Data to a Table
Statement stmt = con.createStatement();
stmt.executeUpdate ( "INSERT INTO COFFEES
" +
"VALUES ('Colombian', 101, 7.99, 0, 0)");
stmt.executeUpdate ("INSERT INTO COFFEES "
+
"VALUES ('French_Roast', 49, 8.99, 0, 0)");
stmt.executeUpdate ("INSERT INTO COFFEES "
+
"VALUES ('Espresso', 150, 9.99, 0, 0)");
stmt.executeUpdate ("INSERT INTO COFFEES "
+
"VALUES ('Colombian Decaf' 101 8 99 0 0)");
stmt.executeUpdate ("INSERT INTO COFFEES "
+
18. Batch Update
What is batch update?
- Send multiple update statement in a
single request to the database
Why batch update?
-Better performance
How do we perform batch update?
-Statement.addBatch (sqlString);
- Statement.executeBatch();
19. 4.Get ResultSet
String queryStudent = "select * from Student";
ResultSet rs =
Stmt.executeQuery(queryStudent);
//What does this statement do?
while (rs.next()) {
int ssn = rs.getInt("SSN");
String name = rs.getString("NAME");
int marks = rs.getInt("MARKS");
}
21. Sample Program
import java.sql.*;
class TestThinApp {
public static void main (String args[])
throws ClassNotFoundException,
SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver
");
// or you can use:
DriverManager.registerDriver(new
oracle.jdbc.driver.OracleDriver());
Connection conn =
DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","scott","ti
23. SUMMARY
JDBC makes it very easy to connect
to DBMS and to manipulate the data
in it.
You have to install the oracle driver in
order to make the connection.
It is crucial to setup PATH and
CLASSPATH properly