0% found this document useful (0 votes)
73 views

Outline: Wendy Liu CSC309F - Fall 2007

This document provides an overview of database connectivity using JDBC. It discusses persistence via databases, the JDBC API, and how JDBC provides a common interface for connecting to and interacting with various database systems from Java applications. The document also includes examples of using JDBC to interact with a movie database, describing tables, rows, columns and how JDBC handles connections, statements and result sets.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

Outline: Wendy Liu CSC309F - Fall 2007

This document provides an overview of database connectivity using JDBC. It discusses persistence via databases, the JDBC API, and how JDBC provides a common interface for connecting to and interacting with various database systems from Java applications. The document also includes examples of using JDBC to interact with a movie database, describing tables, rows, columns and how JDBC handles connections, statements and result sets.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Outline

„ Persistence via Database


„ JDBC (Java Database Connectivity)
Lecture 10:
„ JDBC API
Database Connectivity
- 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 Object Database 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 table consists of rows and columns 2 1 1 3/20/2005 22:00:00 90

„ Each row represents a record


orders
orderid userid showtimeid
1 1 1
„ Each column represents an attribute of the
records contained in the table

7 8

RDBMS Technology
„ Client/Server Databases
„ 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
„ Specific driver required for the chosen database
„ JDBC API
„ 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()

„ Statement
„ For SQL statements
„ public Connection getConnection(String username,
„ 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
„ Letter
size
„ Starting Tuesday Oct 23, 2007
„ The official lecture room will be BA2185 on each
„ Double sided
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

You might also like