Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 160: Software Engineering October 6 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Similar presentations


Presentation on theme: "CS 160: Software Engineering October 6 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak www.cs.sjsu.edu/~mak."— Presentation transcript:

1 CS 160: Software Engineering October 6 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak www.cs.sjsu.edu/~mak

2 Computer Science Dept. Fall 2014: October 6 CS 160: Software Engineering © R. Mak 2 JDBC Example: Database Record Insert  Method executeUpdate() returns the number of records that were inserted. String insert = "INSERT INTO teacher (id, last, first)" + "VALUES (7088, 'Mak', 'Ron'), " + "(7090, 'Wilson', 'Ron') "; System.out.print(" Insert: "); int rowCount = stmt.executeUpdate(insert); System.out.println(rowCount + " new rows.");

3 Computer Science Dept. Fall 2014: October 6 CS 160: Software Engineering © R. Mak 3 JDBC Example: Database Record Update String update = "UPDATE teacher" + "SET first = 'Ronald'" + "WHERE first = 'Ron'"; System.out.print(" Update: "); rowCount = stmt.executeUpdate(update); System.out.println(rowCount + " rows changed.");  Method executeUpdate() returns the number of records that were changed.

4 Computer Science Dept. Fall 2014: October 6 CS 160: Software Engineering © R. Mak 4 JDBC Example: Database Record Update  In any WHERE clause, LIKE allows wild cards in the string pattern. % matches zero or more characters.  'Ron%' matches 'Ron' and 'Ronald’ _ matches any single character.  '_onald' matches 'Ronald' and 'Donald' String delete = "DELETE FROM teacher" + "WHERE first LIKE 'Ron%'"; System.out.print(" Delete: "); rowCount = stmt.executeUpdate(delete); System.out.println(rowCount + " rows removed."); Demo

5 Computer Science Dept. Fall 2014: October 6 CS 160: Software Engineering © R. Mak 5 MySQL Workbench  Open-source version of some very expensive commercial database design and management tools (such as ERWin Data Modeler). Download from http://dev.mysql.com/downloads/http://dev.mysql.com/downloads/  Features Manage databases and database connections Edit, execute, and save SQL scripts Forward- and reverse-engineering  Generate a crow’s feet ER diagram from an existing database  Manually create an ER diagram  Automatically generate a database from the diagram

6 Computer Science Dept. Fall 2014: October 6 CS 160: Software Engineering © R. Mak MySQL Workbench: Table Contents  Demo: View the contents of tables. The School2 database is a copy of the School database. _ 6

7 Computer Science Dept. Fall 2014: October 6 CS 160: Software Engineering © R. Mak SQL to Create and Drop a Database  Examples to create:  Examples to drop: 7 CREATE DATABASE school3; CREATE DATABASE IF NOT EXISTS school3; DROP DATABASE school3; DROP DATABASE IF EXISTS school3;

8 Computer Science Dept. Fall 2014: October 6 CS 160: Software Engineering © R. Mak CREATE TABLE class ( code INT PRIMARY KEY, teacher_id INT NOT NULL, subject VARCHAR(32) NOT NULL, room INT NOT NULL ); SQL to Create a Table  First we create a new database and connect to it:  Create the Class table: 8 CREATE DATABASE school3; USE school3; CodeTeacher_idSubjectRoom 9087008Data structures114 9267003Java programming 101 9317051Compilers222 9517012Software engineering 210 9747012Operating systems 109

9 Computer Science Dept. Fall 2014: October 6 CS 160: Software Engineering © R. Mak SQL to Add Rows  Add rows to the Class table: 9 CodeTeacher_idSubjectRoom 9087008Data structures114 9267003Java programming 101 9317051Compilers222 9517012Software engineering 210 9747012Operating systems 109 INSERT INTO class (code, teacher_id, subject, room) VALUES (908, 7008, 'Data structures', 114), (926, 7003, 'Java programming', 101), (931, 7051, 'Compilers', 222), (951, 7012, 'Software engineering', 210), (978, 7012, 'Operating systems', 109);

10 Computer Science Dept. Fall 2014: October 6 CS 160: Software Engineering © R. Mak SQL Script create_school.sql 10 DROP DATABASE IF EXISTS school3; CREATE DATABASE school3; USE school3; CREATE TABLE class ( codeINTPRIMARY KEY, teacher_id INT NOT NULL, subject VARCHAR(32)NOT NULL, room INT NOT NULL, ); INSERT INTO class (code, teacher_id, subject, room) VALUES(908, 7008, 'Data structures', 114), (926, 7003, 'Java programming', 101), (931, 7051, 'Compilers', 222), (951, 7012, 'Software engineering', 210), (978, 7012, 'Operating systems', 109);

11 Computer Science Dept. Fall 2014: October 6 CS 160: Software Engineering © R. Mak SQL Script create_school.sql, cont’d 11 CREATE TABLE contact_info ( idINTPRIMARY KEY, email_addressVARCHAR(32)NOT NULL ); INSERT INTO contact_info (id, email_address) VALUES(1, 'mjane@sjsu.edu'), (2, 'ksmith@sjsu.edu'), (3, 'jdoe@sjsu.edu'), (4, 'tnovak@sjsu.edu'), (5, 'lklein@sjsu.edu'), (6, 'trogers@sjsu.edu'), (7, 'athompson@sjsu.edu'), (8, 'jlane@sjsu.edu'), (9, 'mflynn@sjsu.edu');

12 Computer Science Dept. Fall 2014: October 6 CS 160: Software Engineering © R. Mak SQL Script create_school.sql, cont’d 12 CREATE TABLE teacher ( idINTPRIMARY KEY, lastVARCHAR(32)NOT NULL, firstVARCHAR(32)NOT NULL, contact_idINTREFERENCES contact_info(id) ); INSERT INTO teacher (id, last, first, contact_id) VALUES(7003, 'Rogers','Tom',6), (7008, 'Thompson','Art',7), (7012, 'Lane','John’,8), (7051, 'Flynn','Mabel',9);  Use the MySQL source command: mysql> source create_school.sql

13 Computer Science Dept. Fall 2014: October 6 CS 160: Software Engineering © R. Mak MySQL Workbench: Table Contents  Demo: Add rows to tables.  Demo: Delete rows from tables. _ 13

14 Computer Science Dept. Fall 2014: October 6 CS 160: Software Engineering © R. Mak MySQL Workbench: EER Diagrams  MySQL Workbench creates Extended ER (EER) diagrams.  Demo: View an existing diagram Save the diagram as a PDF. _ 14

15 Computer Science Dept. Fall 2014: October 6 CS 160: Software Engineering © R. Mak MySQL Workbench: EER Diagrams, cont’d  MySQL Workbench can generate a new EER diagram by “reverse engineering” an existing database.  Demo: Generate a new EER diagram. _ 15

16 Computer Science Dept. Fall 2014: October 6 CS 160: Software Engineering © R. Mak MySQL Workbench: EER Diagrams, cont’d  Demo: Manually create a new EER diagram. Generate a new database from the EER diagram. _ 16


Download ppt "CS 160: Software Engineering October 6 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak www.cs.sjsu.edu/~mak."

Similar presentations


Ads by Google