Outline

„ Persistence via Lecture 10: „ JDBC ( Database Connectivity) Database Connectivity „ JDBC API -JDBC

Wendy Liu CSC309F – Fall 2007

1 2

Java Persistence

„ JDBC (Java Database „ Relational Database Connectivity) Management System „ Object relational (RDBMS) Persistence via Database mapping „ Object-oriented „ Java Data Management (JDO) System (OODBMS) „ Enterprise JavaBean (EJB)

3 4

Three Tier Architecture Database Advantages

„ Data Safety „ data is immune to program crashes „ Concurrent Access „ atomic updates via transactions „ Fault Tolerance „ replicated DBs for instant fail-over on machine/disk crashes „ Data Integrity „ aids to keep data meaningful „ Scalability „ Database „ can handle small/large quantities of data in a uniform manner „ A way of saving and accessing structured data on „ Reporting persistent (disk) storage „ easy to write SQL programs to generate arbitrary reports

5 6

1 Relational Database Movie Database Example

„ First published by Edgar F. Codd in 1970 „ A relational database consists of a collection showtimes of tables showtimeid movieid theaterid sdate stime available 1 1 1 3/20/2005 20:00:00 90 „ A consists of rows and columns 2 1 1 3/20/2005 22:00:00 90 orders „ Each represents a record orderid userid showtimeid 11 1 „ Each represents an attribute of the records contained in the table

7 8

RDBMS Technology

„ Client/Server „ Derby, Oracle, Sybase, MySQL, PointBase, SQLServer JDBC „ Embedded Databases (Java DataBase Connectivity) „ Derby,PointBase „ Personal Databases „ Access

9 10

JDBC Background JDBC Architecture

„ Common SQL database access interface „ Allow Java programs to issue SQL statements and process the results „ Database independence „ Can update underlying database with minimal code impact „ Represent database constructs as objects „ Database connections, SQL statements, result sets, and database metadata http://java.sun.com/docs/books/tutorial/jdbc/basics/index.html

11 12

2 Components of JDBC Architecture - 1 Components of JDBC Architecture - 2

„ Java application „ JDBC Driver „ In need to access database „ Translates API calls to requests made against the „ Uses the API specific database „ JDBC API „ Specific driver required for the chosen database „ Installed on the client. Usually a set of class files „ Provides DB independent abstraction to placed in the class path „ Establish a connection with a database „ „ Send SQL statements All large databases are now supported „ Process the results

13 14

Components of JDBC Architecture - 3

„ DBMS „ The actual database engine „ Derby, MySQL, Oracle, SQL Server, MS Access, PointBase, Postgresql JDBC API

v3.0 v4.0 (May incur different class names in the Derby drivers)

15 16

API Highlights Establishing a Connection

„ http://java.sun.com/javase/6/docs/api/ „ javax.sql „ javax.sql „ DataSource „ getConnection() „ DataSource „ java.sql „ - Read the full method signatures from this API carefully before using - „ public Connection getConnection() „ Connection „ „ commit(), rollback(), setAutoCommit() Attempts to establish a connection with the data source „ For transactions that this DataSource object represents „ createStatement(), prepareStatement() „ For SQL statements „ public Connection getConnection(String username, „ Statement „ executeQuery(), executeUpdate() String password) „ PreparedStatement „ Precompiled SQL statement; more efficient for multiple executions „ Attempts to establish a connection with the data source „ executeQuery(), executeUpdate(), setInt(), setString() that this DataSource object represents using the given „ Parameter index starts from 1 „ ResultSet username and password „ next() „ Accessing next row „ getString(), getInt() „ Retrieving attribute values

17 18

3 Derby Example 1: Connection Executing a Query import org.apache.derby.jdbc.EmbeddedDataSource; „ java.sql import javax.sql.DataSource; „ PreparedStatement import java.sql.*; „ Precompiled SQL statement; more efficient for multiple executions … „ executeQuery(), executeUpdate(), setInt(), setString() // Driver code „ Parameter index starts from 1 EmbeddedDataSource eds = new EmbeddedDataSource(); „ Statement eds.setDatabaseName(dbname); „ executeQuery(), executeUpdate() eds.setCreateDatabase("create"); „ ResultSet … „ next() // JDBC code „ Accessing next row Connection con = eds.getConnection(); „ getString(), getInt() „ Retrieving attribute values

19 20

Example: PreparedStatement Example: Executing Query String insertStmt="INSERT INTO ACCOUNT Statement stmt = con.createStatement(); (NAME, AMOUNT) VALUES (?, ?);"; PreparedStatement ps = con.prepareStatement(insertStmt); // Send the query to the DB, get back a ResultSet ResultSet rs = stmt.executeQuery("SELECT * FROM PART;"); // Fill in the first and second args // Go through all rows returned by the query ps.setString(1,"Charlie Smith"); while(rs.next()){ ps.setDouble(2, 23.45); // Pull out individual columns from the current row int rowsAffected = ps.executeUpdate(); int pno = rs.getInt("PNO"); String pname = rs.getString("PNAME"); // Replace the first and second args // Print out the values ps.setString(1, "Arnold Jones"); System.out.println(pno + "\t“ + pname); ps.setDouble(2, 102.23); } rowsAffected=ps.executeUpdate(); rs.close();

21 22

Executing Update int rowsAffected = stmt.executeUpdate( "DELETE * FROM ACCOUNTS;"); „ Executes SQL INSERT, UPDATE, or Announcements DELETE statements „ Returns the number of rows affected

23 24

4 Midterm Important Announcement (Repeat)

„ Topics covered „ Tuesday Oct 16, 2007 „ Lectures 1-9 inclusive „ 2-hr tutorial given in a CDF lab, BA3185 „ From XHTML to Java Servlets and JSP „ Thursday Oct 18, 2007 „ Use lecture notes as a guideline „ Midterm, BA2195 „ You can bring only 1 page cheat-sheet „ Starting Tuesday Oct 23, 2007 „ Letter size „ „ Double sided The official lecture room will be BA2185 on each Tuesday until the end of term „ Format similar to previous years „ No change to Thursdays’ classes (BA1240) „ Short conceptual questions ~40% „ Programming questions ~60%

25 26

5