ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

J2EE Without the Application Server
Pages: 1, 2, 3, 4, 5, 6, 7

Step 1: Coding The Bank DAO

The source code for Bank is relatively straightforward JDBC, as shown below. I should point out that cleaning up resources and exception handling have been kept to a minimum for reasons of clarity.



package jdbc;

import javax.sql.*;

import java.sql.*;

public class Bank 
{

  private DataSource dataSource;

    
  public Bank() {}
  
  public void setDataSource ( DataSource dataSource )
  {
    this.dataSource = dataSource;
  }

  private DataSource getDataSource()
  {
    return this.dataSource;
  }


  private Connection getConnection()
  throws SQLException
  {
    Connection ret = null;
    if ( getDataSource() != null ) {
        ret = getDataSource().
              getConnection();
    }
    return ret;
  }

  private void closeConnection ( Connection c )
  throws SQLException
  {
    if ( c != null ) c.close();
  }
    
  public void checkTables()
  throws SQLException
  {
        
    Connection conn = null;
    try {
      conn = getConnection();
      Statement s = conn.createStatement();
      try {
        s.executeQuery ( 
        "select * from Accounts" );
      }
      catch ( SQLException ex ) {
        //table not there => create it
        s.executeUpdate ( 
        "create table Accounts ( " +
        "account VARCHAR ( 20 ), " + 
        "owner VARCHAR(300), " + 
        "balance DECIMAL (19,0) )" );
        for ( int i = 0; i < 100 ; i++ ){
          s.executeUpdate ( 
          "insert into Accounts values ( " +
          "'account"+i +"' , 'owner"+i +"', 10000 )"
          );
        }
      }
      s.close();
      }
      finally {
        closeConnection ( conn );

      }

      //That concludes setup
  }

    
  //
  //Business methods are below
  //

  public long getBalance ( int account )
  throws SQLException
  {
        
    long res = -1;
    Connection conn = null;

    try {
      conn = getConnection();
      Statement s = conn.createStatement();
      
      String query = 
      "select balance from Accounts where account='"+
      "account" + account +"'";
      
      ResultSet rs = s.executeQuery ( query );
      if ( rs == null || !rs.next() ) 
        throw new SQLException ( 
        "Account not found: " + account );
      res = rs.getLong ( 1 );
      s.close();
    }
    finally {
        closeConnection ( conn );
    }
    return res;
        
  }

  public void withdraw ( int account , int amount )
  throws Exception
  {
    Connection conn = null;

    try {
      conn = getConnection();
      Statement s = conn.createStatement();

      String sql = 
      "update Accounts set balance = balance - "+
      amount + " where account ='account"+
      account+"'";
      
      s.executeUpdate ( sql );
      s.close();
    
    }
    finally {
        closeConnection ( conn );

    }
  }
        
}

Note that there are no dependencies on EJB, or anything else application-server-specific. Indeed, this is pure Java code that can run in any Java Standard Edition (J2SE) environment.

You should also note that we use the generic DataSource interface from JDBC. This means that our class is independent of the actual JDBC vendor class. You might be wondering how this is tied to your particular database management system (DBMS) vendor's JDBC implementation. This is what the Spring framework does. The technique is called dependency injection: the datasource object is supplied by Spring when it calls the setDataSource method during the startup phase of our application. More information on Spring follows in the next sections. If we were using an application server, then we would have to resort to Java Naming and Directory Interface (JNDI) lookups instead.

Instead of accessing JDBC directly, we could also use Hibernate or a Java Data Objects (JDO) tool to do the persistence for us. Again, this would be free of any EJB-related code.

Pages: 1, 2, 3, 4, 5, 6, 7

Next Pagearrow