JDBC (Java Database Connectivity) : Snu Oopsla Lab. October 2005
JDBC (Java Database Connectivity) : Snu Oopsla Lab. October 2005
JDBC (Java Database Connectivity) : Snu Oopsla Lab. October 2005
Contents
Overview
History of JDBC JDBC Model JDBC Driver Type
Overview (1/2)
JDBC
JDBC is a standard interface for connecting to relational databases from Java The JDBC Classes and Interfaces are in the java.sql package JDBC is Java API for executing SQL statements
Provides a standard API for tool/database developers Possible to write database applications using a pure Java API Easy to send SQL statements to virtually any relational database
JDBC Driver
Database Command
Database
Overview (2/2)
Reason for JDBC
Database vendors (Microsoft Access, Oracle etc.) provide proprietary (non standard) API for sending SQL to the server and receiving results from it Languages such as C/C++ can make use of these proprietary APIs directly High performance Can make use of non standard features of the database All the database code needs to be rewritten if you change database vendor or product JDBC is a vendor independent API for accessing relational data from different database vendors in a consistent way
JDBC Model
JAVA Applet/ Application
Java Application Developer
JDBC API
Driver Manager Driver API
Vender Specific JDBC developer JDBC Developer
Database Database
Query
Process Results
Close
1) Close the result set 2) Close the statement 3) Close the connection
Skeleton Code
Class.forName(DRIVERNAME); Loading a JDBC driver
Executing SQL
Ex) jdbc:mysql://oopsla.snu.ac.kr/mydb
Vendor of database, Location of Vendor of database, Location of database server and name of database server and name of database database
getString(), getInt(), and getXXX() assign each value to a Java variable ResultSet Internal Pointer
Record 1
Record 2
Record 3
Record 4
NOTE
While (rs.next()){
int id = rs.getInt(ID); String name = rs.getString(name); float score = rs.getFloat(score); System.out.println(ID= + id + + name + + score);}
ID
1 2 3
You must step the cursor to the first record before read the results This code will not skip the first record
name
James Smith Donald
score
90.5 45.7 80.2
Output ID=1 James 90.5 ID=2 Smith 45.7 ID=3 Donald 80.2
Table1
Summary
JDBC
Standard interface for connecting to relational databases from Java Vendor independent API for accessing relational data JDBC has four driver type
JDBC-ODBC bridge plus ODBC driver Native-API partly-Java driver JDBC-Net pure Java driver Native Protocol pure Java API driver
Online Resources
Suns JDBC site
http://java.sun.com/products/jdbc/
JDBC tutorial
http://java.sun.com/docs/books/tutorial/jdbc/