Java the UML Way http www tisip noJava











- Slides: 11
Java the UML Way http: //www. tisip. no/Java. The. Uml. Way/ Programming with Databases What is JDBC? Database drivers The example Database Establishing database contact The relationship between SQL data types, Java data types and getxxx() methods A bigger example A database application The three-layer architecture Transactions Compiled SQL statements versjon 2002 -04 -17 page 2 page 3 page 4 page 5 page 6 page 7 page 8 page 9 page 10 page 11 Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0 -470 -84386 -1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http: //tisip. no/engelsk/ Chapter 20
What is Java Database Connectivity (JDBC)? • JDBC is – not a database system, but makes communication with a database system possible – not a ”point and click, drag and drop” tool, such as Microsoft Access, but it may be used by the developers of such tools to communicate with the database. • JDBC is an API offering classes that makes it possible to send SQL statements to a database, and to interpret the results from the statements. • This chapter assumes that you are familiar with relational databases and SQL. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0 -470 -84386 -1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http: //tisip. no/engelsk/ 2
Database Drivers Java application JDBC Driver Manager JDBC-ODBC Bridge Oracle Driver Sybase Driver ODBC Driver Microsoft Access Microsoft SQL Server Oracle Database Sybase Database Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0 -470 -84386 -1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http: //tisip. no/engelsk/ A database driver consists of classes that implement certain interfaces. 3
The Example Database create table person( identno integer primary key, firstname varchar(30) not null, lastname varchar(30) not null); insert into person values (100, 'EDWARD', 'BROWN'); insert into person values (101, 'ANN MARGARET', 'GREEN'); insert into person values (102, 'JOHN', 'JOHNSON'); identno firstname lastname 100 EDWARD BROWN 101 ANN MARGARET GREEN 102 JOHNSON Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0 -470 -84386 -1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http: //tisip. no/engelsk/ 4
A Little Program Communicating With the Database import javax. swing. *; import java. sql. *; class Database. Contact { public static void main(String[] args) throws Exception { String database. Driver = "oracle. jdbc. driver. Oracle. Driver"; Class. for. Name(database. Driver); String user. Name = JOption. Pane. show. Input. Dialog("User Name: "); String password = JOption. Pane. show. Input. Dialog("Password: "); String database. Name = "jdbc: oracle: thin: @loiosh. stud. idb. hist. no: 1521: orcl"; Connection conn = Driver. Manager. get. Connection(database. Name, user. Name, password); Statement statement = conn. create. Statement(); Result. Set res = statement. execute. Query("select * from person"); while (res. next()) { int id. No = res. get. Int("ident. No"); String first. Name = res. get. String("firstname"); Our database driver is the String last. Name = res. get. String("lastname"); System. out. println(id. No + ": " + first. Name + " " + last. Name); classes 111. zip file. CLASSPATH } has to contain classes 111. zip. res. close(); This file contains, statement. close(); conn. close(); among other things System. exit(0); oracle. jdbc. driver. Oracle. Driver. } } Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0 -470 -84386 -1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http: //tisip. no/engelsk/ 5
The Relationship Between SQL Data Type, Java Data Types, and getxxx() Methods CHAR, VARCHAR, LONGVARCHAR String get. String() NUMERIC, DECIMAL java. math. Big. Decimalget. Big. Decimal() BIT boolean get. Boolean() TINYINT byte get. Byte() SMALLINT short get. Short() INTEGER int get. Int() BIGINT long get. Long() REAL float get. Float() FLOAT, DOUBLE double get. Double() BINARY, VARBINARY, LONGVARBINARY byte[] get. Bytes() DATE java. sql. Date get. Date() TIME java. sql. Time get. Time() TIMESTAMP java. sql. Timestamp get. Timestamp() Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0 -470 -84386 -1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http: //tisip. no/engelsk/ 6
A Bigger Example Database get. All() update. Name(the. Person: Person) register. New. Person(first. Name: String, last. Name: String) delete. Person(ident. No: int) close. The. Connection() Show program listing 20. 2, pp. 629 -634. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0 -470 -84386 -1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http: //tisip. no/engelsk/ 7
A Database Application Show program listing 20. 3, pp. 635 -639. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0 -470 -84386 -1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http: //tisip. no/engelsk/ 8
The Three-Layer Architecture 1. The physical database 2. The component which communicates with the database 3. The user interface component which communicates with the component above. Solve the problems, page 641. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0 -470 -84386 -1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http: //tisip. no/engelsk/ 9
Transactions • A transaction is a logical unit of work and can consist of multiple update statements to the database. • For example, if money is going to be transferred from one account to another, it’s important that the balances in both of the accounts change. If an error occurs, we can’t risk that only one of the accounts changes its balance. This transaction consists of two update statements. • The transaction is worked out in a correct way: Commit! • The transaction is not worked out in a correct way: Rollback! • As default, every single SQL statement is a transaction unit. • The Connection interface let us create transactions consisting of more than one SQL statement. – public void set. Auto. Commit(boolean autocommit) // default is true – public void commit() – public void rollback() Solve problem 1, page 645. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0 -470 -84386 -1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http: //tisip. no/engelsk/ 10
Compiled SQL Statements • The Java interpreter sends the SQL statement to the database system. • The database system sets up a plan so that the search can be done in the most efficient way possible. • Unnecessary to compile again if only literals are changed: – select * from person where first. Name like ’ANNE’ and last. Name like ’GREEN’; – select * from person where first. Name like ’JOHN’ and last. Name like ’JOHNSON’; • The Prepared. Statement interface let us create precompiled statement objects. An example: String sql. Statement = "select * from person where first. Name like ? and last. Name like ? "; Prepared. Statement statement = conn. prepare. Statement(sql. Statement); setning. set. String(1, ”ANNE”); // The search criteria may be input, setning. set. String(2, ”GREEN”); // see program listing 20. 4 pp. 644 -645. Result. Set res = statement. execute. Query(); while (res. next()) { …osv…. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0 -470 -84386 -1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http: //tisip. no/engelsk/ 11