Upcoming SlideShare
Loading in...5

Like this? Share it with your network








Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

DaisyWongJ2EE.ppt Presentation Transcript

  • 1. JDBC in J2EE
    • Connection to Cloudscape database using JDBC
    • J2EE Platform – services and architecture
    • Enterprise JavaBeans (EJB)
      • Session Beans vs. Entity Beans
    • EJB access to databases using JDBC
      • Database connection
      • Persistence management (Entity Bean e.g.)
      • Transaction management (Session Bean e.g.)
  • 2. J2EE Services
    • HTTP - enables Web browsers to access servlets and JavaServer PagesTM (JSP) files
    • EJB - allows clients to invoke methods on enterprise beans
    • Authentication - enforces security by requiring users to log in
    • Naming and Directory - allows programs to locate services and components through the Java Naming and Directory InterfaceTM (JNDI) API
  • 3. J2EE Architecture Ref. Java TM 2 Enterprise Edition Developer's Guide, Figure 1-2
  • 4. Enterprise JavaBeans (EJB)
    • Server-side Java components
    • Contain the business logic of enterprise application
    • Support database access
    • Transactional
    • Multi-user secure
    • Managed by the EJB container
    • Prohibited from a set of operations
  • 5. Session Bean vs. Entity Bean Ref. Java TM 2 Enterprise Edition Developer's Guide, Table 1-1 Persistent. Entity state remains in a database. Not persistent. Persistence May be shared by multiple clients. May have one client. Shared Access Represents a business entity object that exists in persistent storage. Performs a task for a client Purpose Entity Bean Session Bean
  • 6. EJB Access to Databases Using JDBC API
    • Making a connection to database:
    • Should not hardcode the actual name (URL) of the database in EJB
    • Should refer to the database with a logical name
    • Use a JNDI lookup when obtaining the database connection.
    • Users and password not needed for the Cloudscape bundled with J2EE
    J2EE uses JDBC 2.0 (java.sql) and JDBC 2.0 Optional package (javax.sql)
    • Driver and Data source properties:
    • set in J2EE default.properties file
    • jdbc.drivers=COM.cloudscape.core.RmiJdbcDriver
    • jdbc.datasources=jdbc/Cloudscape|jdbc:cloudscape:rmi:CloudscapeDB; create=true
  • 7. Making a connection to database example 1. Specify the logical database name. private String dbName = "java:comp/env/jdbc/AccountDB"; 2. Obtain the DataSource associated with the logical name. InitialContext ic = new InitialContext(); DataSource ds = (DataSource) ic.lookup(dbName); 3. Get the Connection from the DataSource. Connection con = ds.getConnection();
  • 8. Specifying JNDI name for deployment Step 1: Enter the code name
  • 9. Step 2: Map the coded name to the JNDI name
  • 10. Persistence Management
    • Container-Managed Persistence
    • Entity bean code does not contain database access calls.
    • The EJB container generates the SQL statements.
    • Bean-Managed Persistence
    • Entity bean code contains the database access calls (SQLs) (i.e. you write the code!)
  • 11.
    • Container Managed example : Product entity bean
      • ProductEJB.java
      • ProductHome.java
      • Product.java
      • ProductClient.java
    • Bean Managed example : Account entity bean
      • AccountEJB.java
      • AccountHome.java
      • Account.java
      • AccountClient.java
  • 12.  
  • 13.  
  • 14.  
  • 15. Transaction Management
  • 16. Container-Managed Transactions
    • Description
    • Code does not include statements that begin and end the transaction
    • Immediately before an EJB method starts - transaction begins
    • Just before the method exits - commits
    • Each method can be associated with a single transaction
    • Prohibited methods, e.g.:
    • commit, setAutoCommit, and rollback methods of java.sql.Connection
    • Limitation:
    • When a method is executing, it can be associated with either a single transaction or no transaction at all
  • 17. Bean Managed Transaction
    • Description
    • Session bean code invokes methods that mark the boundaries of the transaction - setAutoCommit(); commit(); rollback();
    • An entity bean may not have bean-managed transactions
    public void ship (String productId, String orderId, int quantity) { try { con.setAutoCommit(false); updateOrderItem(productId, orderId); updateInventory(productId, quantity); con.commit(); } catch (Exception ex) { try { con.rollback(); throw new EJBException("Transaction failed: " + ex.getMessage()); } catch (SQLException sqx) { throw new EJBException("Rollback failed: " + sqx.getMessage()); } } } Ref. Java TM 2 Enterprise Edition Developer's Guide, JDBC Transaction
  • 18. Resouces
    • Java TM 2 SDK, Enterprise Edition Technical Documentation
      • Java TM 2 Enterprise Edition Developer's Guide
    • http://java.sun.com/j2ee/j2sdkee/
    • http://archives.java.sun.com/archives/j2ee-interest.html
    • Developing Enterprise Applications with the Java TM 2 Platform Enterprise Edition http://java.sun.com/j2ee/blueprints/
    • http://www.cloudscape.com