Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
JAVA
JAVA
borderStylesolid
titleAccountClient.java
package sample.jpa.appclient;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;



public class AccountClient {

 //@PersistenceContext(unitName="JPA-App-Client")
   private EntityManager em;

    public AccountClient() {

        EntityManagerFactory emf = 
   Persistence.createEntityManagerFactory("JPA-App-Client");

        if (emf == null) {
      System.out.println("emf is null!!!     throw new IllegalStateException("EntityManagerFactory is unavailable");
        }

        em = emf.createEntityManager();
        if (em == null) {
      System.out.println("em      throw new IllegalStateException("EntityManager is null!!!unavailable");

         }
    }

    public static void main(String[] args) {

        AccountClient client = new AccountClient();

        String opt = args[0];

        if (opt"create".equals("create"opt)) {

            if (args.length != 6) {

    System.out.println("Enter values for accountNo, 
        System.err.println("No values for accountNo, name, address, branchCode and balance;. Exiting.");

                System.exit(0);
            }
 else else{

                int accNo = Integer.parseInt(args[1]);
    String name =           String name = args[2];
                String address = args[3];
                int branchCode = Integer.parseInt(args[4]);
                double balance = Double.parseDouble(args[5]);
                client.createAccount(accNo, name, address, branchCode, balance);

            }

        } else if (opt"list".equals("list"opt)) {

            ListList<Account> accListaccounts = client.listAccounts();
   for(int i = 0; i < accList.size(); i++){
  for  System(Account account: accounts) {
                System.out.println("_________________________________");
      Account    acc = (Account)accList.get(i);
    System.out.println(accaccount.getAccountNo());
                System.out.println(accaccount.getName());
      System.out.println(acc          System.out.println(account.getAddress());
                System.out.println(accaccount.getBranchCode());
                System.out.println(accaccount.getBalance());
                System.out.println("____________________________________________");
                System.out.println("");
   }

 }else         }
        } else if (opt"update".equals("update"opt)) {

            if (args.length != 3) {
                System.out.println("EnterNo values for accountNo and new balace value ;");
   System.exit(0);
  }

  else{
. Exiting.");
                System.exit(0);
            } else {
                int accNo = Integer.parseInt(args[1]);
                double newbalance = Double.parseDouble(args[2]); 

                client.updateAccountBalance(accNo, newbalance);
  }
			
 }          }
        } else if (opt.equals("delete")) {

            if (args.length != 2) {
                System.out.println("EnterNo values for accountNo for delete. Exiting.");
                System.exit(0);
  }
  else {

            } else {
                int accNo = Integer.parseInt(args[1]);
                client.deleteAccount(accNo);
            }
        }
       
 else {
            System.out.println("Unknown option selected...!!err.println("Unknown option (" + opt + ") selected");
        }

 }
	
 public Account createAccount}

    public Account   createAccount(int accNo, String name, 
	 String address, int branchCode,
	 double balance) {
		
        Account acc1 = em.find(Account.class, accNo);
  if(acc1 != null) throw new 
   IllegalArgumentException
           if (acc1 != null) {
            throw new IllegalArgumentException("Account already exists: - Accountaccount Number (" + accNo + ")");
        }
        Account acc = new Account();
        acc.setAccountNo(accNo);
        acc.setAddress(address);
        acc.setBalance(balance);
        acc.setBranchCode(branchCode);
);
        acc.setName(name);
        System.out.println("Persisting account entity (accNo = " + accNo + ")");
        em.persist(acc);
        System.out.println("Persisted successfully account entity (accNo = " + accNo + ")");
        return acc;

    }
	
  public List listAccounts@SuppressWarnings("unchecked"){
   if(em ==public null) System.out.println("em is null!!!");
  List<Account> listAccounts() {
        Query q = em.createQuery("SELECT a FROM Account a");
    List    List<Account> currList = q.getResultList();
        return currList;
 }

	
 public Account updateAccountBalance}

    public Account  updateAccountBalance(int accNo, double newBalance) {

        Account acc = em.find(Account.class, accNo);

        if (acc == null) throw new 
 null) {
            IllegalArgumentException
throw new IllegalArgumentException("Account not found : Account Number (" + accNo + ")");

   acc.setBalance(newBalance);
		em.merge(acc);
  return acc;
		 
 }
	
 public void deleteAccount(int accNo){
  Account acc = em.find(Account.class, accNo        acc.setBalance(newBalance);
        em.merge(acc);
  if(acc == null) throw new IllegalArgumentException      return acc;

   ("Account not found : Account Number ("+accNo+")");
  em.remove(acc);
 }

    public void deleteAccount(int accNo) {
        em.remove(em.getReference(Account.class, accNo));
    }
}

Preparing Deployment Descriptors and Deployment Plans

...

Change the directory to <geronimo_home>\bin.
Submit the below command
deploy.bat --user system --password manager deploy <location_of_the_exported_jar_file>

...