Top Banner
JDBC by Jon Pearce
30

JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

JDBC

by Jon Pearce

Page 2: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

DBase Concepts

Table

Row*

1

Field

*1

1

*

1*

DBMSDataBase

*

1

*

1* 1* 1

< manages

Page 3: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

Terms

• Table

• Row/Entity

• Column/Field/Attribute

• Key/Primary Key/Foreign Key

Page 4: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

Entity-Relation Diagram

Person

OID

FIRST

LAST

ADDRESS

PHONE

Phone

OID

AREA

NUMBER

Address

OID

STREET

CITY

STATE

1

*

*

1

Page 5: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

Person Table

OID FIRST LAST ADDRESS PHONE

500 Bill Ding 282 150

501 Carl LaFong 283 151

Page 6: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

Address Table

OID STREET CITY STATE

282123 Sesame

NYC NY

28377 Sunset

LA CA

Page 7: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

Phone Table

OIDAREA CODE

NUMBER

150 212 555-4321

151 213 333-5245

Page 8: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

SQL

• SELECT-FROM-WHERE

• INSERT INTO

• UPDATE

• DELETE-FROM

Page 9: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

Select

SELECT field1, field2, field3 ... FROM table1, table2, table3 ... WHERE condition

Page 10: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

Projection

SELECT first, last FROM Person

Page 11: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

Selection

SELECT * FROM Person WHERE last LIKE 'D%'

Page 12: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

Join

SELECT * FROM Person, Address WHERE Person.Address = Address.OID

Page 13: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

Insert

INSERT INTO table (field, field, ...) VALUES (value, value, ...)

Page 14: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

Update

UPDATE table SET field = value, ... WHERE condition

Page 15: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

Delete

DELETE FROM table WHERE condition

Page 16: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

Cloudview

Page 17: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

java.sql

Connection

createStatement()commit()rollback()

<<Interface>>

java.xml

DriverManager

getDriver()Driver

<<Interface>>

connect()

<<creates>>

<<creates>>

Statement<<Interface>>

executeQuery()

<<creates>>

ResultSet<<Interface>>

<<creates>>

Page 18: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

JDBC Driver Types

• JDBC-to-ODBC Bridge

• Native API partly Java drivers

• JDBC-Net pure Java drivers

• Native-protocol pure Java drivers

Page 19: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

JDBC Driver Types II

Driver<<Interface>>

VendorAPI

PureJava JNI

NetBased Bridge

ODBCDriver

Page 20: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

Informix Cloudscape

CSServerCSClient <<RMI>>

Web Appframeworks\RmiJdbc

Page 21: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

An SQL Browser

Console

controlLoop()help()about()handle()execute()

Connection<<Interface>>

Statement<<Interface>>

<<creates>>

SQLBrowser

execute()

ResultSet<<Interface>>

<<creates>>

Reusable CUI Class

Page 22: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

SQL Browser Declaration

public class SQLBrowser extends Console { protected String driverName; protected String dbaseName; protected Connection connection; protected Statement statement; protected ResultSet result; public SQLBrowser(String db) throws SQLException, ClassNotFoundException {...} public void finalize() throws SQLException {...} private String toString (ResultSet rs) throws SQLException {...} public String execute(String sql) throws AppError {...}}

Page 23: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

Connecting

public SQLBrowser(String db) throws SQLException, ClassNotFoundException { Class.forName(driverName); dbaseName = "jdbc:cloudscape:rmi:" + db; connection = DriverManager.getConnection(dbaseName); statement = connection.createStatement(); meta = connection.getMetaData();}

Page 24: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

Executing a Query

public String execute(String sql) throws AppError { String answer = "???"; try { result = statement.executeQuery(sql); answer = toString(result); } catch (SQLException e) { } return answer;}

Page 25: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

Processing a Result Set

private String toString (ResultSet rs) throws SQLException { StringBuffer results = new StringBuffer(); ResultSetMetaData metaData = rs.getMetaData(); int numCols = metaData.getColumnCount(); for(int i = 1; i <= numCols; i++) { // get column names results.append(metaData.getColumnName(i) + "\t"); } results.append("\n"); while(rs.next()) { // get next row for(int i = 1; i <= numCols; i++) { results.append(rs.getObject(i) + "\t"); } results.append("\n"); } return results.toString();}

Page 26: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

Closing the Connection

public void finalize() throws SQLException { statement.close(); connection.close();}

Page 27: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

DBase DAO

public class DBaseDAO { protected String driver; protected String url; // "protocol:subprotocol:dbase" protected Connection connection; public DBaseDAO(String d, String u) throws DAOException {...} protected void connect() throws Exception {...} public void close() throws DAOException {...} protected void finalize() {...}}

Page 28: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

Connecting

protected void connect() throws Exception { Class.forName( driver ); connection = DriverManager.getConnection( url ); connection.setAutoCommit( false );}

Page 29: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

DAO Pattern

PersonDAO<<Interface>>

insert()delete()find()update()

DAOFactory<<Interface>>

makeDAOFactory()makePersonDAO()

CloudscapeDAOFactgory

makePersonDAO()

<<creates>>

DBasePersonDAO

<<creates>>

uses JDBC

Page 30: JDBC by Jon Pearce. DBase Concepts Terms Table Row/Entity Column/Field/Attribute Key/Primary Key/Foreign Key.

DBasePersonDAO

PersonDAO<<Interface>>

insert()delete()update()find()

Driver<<Interface>>

DBaseDAO

connect()

Statement<<Interface>>

Connection<<Interface>>

<<creates>>

<<creates>> DBasePersonDAO

PreparedStatement<<Interface>>

**