Your SlideShare is downloading. ×
0
Download
Download
Download
Download
Download
Download
Download
Download
Download
Download
Download
Download
Download
Download
Download
Download
Download
Download
Download
Download
Download
Download
Download
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Download

208

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
208
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. PROJECT OVERVIEW Hyun J. Moon and Carlo Zaniolo University of California, Los Angeles CS143, Fall 2004
  • 2. Projects <ul><li>Two Projects </li></ul><ul><li>Project1 </li></ul><ul><ul><li>Defining and Querying Databases in SQL Using IBM DB2 </li></ul></ul><ul><ul><li>Techniques: DB2, JDBC (Java Database Connectivity) </li></ul></ul><ul><li>Project2 </li></ul><ul><ul><li>Web-based Online Access of Databases: online access/update of a movie database </li></ul></ul><ul><ul><li>Techniques: DB2, JDBC, HTML, Java Servlet </li></ul></ul><ul><li>Hot topics! Good for job hunting! </li></ul><ul><li>Start early, really early! </li></ul>
  • 3. Project SEASNET Account <ul><li>Each student is assigned a seas account for the project (not your personal seas account) </li></ul><ul><ul><li>Pick it up in the break of discussion or office hours </li></ul></ul><ul><ul><li>The login/passwd will be explicitly used in your project </li></ul></ul><ul><ul><ul><li>Change your passwd to anything except your private passwd </li></ul></ul></ul><ul><ul><ul><li>Change your home directory permissions:chmod go-rxw dir </li></ul></ul></ul>
  • 4. Project SEASNET Account (cont’d) <ul><li>3 machines to login </li></ul><ul><ul><ul><li>landfair .seas.ucla.edu </li></ul></ul></ul><ul><ul><ul><li>lindbrook .seas.ucla.edu </li></ul></ul></ul><ul><ul><ul><li>westholme .seas.ucla.edu </li></ul></ul></ul><ul><li>To decide which machine you need to login: LAST_2_DIGITS_OF_YOUR_STUDENT_ID mode 3 </li></ul>studb w studb n studb l Database name westholme lindbrook landfair Machine name 2 1 0 mod value
  • 5. Project DBMS: IBM DB2 <ul><li>IBM DB2 (also Universal Database Server ) </li></ul><ul><li>Why DB2 </li></ul><ul><ul><li>Garner 2002 on RDBMS market share (new licenses) </li></ul></ul><ul><ul><ul><li>IBM 34.6% ORCL 32.0% MSFT: 16.3% SYBASE: 2.6% </li></ul></ul></ul><ul><ul><li>DB2 is cheap: at least 4 times cheaper than Oracle </li></ul></ul><ul><ul><li>DB2 is the standard </li></ul></ul><ul><ul><li>DB2 is free for education/research use </li></ul></ul><ul><ul><li>DB2 is small and simple </li></ul></ul>
  • 6. Setup DB2 Environments <ul><li>DB2 environments like path for db2 libraries, executable files, DB2 Java CLASSPATH </li></ul><ul><li>csh/tcsh: </li></ul><ul><ul><li>source /u/cs/class/cs143v/cs143vta/sqllib/db2cshrc </li></ul></ul><ul><ul><li>or put it into .cshrc to run automatically after login </li></ul></ul><ul><li>bash: </li></ul><ul><ul><li>source /u/cs/class/cs143v/cs143vta/sqllib/db2profile </li></ul></ul><ul><ul><li>or put it into .bashrc to run automatically after login </li></ul></ul>
  • 7. Test DB2 <ul><li>Set your db2 environments first </li></ul><ul><li>connect to a database: </li></ul><ul><ul><li>$ db2 connect to sample1 user yourlogin </li></ul></ul><ul><ul><li>type your passwd </li></ul></ul><ul><ul><li>$ db2 “select * from cs143vta.staff” </li></ul></ul><ul><ul><li>Other sample tables: </li></ul></ul><ul><ul><ul><li>album, cl_sched, department, emp_act, emp_photo, emp_resume, employee, in_tray, org, project, sales, staff </li></ul></ul></ul><ul><li>Get help on DB2: </li></ul><ul><ul><li>http://www.seas.ucla.edu/db2/ </li></ul></ul>
  • 8. Use DB2 <ul><li>Many ways: </li></ul><ul><ul><li>command line </li></ul></ul><ul><ul><li>From applications: embedded SQL, JDBC, Stored Procedures, etc. </li></ul></ul><ul><li>Command line: </li></ul><ul><ul><li>DB2 shell: $db2; then input SQL commands dynamically </li></ul></ul><ul><ul><li>Through OS command line: $db2 SQL command </li></ul></ul><ul><ul><li>SQL script files: put SQL scripts in a script file, and run as: </li></ul></ul><ul><ul><ul><li>$db2 –tf &lt;filename&gt; </li></ul></ul></ul>
  • 9. Use DB2: DB2 Command Line <ul><li>DB2 command line: enter $ db2 &lt;enter&gt; </li></ul><ul><ul><li>You can enter SQL commands + DB2 system commands </li></ul></ul><ul><ul><li>enter ? for help, or ? cmdname for a specific command </li></ul></ul><ul><ul><li>commonly used: (no semicolon at the end please) </li></ul></ul><ul><ul><ul><li>connect to dbname (connect to database dbname) </li></ul></ul></ul><ul><ul><ul><li>list tables (list tables of your schema) </li></ul></ul></ul><ul><ul><ul><li>list database directory (list all database directory) </li></ul></ul></ul><ul><ul><ul><li>connect reset (drop your connection to the database) </li></ul></ul></ul><ul><ul><ul><li>terminate (terminate current session and exit db2 interactive mode. Always leave your session this way!) </li></ul></ul></ul><ul><ul><ul><li>echo abc (make comments to your codes) </li></ul></ul></ul>
  • 10. Use DB2 (cont’d) <ul><li>Similar as db2 command line except beginning with “db2” </li></ul><ul><ul><ul><li>$ db2 connect to dbname </li></ul></ul></ul><ul><ul><ul><li>$ db2 “select * from cs143vta.staff” (SQL needs to be quoted on UNIX) </li></ul></ul></ul><ul><li>SQL script file </li></ul><ul><ul><li>put SQL scripts to a file query.sql seperated by semicolons: </li></ul></ul><ul><ul><ul><li>connect to studbn user xxx using yyy; </li></ul></ul></ul><ul><ul><ul><li>echo test sql script; </li></ul></ul></ul><ul><ul><ul><li>create table emp(empno int, name varchar(32) ); </li></ul></ul></ul><ul><ul><ul><li>insert into emp values(10, ‘Joe Doe’); </li></ul></ul></ul><ul><ul><ul><li>select * from emp; </li></ul></ul></ul><ul><ul><ul><li>connect reset </li></ul></ul></ul><ul><ul><ul><li>terminate; </li></ul></ul></ul><ul><ul><li>Run as: $db2 –tf query.sql </li></ul></ul>
  • 11. Connect to DB2 <ul><li>DB2 has 1 or more instances: cs143vta </li></ul><ul><li>Each instance can have databases </li></ul><ul><ul><ul><li>3 project databases: studbl, studbn, studbw </li></ul></ul></ul><ul><li>To access a database, the user must have the privilege to connect to that database </li></ul><ul><ul><ul><li>For DB2, a user to a database must also be a user to the OS </li></ul></ul></ul><ul><li>Connect to a database named sample1: </li></ul><ul><ul><ul><li>connect to sample1 [user userid ] [using passwd ] </li></ul></ul></ul><ul><ul><ul><li>If locally: you can ignore userid/passed </li></ul></ul></ul><ul><ul><ul><li>In our project, you will connect remotely </li></ul></ul></ul><ul><li>Always run “terminate” after you finish </li></ul>
  • 12. Schema <ul><li>Schema: a named collection of objects such as tables. Such objects have to be unique inside the schema </li></ul><ul><li>When you create an object (e.g., a table), a default schema name is automatically created </li></ul><ul><ul><ul><li>e.g., create table test(a int); a schema named your login, e.g., cs143vxy is automatically created. </li></ul></ul></ul><ul><ul><ul><li>You can refer to the table as cs143vxy.test, or test (schema cs143vxy, your loginname is implied) </li></ul></ul></ul>
  • 13. A Sample Table <ul><li>A sample employee relation: </li></ul><ul><ul><li>employee(id, firstname, lastname, gender, hiredate, deptno, title, salary) </li></ul></ul>connect to studbn user xxx passwd yyy; drop table employee; create table employee ( id char(8), firstname varchar(24), lastname varchar(24), sex char(1), hiredate date, deptno char(8), title varchar(24) not null, salary integer); insert into employee values(‘10001’, ‘Joe’, ‘Doe’, ‘F’, ’10/02/2003’, ‘d01’, ‘Engineer’, ‘50000’); Run as: $db2 –tf query.sql
  • 14. DB2 Built-in Data Types <ul><li>Date/Time: </li></ul><ul><ul><ul><li>Date(10/02/2003), Time(17:12:25), Timestamp(2003-10-02-17.12.32.963078) </li></ul></ul></ul><ul><li>String: </li></ul><ul><ul><ul><li>char (fixed length), varchar(variable length), clob( character LOB), blob(binary LOB). e.g., char(4): n &lt;254. varchar(32): n&lt;4000 clob(2M) </li></ul></ul></ul><ul><li>Numeric: </li></ul><ul><ul><ul><li>REAL, DOUBLE </li></ul></ul></ul><ul><ul><ul><li>SMALLINT(16bits), INTEGER(32bits), BIGINT(64bits), DECIMAL. e.g., DECIMAL(5,2): 5: scale, 2 is the # of digits after the decimal point </li></ul></ul></ul><ul><li>Maximum length in a row: 4005 for 4KBytes pages </li></ul>
  • 15. Load Data <ul><li>Import example: </li></ul><ul><li>Normally data files can be: </li></ul><ul><ul><ul><li>ASC (non-delimited ASCII format) </li></ul></ul></ul><ul><ul><ul><li>DEL (delimited ASCII format, default delimiter: ,) </li></ul></ul></ul><ul><ul><ul><li>IXF (integrated exchange format) </li></ul></ul></ul><ul><li>Refresh data: </li></ul><ul><ul><li>I. drop tables + create tables + load data </li></ul></ul><ul><ul><li>II. delete from emp + load data </li></ul></ul>IMPORT FROM datafile OF DEL RESTARTCOUNT 100 MESSAGES import.msg insert INTO employee;
  • 16. DB2 Setup <ul><li>You can work on your project on your own DB2 and copy it over to seas account, but make sure it works! </li></ul><ul><li>DB2 download: </li></ul><ul><ul><ul><li>http://www.ibm.com/db2 , download </li></ul></ul></ul><ul><li>For windows, when you setup, you need to create an administration account, e.g., db2admin </li></ul><ul><li>To start up db2, run with an Administrator account: </li></ul><ul><ul><ul><li>c:&gt;db2cmd (setup db2 environment) </li></ul></ul></ul><ul><ul><ul><li>c:&gt;db2start (start up db2 instance) </li></ul></ul></ul><ul><ul><ul><li>c:&gt;db2jstrt (start up java connection) </li></ul></ul></ul><ul><ul><ul><li>c:&gt;db2admin start (startup admin server so you can run DB2 control center) </li></ul></ul></ul><ul><ul><ul><li>c:&gt;db2cc (Control Center, a GUI for administration) </li></ul></ul></ul><ul><ul><ul><li>c:&gt;db2stop (stop db2) </li></ul></ul></ul><ul><ul><ul><li>c:&gt;db2admin stop (stop db2 admin server) </li></ul></ul></ul>
  • 17. Project 1 <ul><li>Defining and Querying Databases in SQL Using IBM DB2 Due: Noon Tuesday, Nov 2 </li></ul><ul><li>Tasks </li></ul><ul><ul><li>Define and create tables </li></ul></ul><ul><ul><li>Load data into the tables </li></ul></ul><ul><ul><li>Write queries as SQL scripts </li></ul></ul><ul><ul><li>Write queries with JDBC </li></ul></ul><ul><li>Start early! </li></ul>
  • 18. JDBC <ul><li>JDBC (Java Database Connectivity) API: a standard SQL database access interface </li></ul><ul><ul><li>same API for all databases </li></ul></ul><ul><li>Environment (already setup if you set db2 env) </li></ul><ul><ul><li>Java and javac </li></ul></ul><ul><ul><li>db2java.zip (jdbc lib) </li></ul></ul><ul><li>Sample files: </li></ul><ul><ul><li>/w/class.01/cs/cs143v/cs143vta/sample-code </li></ul></ul>
  • 19. Establishing a Connection <ul><li>Load the vendor specific driver: </li></ul><ul><ul><ul><li>Class.forName(&amp;quot;COM.ibm.db2.jdbc. app .DB2Driver&amp;quot;) (on local) </li></ul></ul></ul><ul><ul><ul><li>Class.forName(&amp;quot;COM.ibm.db2.jdbc. net .DB2Driver&amp;quot;) (remote) </li></ul></ul></ul><ul><li>Make the connection </li></ul><ul><ul><li>connection url: e.g., jdbc:db2://national.seas.ucla.edu/studbn </li></ul></ul><ul><ul><li>username, passwd </li></ul></ul><ul><ul><li>Connection con = DriverManager.getConnection(url, userid, passwd); </li></ul></ul>
  • 20. Creating JDBC Statements and Send Queries <ul><li>A JDBC Statement object is used to send your SQL statements to the DBMS </li></ul><ul><ul><li>Statement stmt = con.createStatement(); </li></ul></ul><ul><li>Execute the query: </li></ul><ul><ul><li>For select queries: </li></ul></ul><ul><ul><ul><li>ResultSet rs = stmt. executeQuery (&amp;quot;SELECT * from employee&amp;quot;); </li></ul></ul></ul><ul><ul><li>For update/insert/delete/or DDL queries: </li></ul></ul><ul><ul><ul><li>ResultSet rs = stmt. executeUpdate (“delete from employee where empno=‘1’ &amp;quot;); </li></ul></ul></ul>
  • 21. Accessing ResultSet <ul><li>rs.next() returns false when there are no more rows </li></ul><ul><ul><ul><li>while (rs.next()) { </li></ul></ul></ul><ul><ul><ul><li>String a = rs.getString(1); </li></ul></ul></ul><ul><ul><ul><li>String str = rs.getString(2); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><li>Help: http://java.sun.com/j2se/1.4.2/docs/api/ </li></ul>
  • 22. accessDatabase.java
  • 23. Academic Honesty Policy <ul><li>You are expected to submit your own original work </li></ul><ul><li>It is encouraged to discuss the project with peer students, provided that you indicate their names in your submission. </li></ul><ul><li>All code must be written up independently. </li></ul><ul><li>Any suspected academic dishonesty will be reported to the Dean of Students for disciplinary action, which may result in suspension or dismissal from the University. </li></ul>

×