JDBC - Drivers
JDBC - Drivers
What is JDBC?
An API that lets you access virtually any tabular data source from the Java programming language
Whats an API? See J2SE documentation Whats a tabular data source? access virtually any data source, from relational databases to spreadsheets and flat files.
JDBC Documentation
General Architecture
What design pattern is implied in this architecture? What does it buy for us? Why is this architecture also multi-tiered?
1.Establish a connection 2.Create JDBC Statements 3.Execute SQL Statements 4.GET ResultSet 5.Close connections
1. Establish a connection
Class.forName("oracle.jdbc.driver.OracleDriver");
What do you think this statement does, and how? Dynamically loads a driver class, for Oracle database
What do you think this statement does? Establishes connection to database by obtaining a Connection object
6
String createLehigh = "Create table Lehigh " + "(SSN Integer not null, Name VARCHAR(32), " + "Marks Integer)"; stmt.executeUpdate(createLehigh); //What does this statement do? String insertLehigh = "Insert into Lehigh values + "(123456789,abc,100)"; stmt.executeUpdate(insertLehigh);
8
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"); }
Close connection
stmt.close(); con.close();
10
JDBC allows SQL statements to be grouped together into a single transaction Transaction control is performed by the Connection object, default mode is auto-commit, I.e., each sql statement is treated as a transaction We can turn off the auto-commit mode with con.setAutoCommit(false); And turn it back on with con.setAutoCommit(true); Once auto-commit is off, no SQL statement will be committed until an explicit is invoked con.commit(); At this point all changes done by the SQL statements will be made permanent in the database.
11
Programs should recover and leave the database in a consistent state. If a statement in the try block throws an exception or warning, it can be caught in one of the corresponding catch statements How might a finally {} block be helpful here? E.g., you could rollback your transaction in a catch { } block or close database connection and free database related resources in finally {} block
12
13
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");
14
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); } } }
15
16
Metadata from DB
A Connection's database is able to provide schema information describing its tables, its supported SQL grammar, its stored procedures the capabilities of this connection, and so on
What is a stored procedure? Group of SQL statements that form a logical unit and perform a particular task
20
21
API for network-wide sharing of information about users, machines, networks, services, and applications Preserves Javas object model Models persistence of objects, using RDBMS as repository Save, load objects from RDBMS Standardized and optimized by Sybase, Oracle and IBM Java extended with directives: # sql SQL routines can invoke Java methods Maps SQL types to Java classes
22
JDBC references
JDBC Documentation
java.sql package
http://java.sun.com/docs/books/jdbc/
24
JDBC
JDBC Documentation
java.sql package
http://java.sun.com/docs/books/jdbc/
25