CS 160: Software Engineering October 6 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
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.");
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.
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
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 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
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
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;
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 Data structures Java programming Compilers Software engineering Operating systems 109
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 Data structures Java programming Compilers Software engineering Operating 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);
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);
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, _addressVARCHAR(32)NOT NULL ); INSERT INTO contact_info (id, _address) VALUES(1, (2, (3, (4, (5, (6, (7, (8, (9,
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
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
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
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
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