CreditCardImpl.java


package creditcard.ejb;

import java.rmi.RemoteException; 
import java.rmi.Remote; 
import import javax.ejb.*; 

public class CreditCardImpl implements SessionBean { 

private transient SessionContext ctx;
private transient Properties props; 
public void ejbActivate () { } 

public void ejbRemove () { } 

public void ejbPassivate () { } 

public void setSessionContext (SessionContext ctx) {

this.ctx = ctx; 
props = ctx.getEnvironment(); 

public void ejbCreate () throws CreateException, RemoteException{ } 

public boolean isValid(String CardNumber) throws RemoteException{ 

java.sql.Connection conn = null
java.sql.Statement stmt = null
java.sql.ResultSet rs = null

try
            InitialContext ctx = new InitialContext(); 
           DataSource ds = (DataSource)ctx.lookup("jdbc/JdbcHelloWorldAppGlobal"); 
            conn = ds.getConnection(); 

           stmt = conn.createStatement(); 

           String query ="select * from creditcard where ccnum ='" + CardNumber + "'"; 
             rs = stmt.executeQuery(query); 

          if  ((rs !=null) && rs.next()) { 

        String ValidCard =  rs.getString("CCNUM"); 
                System.out.println("This is my valid Card Number" + ValidCard); 
      }

     else  { 

  return false;

       } 
       catch(javax.naming.NamingException n) { 
            n.printStackTrace(); 
           throw new java.rmi.RemoteException("Lookup Failed"); 

       catch (java.sql.SQLException e) { 

       e.printStackTrace(); 
       throw new java.rmi.RemoteException(" SQL exception", e); 
       } 

finally

         try
                  rs.close(); 
                  stmt.close(); 
                  conn.close(); 
                  }
catch ( java.sql.SQLException e ) { 
                 e.printStackTrace(); 
               throw new RemoteException ("SQL exception", e); 
                  } 
           } 
return true; 
public boolean isExpired(String CardNumber) throws RemoteException{ 

 java.sql.Connection conn = null; 
 java.sql.Statement stmt = null; 
 java.sql.ResultSet rs = null; 
 try

            InitialContext ctx = new InitialContext(); 
            DataSource ds = (DataSource)ctx.lookup("jdbc/JdbcHelloWorldAppGlobal"); 
            conn = ds.getConnection(); 
            java.util.Date todaysDate = new java.util.Date(); 

           stmt = conn.createStatement(); 

           String query ="select expdate from creditcard where ccnum ='" + CardNumber + "'"; 
            rs = stmt.executeQuery(query); 

            if  ((rs !=null) && rs.next()) { 
        java.sql.Date expirationDate = rs.getDate("EXPDATE"); 

        System.out.println("Card Number" + CardNumber +"expiration date is:" + expirationDate); 
                     System.out.println("oracle : "+ expirationDate); 
                     System.out.println("now    : "+ todaysDate); 

  if (todaysDate.getTime()<expirationDate.getTime()) { 
                                         System.out.println("in the future -- ie. HasNotExpired"); 
                               } 
                              else
                                          System.out.println("in the past -- ie. Expired"); 
            return true;
                                } 
 

      } 
     else
     { 
                throw new RemoteException(" Unable to locate credit card with number =" +CardNumber); 
         } 
       } 

       catch(javax.naming.NamingException n) 
      { 
            n.printStackTrace(); 
           throw new java.rmi.RemoteException("Lookup Failed"); 
       } 

       catch (java.sql.SQLException e) { 
       e.printStackTrace(); 
       throw new java.rmi.RemoteException(" SQL exception", e); 

finally
   try
     rs.close(); 
     stmt.close(); 
     conn.close(); 
   } 

   catch ( java.sql.SQLException e ) { 
     e.printStackTrace(); 
     throw new RemoteException ("SQL exception", e); 
   } 

return false; 
}

public boolean sufficientFunds(String CardNumber, double PurchaseAmount) throws RemoteException{

 java.sql.Connection conn = null;
 java.sql.Statement stmt = null;
 java.sql.ResultSet rs = null;
 try {
            InitialContext ctx = new InitialContext();
            DataSource ds = (DataSource)ctx.lookup("jdbc/JdbcHelloWorldAppGlobal");
            conn = ds.getConnection();
 
           stmt = conn.createStatement();
 
            String query ="select limit from creditcard where ccnum ='" + CardNumber + "'";
            rs = stmt.executeQuery(query);
 
            if  ((rs !=null) && rs.next()) {
      double availiableFunds = rs.getDouble("LIMIT");
  if ( availiableFunds >= PurchaseAmount)
  {
  System.out.println("Passed Credit Limit Check");
                                 }
                                 else
                                     {
                                      System.out.println("Failed Credit Limit Check");
                                      return false;
                                      }
    }
    else
          {
                  throw new RemoteException(" Unable to locate credit card with number =" +CardNumber);
             }
                }
 catch(javax.naming.NamingException n)  {
     n.printStackTrace();
     throw new java.rmi.RemoteException("Lookup Failed");
  }

 catch (java.sql.SQLException e) {
       e.printStackTrace();
       throw new java.rmi.RemoteException(" SQL exception", e);
   }
    finally {
           try {
                  rs.close();
                  stmt.close();
                  conn.close();
                  }

                 catch ( java.sql.SQLException e ) {
                 e.printStackTrace();
                 throw new RemoteException ("SQL exception", e);
                  }
           }
return true;
}

public void debit(String CardNumber, double PurchaseAmount) throws RemoteException{

 java.sql.Connection conn = null;
 java.sql.Statement stmt = null;
 java.sql.ResultSet rs = null;
 try {
            InitialContext ctx = new InitialContext();
            DataSource ds = (DataSource)ctx.lookup("jdbc/JdbcHelloWorldAppGlobal");
            conn = ds.getConnection();
 
           stmt = conn.createStatement();
 
            String query ="select limit from creditcard where ccnum ='" + CardNumber + "'";
            rs = stmt.executeQuery(query);
 
            if  ((rs !=null) && rs.next()) {
      double availiableFunds = rs.getDouble("LIMIT");
      double adjustedCredit = (availiableFunds - PurchaseAmount);
  query = "update creditcard SET limit ='" + adjustedCredit + "' where ccnum ='" + CardNumber + "'";
                                rs = stmt.executeQuery(query);
 
                     }
                      else {
                  throw new RemoteException(" Unable to locate credit card with number =" +CardNumber);
             }
       }
 catch(javax.naming.NamingException n)  {
     n.printStackTrace();
     ctx.setRollbackOnly();
     throw new java.rmi.RemoteException("Lookup Failed");
  }

 catch (java.sql.SQLException e) {
        e.printStackTrace();
        ctx.setRollbackOnly();
        throw new java.rmi.RemoteException(" SQL exception", e);
   }
    finally {
           try {
                  rs.close();
                  stmt.close();
                  conn.close();
                  }

                 catch ( java.sql.SQLException e ) {
                 e.printStackTrace();
                 ctx.setRollbackOnly();
                 throw new RemoteException ("SQL exception", e);
                  }
           }
    }
}