作業一 Using DBMS ( 使用 DB2) C. J. Date 課本習題 4.1~4.6 Ex. 4.5, 4.6...

19
作作Using DBMS ( 作作 DB2) C. J. Date 課課課課 4.1~4.6 Ex. 4.5, 4.6 課課課課課課課 Embedded SQL 課課 DB2. EX. 4.1-4.6 (p.99-100) Using the suppliers-parts-projects database, write a program with embedded SQL statements to list all supplier rows, in supplier number order. Each supplier row should be immediately followed in the listing by all project rows for projects supplied by that supplier, in project number order. create database selection update query catalog . . . embedded SQL (program)

Transcript of 作業一 Using DBMS ( 使用 DB2) C. J. Date 課本習題 4.1~4.6 Ex. 4.5, 4.6...

Page 1: 作業一 Using DBMS ( 使用 DB2) C. J. Date 課本習題 4.1~4.6 Ex. 4.5, 4.6 程式作業要使用 Embedded SQL 連結 DB2. EX. 4.1-4.6 (p.99-100) Using the suppliers-parts-projects

作業一 Using DBMS ( 使用 DB2)

C. J. Date 課本習題 4.1~4.6 Ex. 4.5, 4.6 程式作業要使用 Embedded SQL 連結 DB2. EX. 4.1-4.6 (p.99-100)

Using the suppliers-parts-projects database, write a program with embedded SQL statements to list all supplier rows, in supplier number order. Each supplier row should be immediately followed in the listing by all project rows for projects supplied by that supplier, in project number order.

create database selection update query catalog . . . embedded SQL (program)

Page 2: 作業一 Using DBMS ( 使用 DB2) C. J. Date 課本習題 4.1~4.6 Ex. 4.5, 4.6 程式作業要使用 Embedded SQL 連結 DB2. EX. 4.1-4.6 (p.99-100) Using the suppliers-parts-projects
Page 3: 作業一 Using DBMS ( 使用 DB2) C. J. Date 課本習題 4.1~4.6 Ex. 4.5, 4.6 程式作業要使用 Embedded SQL 連結 DB2. EX. 4.1-4.6 (p.99-100) Using the suppliers-parts-projects
Page 4: 作業一 Using DBMS ( 使用 DB2) C. J. Date 課本習題 4.1~4.6 Ex. 4.5, 4.6 程式作業要使用 Embedded SQL 連結 DB2. EX. 4.1-4.6 (p.99-100) Using the suppliers-parts-projects
Page 5: 作業一 Using DBMS ( 使用 DB2) C. J. Date 課本習題 4.1~4.6 Ex. 4.5, 4.6 程式作業要使用 Embedded SQL 連結 DB2. EX. 4.1-4.6 (p.99-100) Using the suppliers-parts-projects

DB2 安裝使用

先安裝 J2SDK J2sdk-1_4_2.exe 再安裝 DB2 Universal Database Personal

Edition 下戴處 IBM web side

課程網頁上為 windows 版本 RELEASED PRODUCT:   DB2 Universal Database (DB2 Personal Edition for Wi

ndows (32 Bit))

FREE download of DB2 Universal Database products.

Page 6: 作業一 Using DBMS ( 使用 DB2) C. J. Date 課本習題 4.1~4.6 Ex. 4.5, 4.6 程式作業要使用 Embedded SQL 連結 DB2. EX. 4.1-4.6 (p.99-100) Using the suppliers-parts-projects

DB2 控制中心

Page 7: 作業一 Using DBMS ( 使用 DB2) C. J. Date 課本習題 4.1~4.6 Ex. 4.5, 4.6 程式作業要使用 Embedded SQL 連結 DB2. EX. 4.1-4.6 (p.99-100) Using the suppliers-parts-projects
Page 8: 作業一 Using DBMS ( 使用 DB2) C. J. Date 課本習題 4.1~4.6 Ex. 4.5, 4.6 程式作業要使用 Embedded SQL 連結 DB2. EX. 4.1-4.6 (p.99-100) Using the suppliers-parts-projects
Page 9: 作業一 Using DBMS ( 使用 DB2) C. J. Date 課本習題 4.1~4.6 Ex. 4.5, 4.6 程式作業要使用 Embedded SQL 連結 DB2. EX. 4.1-4.6 (p.99-100) Using the suppliers-parts-projects
Page 10: 作業一 Using DBMS ( 使用 DB2) C. J. Date 課本習題 4.1~4.6 Ex. 4.5, 4.6 程式作業要使用 Embedded SQL 連結 DB2. EX. 4.1-4.6 (p.99-100) Using the suppliers-parts-projects
Page 11: 作業一 Using DBMS ( 使用 DB2) C. J. Date 課本習題 4.1~4.6 Ex. 4.5, 4.6 程式作業要使用 Embedded SQL 連結 DB2. EX. 4.1-4.6 (p.99-100) Using the suppliers-parts-projects

列出結果

Page 12: 作業一 Using DBMS ( 使用 DB2) C. J. Date 課本習題 4.1~4.6 Ex. 4.5, 4.6 程式作業要使用 Embedded SQL 連結 DB2. EX. 4.1-4.6 (p.99-100) Using the suppliers-parts-projects

Java 連結 DB2 範例

java language & DB2

java

int a, b;a = 5;b =10*a;….EXEC SQL …..…..

SQL:

Page 13: 作業一 Using DBMS ( 使用 DB2) C. J. Date 課本習題 4.1~4.6 Ex. 4.5, 4.6 程式作業要使用 Embedded SQL 連結 DB2. EX. 4.1-4.6 (p.99-100) Using the suppliers-parts-projects

設定 DB2 連結資訊環境

Page 14: 作業一 Using DBMS ( 使用 DB2) C. J. Date 課本習題 4.1~4.6 Ex. 4.5, 4.6 程式作業要使用 Embedded SQL 連結 DB2. EX. 4.1-4.6 (p.99-100) Using the suppliers-parts-projects

設定 DB2 連結資訊環境

Page 15: 作業一 Using DBMS ( 使用 DB2) C. J. Date 課本習題 4.1~4.6 Ex. 4.5, 4.6 程式作業要使用 Embedded SQL 連結 DB2. EX. 4.1-4.6 (p.99-100) Using the suppliers-parts-projects

Java 環境設定

我的電腦 ( 按右鍵 )-> 內容 :

C:\j2sdk1.4.2_01\lib\tools.jar

Page 16: 作業一 Using DBMS ( 使用 DB2) C. J. Date 課本習題 4.1~4.6 Ex. 4.5, 4.6 程式作業要使用 Embedded SQL 連結 DB2. EX. 4.1-4.6 (p.99-100) Using the suppliers-parts-projects

Java 環境設定

;C:\j2sdk1.4.2_01\bin;

Page 17: 作業一 Using DBMS ( 使用 DB2) C. J. Date 課本習題 4.1~4.6 Ex. 4.5, 4.6 程式作業要使用 Embedded SQL 連結 DB2. EX. 4.1-4.6 (p.99-100) Using the suppliers-parts-projects

使用 Javac 編譯程式

C:\work\dbtest.java : 使用者編輯的程式碼

Page 18: 作業一 Using DBMS ( 使用 DB2) C. J. Date 課本習題 4.1~4.6 Ex. 4.5, 4.6 程式作業要使用 Embedded SQL 連結 DB2. EX. 4.1-4.6 (p.99-100) Using the suppliers-parts-projects

Embedded SQL Example

import java.sql.*; import java.lang.*; class dbtest { public static void main(String[] argv) throws Exception { int sum=0; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection ("jdbc:odbc:TEST"); Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); System.out.println(" 連上了~ "); String strSQL="SELECT * FROM DB147.S as S where CITY='london'"; System.out.println(strSQL); ResultSet rs=stmt.executeQuery(strSQL); sum=Integer.parseInt(argv[0]); if(sum>10){ System.out.print("sum > 10; "+"sum*0.5="+sum*0.5+"\n"); } else { System.out.print("sum <= 10; "+"sum+100="+(sum+100)+"\n"); } while(rs.next()){ sum=sum+rs.getInt("STATUS"); System.out.print(" "+rs.getString(1)+" "+rs.getInt("STATUS") + "\n"); } System.out.print("total status = $ "+sum + "\n"); } }

rs.next()

Next()

CITY='london’

Page 19: 作業一 Using DBMS ( 使用 DB2) C. J. Date 課本習題 4.1~4.6 Ex. 4.5, 4.6 程式作業要使用 Embedded SQL 連結 DB2. EX. 4.1-4.6 (p.99-100) Using the suppliers-parts-projects

java 語言範例 結果