EE361 Object Oriented ProgrammingJDBCNov2019
EE361 Object Oriented ProgrammingJDBCNov2019
Programming
JDBC
JDBC
JDBC stands for Java Database Connectivity
JDBC is a standard Java API for providing
connectivity between the Java programming language, and
a wide range of database
It provides a standard library for Java programs to
connect to a database
It generalizes common database access functions
into a set of common classes and methods
Abstracts vendor specific details into a code library
making the connectivity to multiple databases
transparent to user
JDBC
JDBC API Standardizes:
Way to establish connection to database
Approach to initiating queries
Method to create stored procedures
Data structure of the query result
Java and the database
ODBC API
Native API
Client
Application API Protocol Database Specific Database
Protocol
9 11/8/2019
JDBC
Drivers (Type II)
Type II driver communicates directly with native API
Type II makes calls directly to the native API calls
More efficient since there is one less layer to contend with
(i.e. no ODBC)
It is dependent on the existence of a native API for a
database
Driver (Type II)
Native API
Client
Application Database
API Protocol Database Specific
Protocol
10 11/8/2019
JDBC
Drivers (Type III)
Type III driver make calls to a middleware component running on
another server
This communication uses a database independent net protocol
Middleware server then makes calls to the database using database-
specific protocol
The program sends JDBC call through the JDBC driver to the middle
tier
Middle-tier may use Type I or II JDBC driver to communicate with the
database.
Driver (Type III)
Client Middleware
Application Net Protocol Server Database Specific Database
Protocol
11 11/8/2019
JDBC
Drivers (Type IV)
Type IV driver is an all-Java driver that is also called a thin
driver
It issues requests directly to the database using its native protocol
It can be used directly on platform with a JVM
Most efficient since requests only go through one layer
Simplest to deploy since no additional libraries or middle-ware
Driver (Type IV)
Client
Application Database Specific Database
Protocol
12 11/8/2019
Structured Query Language (SQL)
Product
Tuples or rows
Introduction to SQL
SQL functions fit into two broad categories:
Data definition language
SQL includes commands to:
Create database objects, such as tables, indexes, and views
Define access rights to those database objects
Data manipulation language
Includes commands to insert, update, delete, and retrieve data within
database tables
15
CRUD OPERATIONS
• CRUD stands for create, read, update, delete.
• Create statement in SQL looks like
– Create table mytab ( mynum number , name varchar2(25));
• READ statement looks like
– Select * from mytab where mynum=25;
• UPDATE statement looks as
– Update mytab set mynum=88 where mynum=25;
• DELETE statement like
– Delete from mytab where mynum=88;
Create Database
33
1. Establish a connection
import java.sql.*;
Load the vendor specific driver
Class.forName("oracle.jdbc.driver.OracleDriver");
Dynamically loads a driver class, for Oracle database
Make the connection
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@oracle-prod:1521:OPROD",
username, passwd);
Establishes connection to database by obtaining
a Connection object
34
2. Create JDBC statement(s)
Statement stmt = con.createStatement() ;
Creates a Statement object for sending SQL statements to the
database
35
Executing SQL Statements
String createLehigh = "Create table Lehigh " +
"(SSN Integer not null, Name VARCHAR(32), " +
"Marks Integer)";
stmt.executeUpdate(createLehigh);
//What does this statement do?
36
Get ResultSet
String queryLehigh = "select * from Lehigh";
ResultSet rs = Stmt.executeQuery(queryLehigh);
//What does this statement do?
while (rs.next()) {
int ssn = rs.getInt("SSN");
String name = rs.getString("NAME");
int marks = rs.getInt("MARKS");
}
37
Close connection
stmt.close();
con.close();
38
Sample program
import java.sql.*;
class Test {
public static void main(String[] args) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //dynamic loading of driver
String filename = "c:/db1.mdb"; //Location of an Access database
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
database+= filename.trim() + ";DriverID=22;READONLY=true}"; //add on to end
Connection con = DriverManager.getConnection( database ,"","");
Statement s = con.createStatement();
s.execute("create table TEST12345 ( firstcolumn integer )");
s.execute("insert into TEST12345 values(1)");
s.execute("select firstcolumn from TEST12345");
39
Sample program(cont)
ResultSet rs = s.getResultSet();
if (rs != null) // if rs == null, then there is no ResultSet to view
while ( rs.next() ) // this will step through our data row-by-row
{ /* the next line will get the first column in our current row's ResultSet
as a String ( getString( columnNumber) ) and output it to the screen */
System.out.println("Data from column_name: " + rs.getString(1) );
}
s.close(); // close Statement to let the database know we're done with it
con.close(); //close connection
}
catch (Exception err) { System.out.println("ERROR: " + err); }
}
}
40