Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java the UML Way versjon 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and.

Similar presentations


Presentation on theme: "Java the UML Way versjon 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and."— Presentation transcript:

1 Java the UML Way http://www.tisip.no/JavaTheUmlWay/ versjon 2002-04-17 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 Programming with Databases What is JDBC?page 2 Database driverspage 3 The example Databasepage 4 Establishing database contactpage 5 The relationship between SQL data types, Java data types and getxxx() methodspage 6 A bigger examplepage 7 A database applicationpage 8 The three-layer architecturepage 9 Transactionspage 10 Compiled SQL statementspage 11

2 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, page 2 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.

3 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, page 3 Database Drivers A database driver consists of classes that implement certain interfaces. JDBC Driver Manager JDBC-ODBC Bridge ODBC Driver Microsoft Access Microsoft SQL Server Oracle Database Sybase Database Oracle DriverSybase Driver Java application Java application Java application

4 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, page 4 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'); identnofirstnamelastname 100 EDWARDBROWN 101ANN MARGARETGREEN 102JOHNJOHNSON

5 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, page 5 A Little Program Communicating With the Database import javax.swing.*; import java.sql.*; class DatabaseContact { public static void main(String[] args) throws Exception { String databaseDriver = "oracle.jdbc.driver.OracleDriver"; Class.forName(databaseDriver); String userName = JOptionPane.showInputDialog("User Name: "); String password = JOptionPane.showInputDialog("Password: "); String databaseName = "jdbc:oracle:thin:@loiosh.stud.idb.hist.no:1521:orcl"; Connection conn = DriverManager.getConnection(databaseName, userName, password); Statement statement = conn.createStatement(); ResultSet res = statement.executeQuery("select * from person"); while (res.next()) { int idNo = res.getInt("identNo"); String firstName = res.getString("firstname"); String lastName = res.getString("lastname"); System.out.println(idNo + ": " + firstName + " " + lastName); } res.close(); statement.close(); conn.close(); System.exit(0); } Our database driver is the classes111.zip file. C LASSPATH has to contain classes111.zip. This file contains, among other things oracle.jdbc.driver.OracleDriver.

6 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, page 6 The Relationship Between SQL Data Type, Java Data Types, and getxxx() Methods CHAR, VARCHAR, LONGVARCHARStringgetString() NUMERIC, DECIMALjava.math.BigDecimalgetBigDecimal() BITbooleangetBoolean() TINYINTbytegetByte() SMALLINTshortgetShort() INTEGERintgetInt() BIGINTlonggetLong() REALfloatgetFloat() FLOAT, DOUBLEdoublegetDouble() BINARY, VARBINARY, LONGVARBINARYbyte[]getBytes() DATEjava.sql.DategetDate() TIMEjava.sql.TimegetTime() TIMESTAMPjava.sql.TimestampgetTimestamp()

7 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, page 7 A Bigger Example Show program listing 20.2, pp. 629-634. Database getAll() updateName(thePerson: Person) registerNewPerson(firstName: String, lastName: String) deletePerson(identNo: int) closeTheConnection()

8 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, page 8 A Database Application Show program listing 20.3, pp. 635-639.

9 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, page 9 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.

10 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, page 10 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 setAutoCommit(boolean autocommit) // default is true –public void commit() –public void rollback() Solve problem 1, page 645.

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, page 11 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 firstName like ’ANNE’ and lastName like ’GREEN’; –select * from person where firstName like ’JOHN’ and lastName like ’JOHNSON’; The PreparedStatement interface let us create precompiled statement objects. An example: String sqlStatement = "select * from person where firstName like ? and lastName like ?"; PreparedStatement statement = conn.prepareStatement(sqlStatement); setning.setString(1, ”ANNE”); // The search criteria may be input, setning.setString(2, ”GREEN”); // see program listing 20.4 pp. 644-645. ResultSet res = statement.executeQuery(); while (res.next()) { …osv….


Download ppt "Java the UML Way versjon 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and."

Similar presentations


Ads by Google