Sapna Saxena: Presented by
Sapna Saxena: Presented by
Sapna Saxena: Presented by
Sapna Saxena
Lecturer, CSE Department
Chitkara University
Introduction
A RDBMS is usually the primary data resource in
an enterprise application.
The JDBC API provides developers with a way to
connect to relational data from within Java code.
Using the JDBC API, developer can create a client
that can connect to a database, execute SQL
statements, and processes the result of those
statements.
Introduction
JDBC helps you to write java applications that
manage these three programming activities:
1. Connect to a data source, like a database
2. Send queries and update statements to the
database
3. Retrieve and process the results received from
the database in answer to your query
In a package called java.sql, JDBC consists of a
set of class files.
Classes of jav.sql package
java.sql.DriverManager: loads driver,
and creates connection to the database
java.sql.Driver: represents driver
java.sql.Connection: represents a
connection to the database
java.sql.Statement: executes statements
java.sql.Resultset: holds results of
executing statements
JDBC–ODBC
The ODBC class of databases are a subset of ANSI
SQL-2 databases.
It is a standard proposed by Microsoft.
In Computing, Open Database Connectivity
(ODBC) provides a standard software API
method for using DBMSs.
The designers of ODBC aimed to make it
independent of programming languages,
database systems, and Operating systems.
JDBC–ODBC Bridge
Java soft in a join effort with Intersolv that created
an implementation of the java.sql interface which is
designed to allow access to ODBC databases in
order to get JDBC.
There is an ODBC driver for each type of database.
In addition, the application talks to a particular
database through the ODBC driver manager. The
driver manager translates between the application
and the ODBC driver and the ODBC driver
translates between the driver manager and the
particular database.
JDBC Architecture
ResultSet ResultSet ResultSet
Statement PreparedStatement CallableStatement
Connection
JDBC – ODBC
Bridge
ODBC Driver
Oracle Sybase
Database Database
Oracle
Database
JDBC Drivers
Driver must be registered with the JDBC
DriverManager, before using Driver. This is done by
using Class.forName() method, as –
try {
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Class.forName(“com.oracle.jdbc.OracleDriver”);
} catch (ClassNotFoundexception ce) {
// Handle exception
}
JDBC Driver Types
There are following types of JDBC Drivers –
ResultSet rs = Stmt.executeQuery(queryStudent);
Iterate ResultSet
while (rs.next()) {
int ssn = rs.getInt("SSN");
String name = rs.getString("NAME");
int marks = rs.getInt("MARKS");
}
Close connection
stmt.close();
con.close();
import java.sql.*;
import javax.sql.*;
import java.lang.Object;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
public class Display {
public static void main(String[] args) {
Connection conn;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:sap","","");
Statement stmt = conn.createStatement();
ResultSet r = stmt.executeQuery ("SELECT * FROM emp");
while (r.next()){
System.out.println("Name : " + r.getString(1) + " " +
r.getString(2));
System.out.println("Designation : " + r.getString(3));
System.out.println("Salary : " + r.getString(4) + "\n");
}
r.close();
stmt.close();
conn.close();
} catch (Exception e) {
System.out.println("ERROR : " + e);
e.printStackTrace(System.out);
}
}
}
Creating Table
Following table is to be created –
Employee_ID Name
6323 Hemanth
5768 Bob
1234 Shawn
5678 Michaels
PreparedStatement updateSales =
con.prepareStatement( "UPDATE COFFEES SET
SALES = ? WHERE COF_NAME LIKE ? ");
updateSales.setInt(1, 75);
updateSales.setString(2, "Colombian");
updateSales.executeUpdate():
Using a Loop to Set Values
Looking at these examples, you might wonder why
you would choose to use a PreparedStatement object
with parameters instead of just a simple statement,
since the simple statement involves fewer steps. If
you were going to update the SALES column only
once or twice, then there would be no need to use an
SQL statement with input parameters. If you will be
updating often, on the other hand, it might be much
easier to use a PreparedStatement object, especially
in situations where you can use a for loop or while
loop to set a parameter to a succession of values.
PreparedStatement updateSales; String updateString
= "update COFFEES " + "set SALES = ? where
COF_NAME like ?";
updateSales = con.prepareStatement(updateString);
int [] salesForWeek = {175, 150, 60, 155, 90};
String [] coffees = {"Colombian", "French_Roast",
"Espresso", "Colombian_Decaf",
"French_Roast_Decaf"};
int len = coffees.length;
for(int i = 0; i < len; i++) {
updateSales.setInt(1, salesForWeek[i]);
updateSales.setString(2, coffees[i]);
updateSales.executeUpdate();
}
Stored Procedures
A stored procedure is a group of SQL statements
that form a logical unit and perform a particular
task. Stored procedures are used to encapsulate a
set of operations or queries to execute on a
database server. For example, operations on an
employee database (hire, fire, promote, lookup)
could be coded as stored procedures executed by
application code. Stored procedures can be
compiled and executed with different parameters
and results, and they may have any combination
of input, output, and input/output parameters.
Stored Procedures
In some DBMSs, the following SQL statement
creates a stored procedure:
create procedure SHOW_SUPPLIERS
as
select SUPPLIERS.SUP_NAME,
COFFEES.COF_NAME from SUPPLIERS, COFFEES
where SUPPLIERS.SUP_ID = COFFEES.SUP_ID
order by SUP_NAME
Stored Procedures
The following code puts the SQL statement into a
string and assigns it to the variable
createProcedure, which we will use later:
String createProcedure = "create procedure
SHOW_SUPPLIERS " + "as " + "select
SUPPLIERS.SUP_NAME, COFFEES.COF_NAME " +
"from SUPPLIERS, COFFEES " + "where
SUPPLIERS.SUP_ID = COFFEES.SUP_ID " + "order
by SUP_NAME";
Calling a Stored Procedure
from JDBC
JDBC allows you to call a database stored
procedure from an application written in the Java
programming language.
The first step is to create a CallableStatement
object. As with Statement and
PreparedStatement objects, this is done with an
open Connection object.
A callableStatement object contains a call to a
stored procedure; it does not contain the stored
procedure itself.
Calling a Stored Procedure
from JDBC
The first line of code below creates a call to the stored
procedure SHOW_SUPPLIERS using the connection
con. The part that is enclosed in curly braces is the
escape syntax for stored procedures. When the driver
encounters "{call SHOW_SUPPLIERS}", it will translate
this escape syntax into the native SQL used by the
database to call the stored procedure named
SHOW_SUPPLIERS.
CallableStatement cs = con.prepareCall("{call
SHOW_SUPPLIERS}");
ResultSet rs = cs.executeQuery();