1 / 27

Advanced Java Session 5

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

knut
Download Presentation

Advanced Java Session 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advanced JavaSession 5 New York University School of Continuing and Professional Studies

  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

  3. BeanBox • Installing and launching • Using BeanBox to create applications • Creating JavaBean jar files • jar cvfm filename.Jar manifestfile classes

  4. 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)

  5. TimerBean.java • Example of adding custom bean events

  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

  7. Relational Databases • Tables • Fields • Key • Index

  8. 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

  9. Common Operations • SELECT • Simple SELECT • Join of multiple tables • UPDATE • INSERT

  10. JDBC Design Java Application JDBC API JDBC Driver Manager JDBC Driver API JDBC/ODBC Bridge Vendor- Supplied JDBC driver ODBC db db

  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

  12. Typical Applications • Java Applications • Java Applets • Web based Applications

  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

  14. Database URLs • All start with “jdbc” • Are provided with the drivers documentation. • For example: jdbc:odbc:mydb

  15. Loading the Driver class • Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver” Or • Use java -Djdbc.properties=filename

  16. Making a Connection String url = “jdbc:odbc:mydb” String user=“java2” String pass=“java2” Connection con = DriverManager.getConnection(url, user, pass)

  17. Creating JDBC Statements Statement stmt = con.createStatement()

  18. Executing Statementsthat insert, update, or delete data String qry = “INSERT INTO Students (StudentID, Name) Values (1004,’anil’)” ResultSet rs = stmt.executeUpdate(qry);

  19. Executing Statementsthat return rows String qry = “SELECT StudentID, Name FROM Students” ResultSet rs = stmt.executeQuery(qry);

  20. Retrieving Values from ResultSet while ( rs.next() ) { int id = rs.getInt(“StudentID”); String name = rs.getString(“Name”); System.out.println(id+”\t”+name); }

  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); }

  22. Using “bind” variables String qry = “INSERT INTO STUDENTS (id, name) VALUES (?,?)” PreparedStatement pstmt = con.prepareStatement(qry);

  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() }

  24. Calling Stored Procedures String qry = “{call ProcessDailyChecks}” CallableStatement cstmt = con.prepareCall(qry); int n = cstmt.executeUpdate();

  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);

  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

  27. Thank you

More Related