J2EE Without the Application Server

J2EE Without the Application Server

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.

Prev  [1] [2] [3] [4] [5] [6] [7] Next

Close    To Top
  • Prev Article-Java:
  • Next Article-Java:
  • Now: Tutorial for Web and Software Design > Java > Java EE > Java Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction