paymentbean.java

来自「精通NetBeans光盘源代码,很好很好的资料」· Java 代码 · 共 171 行

JAVA
171
字号
package org.netbeans.app.StatelessBeanExample;

//import java.rmi.RemoteException;
//import java.util.Vector;
import javax.ejb.*;
//import java.rmi.*;
//import javax.ejb.*;
//import java.sql.*;
//import javax.sql.*;
//import javax.naming.*;
//import java.util.*;

/**
 * This is the bean class for the PaymentBean enterprise bean.
 * Created 2006-3-17 0:28:52
 * @author Administrator
 */
public class PaymentBean implements SessionBean, PaymentRemoteBusiness {
    private SessionContext context;
    
    // <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods. Click the + sign on the left to edit the code.">
    // TODO Add code to acquire and use other enterprise resources (DataSource, JMS, enterprise bean, Web services)
    // TODO Add business methods or web service operations
    /**
     * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
     */
    public void setSessionContext(SessionContext aContext) {
        context = aContext;
    }
    
    /**
     * @see javax.ejb.SessionBean#ejbActivate()
     */
    public void ejbActivate() {
        
    }
    
    /**
     * @see javax.ejb.SessionBean#ejbPassivate()
     */
    public void ejbPassivate() {
        
    }
    
    /**
     * @see javax.ejb.SessionBean#ejbRemove()
     */
    public void ejbRemove() {
        
    }
    // </editor-fold>
    
    /**
     * See section 7.10.3 of the EJB 2.0 specification
     * See section 7.11.3 of the EJB 2.1 specification
     */
    public void ejbCreate() {
        // TODO implement ejbCreate if necessary, acquire resources
        // This method has access to the JNDI context so resource aquisition
        // spanning all methods can be performed here such as home interfaces
        // and data sources.
    }
    
    
    
    // Add business logic below. (Right-click in editor and choose
    // "EJB Methods > Add Business Method" or "Web Service > Add Operation")
          public boolean payByCash(String customer, double amount) throws java.rmi.RemoteException
          {
              return process(customer, "cash"," ", amount);
          }
      public boolean payByCheck(String customer, String check_number, double amount ) throws java.rmi.RemoteException
      {
          return process(customer, "check", check_number, amount);
      }
      public boolean payByCreditCard(String customer, String card_number, double amount) throws java.rmi.RemoteException
      {
         return process(customer, "credit", card_number, amount);
      }
      public java.util.Vector  search(String customer ) throws java.rmi.RemoteException
      {

	    java.util.Vector vec = new java.util.Vector();
		
	
		 try{
		  	 java.sql.Connection conn = getConnection();
			 java.sql.PreparedStatement ps = conn.prepareStatement("select * from Payment_table where Customer_Name = ?");

			 ps.setString(1, customer);

			 java.sql.ResultSet rs = ps.executeQuery();
			 
			 while (rs.next()) {
   		         String temp[] = new String[4];

			     temp[0]=rs.getString("customer_name");
				 temp[1]=rs.getString("payment_type");
				 temp[2]=rs.getString("card_number");
				 temp[3]=rs.getString("amount");
				 vec.add(temp);
				 temp = null;

			 }

			 rs.close();
			 ps.close();
		     conn.close();

			 }catch(Exception e)

			 {	 
				 System.out.println(e.getMessage());
				 e.printStackTrace();
			 }

		     return vec;
      }
      
       private boolean process(String cusName, String payType, String cardNumber, double amt)
       {
                 boolean retType = false;
		 try{
		  	 java.sql.Connection conn = getConnection();
			 java.sql.PreparedStatement ps = conn.prepareStatement("insert into Payment_table (Customer_Name, Payment_type, Card_number, Amount) values (?, ?, ?, ?)");

			 ps.setString(1, cusName);
			 ps.setString(2, payType);
			 ps.setString(3, cardNumber);
      		         ps.setDouble(4, amt);
			 int value = ps.executeUpdate();
			 if(value == 1) retType = true;

			 ps.close();
		        conn.close();

			 }catch(Exception e)

			 {	 
				 System.out.println(e.getMessage());
				 e.printStackTrace();
			 }

		     return retType;          
       }
    
     	     private java.sql.Connection getConnection() throws java.sql.SQLException	{

		     javax.naming.Context ctx = null;
		     javax.sql.DataSource ds = null;
		     String url = "localhost:1099";
		     try {

			     java.util.Hashtable h = new java.util.Hashtable( );
			     h.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,
						"org.jnp.interfaces.NamingContextFactory");
        		 h.put(javax.naming.Context.PROVIDER_URL, url);
			     ctx = new javax.naming.InitialContext(h);

			     ds = (javax.sql.DataSource) ctx.lookup("java:wzthhb");

		     } catch(Exception ne) {
			     System.out.println("UNABLE to get a connection from !");

		     }

	        return ds.getConnection();
       }   
    
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?