Object oriented analysis and
design
Module 13: JDBC
JDBC
• JDBC (Java Database Connectivity) API
allows Java programs to connect to databases
• Database access is the same for all database
vendors
• The JVM uses a JDBC driver to translate
generalized JDBC calls into vendor specific
database calls
12/11/22 2
Typical JDBC Programming
Procedure
1. Load the database driver
2. Obtain a connection
3. Create and execute statements (SQL queries)
4. Use result sets (tables) to navigate through
the results
5. Close the connection
12/11/22 3
Driver Manager
• The purpose of the java.sql.DriverManager 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,
so that the DriverManager is aware of it
• Load the database driver using
ClassLoader :Class.forName(“oracle.jdbc.driver.Or
acleDriver”);
12/11/22 © Wu, Lee & Offutt 4
Connecting to a Database
• JDBC Driver – Oracle Server
Class.forName (“oracle.jdbc.driver.OracleDriver”);
con = DriverManager.getConnection (
“jdbc:oracle:thin:@bonsai.ite.gmu.edu:1521:ite”,
“accountname", “password”);
• JDBC Driver – MySQL Server
Class.forName (“org.gjt.mm.mysql.Driver”);
con = DriverManager.getConnection
(“jdbc:mysql://localhost/databasename”, uid, passwd);
12/11/22 5
Creating Tables
• Creating a Coffee table
CREATE TABLE COFFEES (COF_NAME VARCHAR(32),
SUP_ID INTEGER, PRICE FLOAT, SALES INTEGER,
TOTAL INTEGER)
SQL query
• Creating JDBC statements
Statement stmt = con.createStatement ();
• Execute a statement
stmt.executeUpdate (“CREATE TABLE
COFFEES “ + “(COF_NAME VARCHAR(32),
SUP_ID INTEGER, PRICE FLOAT, “ +
“SALES INTEGER, TOTAL INTEGER)”);
12/11/22 6
Execute Statements
• This uses executeUpdate because the SQL
statement contained in createTableCoffees
is a DDL (data definition language)
statement
• Statements that create a table, alter a table,
or drop a table are all examples of DDL
statements and are executed with the
method executeUpdate
• executeUpdate is also used to execute
SQL statements that update a table
12/11/22 7
Execute Statements
• In practice, executeUpdate is used far more
often to update tables than it is to create
them because a table is created once but
may be updated many times
• The method used most often for executing
SQL statements is executeQuery
• executeQuery is used to execute SELECT
statements, which comprise the vast
majority of SQL statements
12/11/22 8
JDBC Data Source Architecture
Application JDBC Database
JNDI
Connection Manager
12/11/22 9
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");
10
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); }
}
}
11