310 likes | 484 Views
Advanced Java Session 5. New York University School of Continuing and Professional Studies. Objectives. Java Beans – Part 2 ImageViewerBean Relational Database concepts Schema, tables, fields, rows Common SQL queries – SELECT, INSERT, UPDATE JDBC design Basic JDBC Programming Concepts
E N D
Advanced JavaSession 5 New York University School of Continuing and Professional Studies
Objectives • Java Beans – Part 2 • ImageViewerBean • Relational Database concepts • Schema, tables, fields, rows • Common SQL queries – SELECT, INSERT, UPDATE • JDBC • design • Basic JDBC Programming Concepts • Populating database/Executing Queries • Java Servlets • Overview and Basic Servlet structure
BeanBox • Installing and launching • Using BeanBox to create applications • Creating JavaBean jar files • jar cvfm filename.Jar manifestfile classes
Adding Custom Bean Events • Write a class <eventName>Event that extends EventObject • Write an interface <eventname>Listener with a single notification method with any name that takes a single parameter <eventname>Event and return type of “void” • Supply the following two methods in the bean – • public void add<eventname>Listener(<eventname>Event e) • public void remove<eventname>Listener(<eventname>Event e)
TimerBean.java • Example of adding custom bean events
Database Systems • Relational Databases – organize data in the form of tables - Oracle • Object Databases – organize data as objects - ObjectDB • Hierarchical Databases – organize data in a hierarchical format - WWW • Network Database – organize data as a network of nodes - dbVista
Relational Databases • Tables • Fields • Key • Index
Tables and Relations StudentsXCourses Students RefID StudentID CourseID StudentID Name 1001 Robert Estes Robert Estes 1001 Robert Estes 1002 Mitzy Capture Mitzy Capture 1002 Mitzy Capture EmailAddresses Courses EmailAdrId StudentID EmailAddr CourseID Course 1 1001 Restes@oncr.com 101 Java2 2 1002 Mcapture@oncr.com 102 Networking 3 1001 restes@hotmail.com
Common Operations • SELECT • Simple SELECT • Join of multiple tables • UPDATE • INSERT
JDBC Design Java Application JDBC API JDBC Driver Manager JDBC Driver API JDBC/ODBC Bridge Vendor- Supplied JDBC driver ODBC db db
Driver Classification • Type 1 • JDBC/ODBC bridge • Type 2 • Partly in Java/Partly in native code • Type 3 • Pure Java - through a middle tier • Type 4 • Pure Java - directly to the Database Server
Typical Applications • Java Applications • Java Applets • Web based Applications
Basic JDBC programming • Make a connection with the database • Prepare your query • Supply values (bind variables) if necessary • Execute your query • Examine the results
Database URLs • All start with “jdbc” • Are provided with the drivers documentation. • For example: jdbc:odbc:mydb
Loading the Driver class • Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver” Or • Use java -Djdbc.properties=filename
Making a Connection String url = “jdbc:odbc:mydb” String user=“java2” String pass=“java2” Connection con = DriverManager.getConnection(url, user, pass)
Creating JDBC Statements Statement stmt = con.createStatement()
Executing Statementsthat insert, update, or delete data String qry = “INSERT INTO Students (StudentID, Name) Values (1004,’anil’)” ResultSet rs = stmt.executeUpdate(qry);
Executing Statementsthat return rows String qry = “SELECT StudentID, Name FROM Students” ResultSet rs = stmt.executeQuery(qry);
Retrieving Values from ResultSet while ( rs.next() ) { int id = rs.getInt(“StudentID”); String name = rs.getString(“Name”); System.out.println(id+”\t”+name); }
Retrieving Values from ResultSet(using column ordinals) while ( rs.next() ) { int id = rs.getInt(1); String name = rs.getString(2); System.out.println(id+”\t”+name); }
Using “bind” variables String qry = “INSERT INTO STUDENTS (id, name) VALUES (?,?)” PreparedStatement pstmt = con.prepareStatement(qry);
Executing in a Loop for (i = 0; i < 10; i++) { pstmt.setInt(1, 1000+i); name = inputName(); pstmt.setString(2,name); int n = stmt.executeUpdate() }
Calling Stored Procedures String qry = “{call ProcessDailyChecks}” CallableStatement cstmt = con.prepareCall(qry); int n = cstmt.executeUpdate();
Using Metadata ResultSetMetaData rsmd = rs.getMetaData(); rsmd.getColumnCount() rsmd.getColumnName(int i) rsmd.getColumnType(int i) rsmd.getTableName(int i); rsmd.isAutoIncrement(int i);
Scrollable/Updateable ResultSets ResultSet rs = conn.createStatement(type, concurrency); Types: TYPE_FORWARD_ONLY, TYPE_SCROLL_SENSITIVE, TYPE_SCROLL_INSENSITIVE Concurrency: CONCUR_READ_ONLY CONCUR_UPDATABLE