Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2003 Prentice Hall, Inc. All rights reserved. 1 Java Database Connectivity with JDBC TM.

Similar presentations


Presentation on theme: " 2003 Prentice Hall, Inc. All rights reserved. 1 Java Database Connectivity with JDBC TM."— Presentation transcript:

1  2003 Prentice Hall, Inc. All rights reserved. 1 Java Database Connectivity with JDBC TM

2  2003 Prentice Hall, Inc. All rights reserved. 2 Introduction Database –Collection of data DBMS –Database management system –Stores and organizes data SQL –Relational database –Structured Query Language

3  2003 Prentice Hall, Inc. All rights reserved. 3 Introduction (Cont.) RDBMS –Relational database management system –Cloudscape 5.0.4 www.ibm.com/software/data/cloudscape JDBC –Java Database Connectivity –JDBC driver

4  2003 Prentice Hall, Inc. All rights reserved. 4 23.2 Relational-Database Model Relational database –Table Rows, columns –Primary key Unique data SQL statement –Query

5  2003 Prentice Hall, Inc. All rights reserved. 5 23.2 Relational-Database Model (Cont.) Fig. 23.1 Employee table sample data. NumberNameDepartmentSalaryLocation 23603Jones4131100New Jersey 24568Kerwin4132000New Jersey 34589Larson6421800Los Angeles 35761Myers6111400Orlando 47132Neumann4139000New Jersey 78321Stephens6118500Orlando Row ColumnPrimary key

6  2003 Prentice Hall, Inc. All rights reserved. 6 23.2 Relational-Database Model (Cont.) Fig. 23.2Result of selecting distinct Department and Location data from the Employee table. DepartmentLocation 413New Jersey 611Orlando 642Los Angeles

7  2003 Prentice Hall, Inc. All rights reserved. 7 23.3 Relational Database Overview: The books Database Sample books database –Four tables authors, publishers, authorISBN and titles –Relationships among the tables

8  2003 Prentice Hall, Inc. All rights reserved. 8 23.3 Relational Database Overview: The books Database (Cont.)

9  2003 Prentice Hall, Inc. All rights reserved. 9 23.3 Relational Database Overview: The books Database (Cont.) publisherID publisherName 1 Prentice Hall 2 Prentice Hall PTG Fig. 23.6 Data from the publishers table.

10  2003 Prentice Hall, Inc. All rights reserved. 10 23.3 Relational Database Overview: The books Database (Cont.)

11  2003 Prentice Hall, Inc. All rights reserved. 11 23.3 Relational Database Overview: The books Database (Cont.)

12  2003 Prentice Hall, Inc. All rights reserved. 12 23.3 Relational Database Overview: The books Database (Cont.) authorID isbn authorID isbn 1 0130895725 2 0139163050 2 0130895725 3 0130829293 2 0132261197 3 0130284173 2 0130895717 3 0130284181 2 0135289106 4 0130895601 Fig. 23.10 Sample data from the authorISBN table of books.

13  2003 Prentice Hall, Inc. All rights reserved. 13 23.3 Relational Database Overview: The books Database (Cont.) Fig. 23.11Table relationships in books. authorISBN authorID isbn authors authorID firstName lastName publishers publisherID publisherName titles isbn title editionNumber copyright publisherID imageFile price 1  1  1 

14  2003 Prentice Hall, Inc. All rights reserved. 14 23.4 SQL SQL overview SQL keywords SQL keyword Description SELECT Retrieves data from one or more tables. FROM Tables involved in the query. Required in every SELECT. WHERE Criteria for selection that determine the rows to be retrieved, deleted or updated. GROUP BY Criteria for grouping rows. ORDER BY Criteria for ordering rows. INNER JOIN Merge rows from multiple tables. INSERT Insert rows into a specified table. UPDATE Update rows in a specified table. DELETE Delete rows from a specified table. Fig. 23.12 SQL query keywords.

15  2003 Prentice Hall, Inc. All rights reserved. 15 23.4.1 Basic SELECT Query Simplest form of a SELECT query –SELECT * FROM tableName SELECT * FROM authors Select specific fields from a table –SELECT authorID, lastName FROM authors

16  2003 Prentice Hall, Inc. All rights reserved. 16 23.4.2 WHERE Clause specify the selection criteria –SELECT columnName1, columnName2, … FROM tableName WHERE criteria SELECT title, editionNumber, copyright FROM titles WHERE copyright > 2000 WHERE clause condition operators –, =, =, <> –LIKE wildcard characters % and _

17  2003 Prentice Hall, Inc. All rights reserved. 17 23.4.2 WHERE Clause (Cont.)

18  2003 Prentice Hall, Inc. All rights reserved. 18 23.4.2 WHERE Clause (Cont.) SELECT authorID, firstName, lastName FROM authors WHERE lastName LIKE ‘D%’

19  2003 Prentice Hall, Inc. All rights reserved. 19 23.4.2 WHERE Clause (Cont.) SELECT authorID, firstName, lastName FROM authors WHERE lastName LIKE ‘_i%’

20  2003 Prentice Hall, Inc. All rights reserved. 20 23.4.3 ORDER BY Clause Optional ORDER BY clause –SELECT columnName1, columnName2, … FROM tableName ORDER BY column ASC –SELECT columnName1, columnName2, … FROM tableName ORDER BY column DESC ORDER BY multiple fields –ORDER BY column1 sortingOrder, column2 sortingOrder, … Combine the WHERE and ORDER BY clauses

21  2003 Prentice Hall, Inc. All rights reserved. 21 23.4.3 ORDER BY Clause (Cont.) SELECT authorID, firstName, lastName FROM authors ORDER BY lastName ASC authorID firstName lastName 2 Paul Deitel 1 Harvey Deitel 3 Tem Nieto 4 Sean Santry Fig. 23.17 Sample data from table authors in ascending order by lastName.

22  2003 Prentice Hall, Inc. All rights reserved. 22 23.4.3 ORDER BY Clause (Cont.) SELECT authorID, firstName, lastName FROM authors ORDER BY lastName DESC authorID firstName lastName 4 Sean Santry 3 Tem Nieto 2 Paul Deitel 1 Harvey Deitel Fig. 23.18 Sample data from table authors in descending order by lastName.

23  2003 Prentice Hall, Inc. All rights reserved. 23 23.4.3 ORDER BY Clause (Cont.) SELECT authorID, firstName, lastName FROM authors ORDER BY lastName, firstName authorID firstName lastName 1 Harvey Deitel 2 Paul Deitel 3 Tem Nieto 4 Sean Santry Fig. 23.19 Sample author data from table authors in ascending order by lastName and by firstName.

24  2003 Prentice Hall, Inc. All rights reserved. 24 23.4.3 ORDER BY Clause (Cont.) SELECT isbn, title, editionNumber, copyright, price FROM titles WHERE title LIKE ‘%How to Program’ ORDER BY title ASC

25  2003 Prentice Hall, Inc. All rights reserved. 25 23.4.4 Merging Data from Multiple Tables: Joining Split related data into separate tables Join the tables –Merge data from multiple tables into a single view –INNER JOIN SELECT columnName1, columnName2, … FROM table1 INNER JOIN table2 ON table1.columnName = table2.column2Name SELECT firstName, lastName, isbn FROM authors, authorISBN INNER JOIN authorISBN ON authors.authorID = authorISBN.authorID ORDER BY lastName, firstName

26  2003 Prentice Hall, Inc. All rights reserved. 26 23.4.4 Merging Data from Multiple Tables: Joining (Cont.)

27  2003 Prentice Hall, Inc. All rights reserved. 27 23.4.5 INSERT Statement Insert a row into a table –INSERT INTO tableName ( columnName1, …, columnNameN ) VALUES ( value1, …, valueN ) INSERT INTO authors ( firstName, lastName ) VALUES ( ‘Sue’, ‘Smith’ ) authorID firstName lastName 1 Harvey Deitel 2 Paul Deitel 3 Tem Nieto 4 Sean Santry 5 Sue Smith Fig. 23.22 Sample data from table Authors after an INSERT operation.

28  2003 Prentice Hall, Inc. All rights reserved. 28 23.4.6 UPDATE Statement Modify data in a table –UPDATE tableName SET columnName1 = value1, …, columnNameN = valueN WHERE criteria UPDATE authors SET lastName = ‘Jones’ WHERE lastName = ‘Smith’ AND firstName = ‘Sue’ authorID firstName lastName 1 Harvey Deitel 2 Paul Deitel 3 Tem Nieto 4 Sean Santry 5 Sue Jones Fig. 23.23 Sample data from table authors after an UPDATE operation.

29  2003 Prentice Hall, Inc. All rights reserved. 29 23.4.7 DELETE Statement Remove data from a table –DELETE FROM tableName WHERE criteria DELETE FROM authors WHERE lastName = ‘Jones’ AND firstName = ‘Sue’ authorID firstName lastName 1 Harvey Deitel 2 Paul Deitel 3 Tem Nieto 4 Sean Santry Fig. 23.24 Sample data from table authors after a DELETE operation.

30  2003 Prentice Hall, Inc. All rights reserved. 30 23.6 Manipulating Databases with JDBC Connect to a database Query the database Display the results of the query

31  2003 Prentice Hall, Inc. All rights reserved. 31 23.6.1 Connecting to and Querying a JDBC Data Source (Cont.) Type Description 1 TheJDBC-to-ODBC bridge driver connects Java programs to Microsoft ODBC (Open Database Connectivity) data sources. The Java 2 Software Development Kit from Sun Microsystems, Inc. includes the JDBC-to-ODBC bridge driver ( sun.jdbc.odbc.JdbcOdbcDriver ). This driver typically requires the ODBC driver to be installed on the client computer and normally requires configuration of the ODBC data source. The bridge driver was introduced primarily for development purposes and should not be used for production applications. 2 Native-API, partly Java drivers enable JDBC programs to use database-specific APIs (normally written in C or C++) that allow client programs to access databases via the Java Native Interface. This driver type translates JDBC into database-specific code. Type 2 drivers were introduced for reasons similar to the Type 1 ODBC bridge driver. 3 JDBC-Net pure Java drivers take JDBC requests and translate them into a network protocol that is not database specific. These requests are sent to a server, which translates the database requests into a database-specific protocol. 4 Native-protocol pure Java drivers convert JDBC requests to database-specific network protocols, so that Java programs can connect directly to a database. Fig. 23.26 JDBC driver types.

32  2003 Prentice Hall, Inc. All rights reserved. 32 JDBC-ODBC Bridge( 補充 ) From JNetDirect (http://www.jnetdirect.com)http://www.jnetdirect.com Download JNetDirect driver from http://mail.im.tku.edu.tw/~cjou/java/index2.html http://mail.im.tku.edu.tw/~cjou/java/index2.html 執行該安裝檔案

33  2003 Prentice Hall, Inc. All rights reserved. 33 ODBC Data Source Name Setup 控制台 -> 系統管理工具 -> 資料來源 (ODBC) 捷徑 按 “ 系統資料來源名稱 ” Tab 按 “ 新增 ”

34  2003 Prentice Hall, Inc. All rights reserved. 34 ODBC Data Source Name Setup 選擇 Microsoft Access Driver (*.mdb) 按 “ 完成 ”

35  2003 Prentice Hall, Inc. All rights reserved. 35 ODBC Data Source Name Setup 設定 “ 資料來源名稱 ” 為 MyDSN 按 “ 選擇 ” 去選取 Access 資料庫檔案 按 “ 進階 ”

36  2003 Prentice Hall, Inc. All rights reserved. 36 ODBC Data Source Name Setup 輸入 “ 登入名稱 ” 及 “ 密碼 ” 按 “ 確定 ” ( 三次 )

37  2003 Prentice Hall, Inc. All rights reserved. 37 範例程式 copy JData2_0.jar from C:\Program Files\JNetDirect\JDataConnect\JARS\ to C:\Program Files\JNetDirect\JDataConnect\Examples javac ExampleApplication.java java -classpath.;./JData2_0.jar ExampleApplication

38  2003 Prentice Hall, Inc. All rights reserved. 38

39  2003 Prentice Hall, Inc. All rights reserved. 39

40  2003 Prentice Hall, Inc. All rights reserved. 40


Download ppt " 2003 Prentice Hall, Inc. All rights reserved. 1 Java Database Connectivity with JDBC TM."

Similar presentations


Ads by Google