⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 registrationbean.java

📁 jdbc书
💻 JAVA
字号:
package registration;

import java.rmi.RemoteException;
import javax.ejb.*;
import java.util.*;
import java.text.NumberFormat;
import java.sql.*;

// Bean-managed persistence using JDBC[TM]

public class RegistrationBean implements EntityBean {

    private transient EntityContext ctx;
    private PreparedStatement ps=null;
    private Connection con=null;
    public String theuser, password, creditcard, emailaddress;
    public double balance;

    static {
        new weblogic.jdbc.jts.Driver(); 
    }

    public Connection getConnection() throws SQLException {
        return DriverManager.getConnection("jdbc:weblogic:jts:ejbPool");
    }



    public boolean verifyPassword(String password) throws RemoteException { 
	if (this.password.equals(password)) {
	    return true;
	} else {
            return false;
        }
    }
    public String getEmailAddress() throws RemoteException { 
	return emailaddress;
    }
    public String getUser() throws RemoteException { 
	return theuser;
    }
    public int adjustAccount(double amount) throws RemoteException { 
        balance=balance+amount;
        return(0);
    }

    public double getBalance() throws RemoteException {
	return balance;
    }

    public RegistrationPK ejbCreate(String theuser, String password, String emailaddress, String creditcard) throws CreateException, RemoteException {

	this.theuser=theuser;
	this.password=password;
	this.emailaddress=emailaddress;
	this.creditcard=creditcard;
	this.balance=0;

        try {
           con=getConnection();
           ps=con.prepareStatement("insert into registration (theuser, password, emailaddress, creditcard, balance) values (?, ?, ?, ?, ?)");
           ps.setString(1, theuser);
           ps.setString(2, password);
           ps.setString(3, emailaddress);
           ps.setString(4, creditcard);
           ps.setDouble(5, balance);
           if (ps.executeUpdate() != 1) {
               throw new CreateException ("JDBC did not create any row");
           }
           RegistrationPK primaryKey = new RegistrationPK();
           primaryKey.theuser = theuser;
           return primaryKey;
       } catch (CreateException ce) {
          throw ce;
       } catch (SQLException sqe) {
          throw new CreateException (sqe.getMessage());
       } finally {
          try {
             ps.close();
          } catch (Exception ignore) {}
          try {
             con.close();
          } catch (Exception ignore) {}
       }
    }
 

    public void ejbPostCreate(String theuser, String password, String emailaddress, String creditcard) throws CreateException, RemoteException {
    }

    public void setEntityContext(javax.ejb.EntityContext ctx) throws RemoteException {
    this.ctx = ctx;
    }

    public void unsetEntityContext() throws RemoteException {   
    ctx = null;  
    } 

    public void ejbRemove() throws RemoteException, RemoveException { }
    public void ejbActivate() throws RemoteException { }
    public void ejbPassivate() throws RemoteException { }
    public void ejbLoad() throws RemoteException { 
       try {
          refresh((RegistrationPK) ctx.getPrimaryKey());
       }
       catch (FinderException fe) {
          throw new RemoteException (fe.getMessage());
       }
    }

    public void ejbStore() throws RemoteException { 
        Connection con = null;
        PreparedStatement ps = null;
        try {
           con = getConnection();
           ps = con.prepareStatement("update registration set password = ?, emailaddress = ?, creditcard = ?, balance = ? where theuser = ?");
           ps.setString(1, password);
           ps.setString(2, emailaddress);
           ps.setString(3, creditcard);
           ps.setDouble(4, balance);
           ps.setString(5, theuser);
           int i = ps.executeUpdate();
           if (i == 0) {
               throw new RemoteException ("ejbStore: Registration (" + theuser
                                   + ") not updated");
           }
       } catch (RemoteException re) {
           throw re;
       } catch (SQLException sqe) {
          throw new RemoteException (sqe.getMessage());
       } finally {
          try {
             ps.close();
          } catch (Exception ignore) {}
          try {
             con.close();
          } catch (Exception ignore) {}
       }
    }

    public RegistrationPK ejbFindByPrimaryKey(RegistrationPK pk)
                           throws FinderException, RemoteException {

        if ((pk == null) || (pk.theuser == null)) {
            throw new FinderException ("primary key cannot be null");
        }
        refresh(pk);
        return pk;
    }

    private void refresh(RegistrationPK pk) 
                      throws FinderException, RemoteException {

        if (pk == null) {
            throw new RemoteException ("primary key cannot be null");
        }
        Connection con = null;
        PreparedStatement ps = null;
        try {
           con=getConnection();
           ps=con.prepareStatement("select password, emailaddress, creditcard, balance from registration where theuser = ?");
           ps.setString(1, pk.theuser);
           ps.executeQuery();
           ResultSet rs = ps.getResultSet();
           if (rs.next()) {
              theuser = pk.theuser;
              password = rs.getString(1);
              emailaddress = rs.getString(2);
              creditcard = rs.getString(3);
              balance = rs.getDouble(4);
           }
           else {
               throw new FinderException ("Refresh: Registration ("
                                   + pk.theuser + ") not found");
           }
       }
       catch (SQLException sqe) {
           throw new RemoteException (sqe.getMessage());
       }
       finally {
          try {
             ps.close();
          }
          catch (Exception ignore) {}
          try {
             con.close();
          } catch (Exception ignore) {}
       }
    }

}

⌨️ 快捷键说明

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