ERwin and JDBC Mar. 6, 2007 Myoung Ho Kim
ERwin ERwin a popular commercial ER modeling tool» other tools: Dia (open source), Visio, ConceptDraw, etc. supports database schema generation 2
ERwin UI 3
Data Models in ERwin Logical Model allows conceptual modeling» simple data types (integer, string, date, etc.)» subclass support Physical Model needed for database schema generation a dedicated model for a particular database» specific data types of target database» subclass needs expanding 4
Modeling Procedure in ERwin Logical Model Physical Model Database Schema ERwin supports round-trip engineering Physical Model Database Schema We will practice this part in this HW 5
Example: Modeling and Schema Generation Create an ER model Select File > New > Create Model 6
Example: Modeling and Schema Generation Create an ER model (cont d) Entities (tables) and relationships Initial design Type setting 7
Example: Modeling and Schema Generation Create an ER model (cont d) Entities (tables) and relationships double click Initial design Type setting 8
Example: Modeling and Schema Generation Schema Generation Schema generation tools forward engineering 9
Example: Modeling and Schema Generation Schema Generation Schema generation tools forward engineering 10
Example: Modeling and Schema Generation Schema Generation Schema generation tools forward engineering 11
Example: Modeling and Schema Generation Schema Generation Schema generation tools forward engineering turn off Click 12
Example: Modeling and Schema Generation Schema Generation (cont d) 13
Example: Modeling and Schema Generation Schema Generation (cont d) Connection string is the net service name you configured with Oracle Net Configuration Assistant program (more on this later) 14
JDBC JDBC (Java Database Connectivity)» Java interfaces to interact with various databases applications use JDBC API to query databases database vendors implement drivers for their databases JDBC SQL statement ResultSet JDBC API JDBC Driver DBMS Applications 15
Example import java.sql.*; class Test { public static void main(string[] args) { Connection con = null; Statement stmt = null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); con = DriverManager.getConnection( "jdbc:oracle:thin:@dbdynasty.kaist.ac.kr:1521:orcl", username", "password"); stmt = con.createstatement(); } ResultSet rs = stmt.executequery("select * from product"); while (rs.next()) { String product = rs.getstring( name ); System.out.println(product); } } catch (Exception e) { e.printstacktrace(); } 16
JDBC API Loading a JDBC driver use Class.forName() Class.forName( oracle.jdbc.driver.oracledriver ); Connecting to DB use DriverManager.getConnection() to get a Connection instance Connection con = DriverManager.getConnection( "jdbc:oracle:thin:@dbdynasty.kaist.ac.kr:1521:orcl", "username", "password"); orcl is SID of the Oracle database ( 서비스이름 ) 17
JDBC API (cont d) Executing queries Statement interface Statement stmt = con.createstatement(); ResultSet rs = stmt.executequery("select name from product"); PreparedStatement interface PreparedStatement pstmt = con.preparestatement( insert into product values(?,?) ); pstmt.setstring(1, mp3 ); pstmt.setint(2, 150); pstmt.executeupdate(); SELECT: executequery() INSERT, UPDATE, DELETE: executeupdate() or execute() 18
JDBC API (cont d) Retrieving query results ResultSet interface» next(), a series of get[type]() methods, etc. ResultSet rs = stmt.executequery("select * from product"); while (rs.next()) { String product = rs.getstring( name ); System.out.println(product); } Index-based retrieval (starting from 1): getint(int), getstring(int), Attribute-based retrieval: getint(string), getstring(string), 19
Server Information Server (Linux machine) domain name: dbdynasty.kaist.ac.kr your account (for login to the server)» userid: s[student_number], password: student_number ex) s20061234/20061234 Oracle database connection info.» port: 1521» SID: orcl your account (for login to the Oracle database)» the same as above 20
Required Software Oracle Client» includes JDBC driver and other useful programs such as SQL*Plus Download» download 10g_win32_client.zip from the following site Installation http://www.oracle.com/technology/software/products/database/oracle10» Unzip the downloaded file and run setup.exe be careful not to include any Korean characters in the path of your installation directory» Configure the net service name with Oracle Net Configuration Assistant (the name is used as connection string in schema generation in ERwin) SID ( 서비스이름 ) : orcl You must install the Oracle Client to use ERwin s schema generation feature 21
References ERwin 30-day evaluation version» http://dbserver.kaist.ac.kr/~mhkim/cs560-07spring.dir/erwin414sp2-b4033.exe ERwin Tutorial» Run ERwin, choose Help menu, and then select Tutorial JDBC Oracle JDBC driver» http://dbserver.kaist.ac.kr/~mhkim/cs560-07spring.dir/ojdbc14.jar» or you can find one in the Oracle Client installation directory JDBC Tutorial» http://java.sun.com/docs/books/tutorial/jdbc/ Advanced Tutorial» http://java.sun.com/developer/books/jdbctutorial/index.html 22