Download presentation
Presentation is loading. Please wait.
Published byClementine Poole Modified over 8 years ago
2
Advanced Java Session 5 New York University School of Continuing and Professional Studies
3
2 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
4
3 BeanBox Installing and launching Using BeanBox to create applications Creating JavaBean jar files –jar cvfm filename.Jar manifestfile classes
5
4 Adding Custom Bean Events Write a class Event that extends EventObject Write an interface Listener with a single notification method with any name that takes a single parameter Event and return type of “void” Supply the following two methods in the bean – –public void add Listener( Event e) –public void remove Listener( Event e)
6
5 TimerBean.java Example of adding custom bean events
7
6 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
8
7 Relational Databases Tables Fields Key Index
9
8 Tables and Relations StudentIDName 1001Robert Estes CourseIDCourse 101Java2 1002Mitzy Capture 102Networking Students Courses RefIDStudentID 1001Robert Estes 1002Mitzy Capture CourseID Robert Estes Mitzy Capture StudentID 1001 1002 StudentsXCourses EmailAddr Restes@oncr.com Mcapture@oncr.com EmailAddresses EmailAdrId 1 2 1001restes@hotmail.com3
10
9 Common Operations SELECT –Simple SELECT –Join of multiple tables UPDATE INSERT
11
10 JDBC Design Java Application JDBC Driver Manager Vendor- Supplied JDBC driver JDBC/ODBC Bridge ODBC db JDBC API JDBC Driver API
12
11 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
13
12 Typical Applications Java Applications Java Applets Web based Applications
14
13 Basic JDBC programming Make a connection with the database Prepare your query Supply values (bind variables) if necessary Execute your query Examine the results
15
14 Database URLs All start with “jdbc” Are provided with the drivers documentation. For example: jdbc:odbc:mydb
16
15 Loading the Driver class Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver” Or Use java -Djdbc.properties=filename
17
16 Making a Connection String url = “jdbc:odbc:mydb” String user=“java2” String pass=“java2” Connection con = DriverManager.getConnection(url, user, pass)
18
17 Creating JDBC Statements Statement stmt = con.createStatement()
19
18 Executing Statements that insert, update, or delete data String qry = “INSERT INTO Students (StudentID, Name) Values(1004,’anil’)” ResultSet rs = stmt.executeUpdate(qry);
20
19 Executing Statements that return rows String qry = “SELECT StudentID, Name FROM Students” ResultSet rs = stmt.executeQuery(qry);
21
20 Retrieving Values from ResultSet while ( rs.next() ) { int id = rs.getInt(“StudentID”); String name = rs.getString(“Name”); System.out.println(id+”\t”+name); }
22
21 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); }
23
22 Using “bind” variables String qry = “INSERT INTO STUDENTS (id, name) VALUES (?,?)” PreparedStatement pstmt = con.prepareStatement(qry);
24
23 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() }
25
24 Calling Stored Procedures String qry = “{call ProcessDailyChecks}” CallableStatement cstmt = con.prepareCall(qry); int n = cstmt.executeUpdate();
26
25 Using Metadata ResultSetMetaData rsmd = rs.getMetaData(); rsmd.getColumnCount() rsmd.getColumnName(int i) rsmd.getColumnType(int i) rsmd.getTableName(int i); rsmd.isAutoIncrement(int i);
27
26 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
28
27 Thank you
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.