📄 customerbean.java
字号:
package bookstore.ejb;
import bookstore.util.*;
import java.sql.*;
import javax.sql.*;
import java.util.*;
import javax.ejb.*;
import javax.naming.*;
public class CustomerBean implements EntityBean {
java.lang.String customerID;
java.lang.String name;
java.lang.String account;
java.lang.String password;
int sexual;
java.sql.Date birthDay;
java.lang.String city;
java.lang.String profession;
java.lang.String iDCardNum;
java.lang.String address;
java.lang.String zip;
java.lang.String phone;
java.lang.String eMail;
int rank;
private Connection con=null;
EntityContext entityContext;
/**************************business methods*************************/
public void setCustomerID(java.lang.String customerID) {
this.customerID = customerID;
}
public void setName(java.lang.String name) {
this.name = name;
}
public void setAccount(java.lang.String account) {
this.account = account;
}
public void setPassword(java.lang.String password) {
this.password = password;
}
public void setSexual(int sexual) {
this.sexual = sexual;
}
public void setBirthDay(java.sql.Date birthDay) {
this.birthDay = birthDay;
}
public void setCity(java.lang.String city) {
this.city = city;
}
public void setProfession(java.lang.String profession) {
this.profession = profession;
}
public void setIDCardNum(java.lang.String iDCardNum) {
this.iDCardNum = iDCardNum;
}
public void setAddress(java.lang.String address) {
this.address = address;
}
public void setZip(java.lang.String zip) {
this.zip = zip;
}
public void setPhone(java.lang.String phone) {
this.phone = phone;
}
public void setEMail(java.lang.String eMail) {
this.eMail = eMail;
}
public void setRank(int rank) {
this.rank = rank;
}
public java.lang.String getCustomerID() {
return customerID;
}
public java.lang.String getName() {
return name;
}
public java.lang.String getAccount() {
return account;
}
public java.lang.String getPassword() {
return password;
}
public int getSexual() {
return sexual;
}
public java.sql.Date getBirthDay() {
return birthDay;
}
public java.lang.String getCity() {
return city;
}
public java.lang.String getProfession() {
return profession;
}
public java.lang.String getIDCardNum() {
return iDCardNum;
}
public java.lang.String getAddress() {
return address;
}
public java.lang.String getZip() {
return zip;
}
public java.lang.String getPhone() {
return phone;
}
public java.lang.String getEMail() {
return eMail;
}
public int getRank() {
return rank;
}
public CustomerDetails getDetails()
{
System.out.println("CustomerBean getDetails");
return new CustomerDetails (customerID, name,
account, password, sexual, birthDay, city,
profession, iDCardNum,address,zip,phone, eMail,rank);
}//getDetails
/**********************************ejb methods*************************/
public java.lang.String ejbCreate(CustomerDetails customerValue)
throws CreateException
{
System.out.println("CustomerBean ejbCreate");
try
{
makeConnection();
this.customerID=DBHelper.getNextCustomerID(con);
}catch(Exception ex)
{
throw new EJBException("ejbCreate: "+ex.getMessage());
}
this.name=customerValue.getName();
this.account=customerValue.getAccount();
this.password=customerValue.getPassword();
this.sexual=customerValue.getSexual();
this.birthDay=customerValue.getBirthDay();
this.city=customerValue.getCity();
this.profession=customerValue.getProfession();
this.iDCardNum=customerValue.getIDCardNum();
this.address=customerValue.getAddress();
this.zip=customerValue.getZip();
this.phone=customerValue.getPhone();
this.eMail=customerValue.getEmail();
this.rank=customerValue.getRank();
try {
insertRow();
} catch (Exception ex) {
throw new EJBException("ejbCreate: " +
ex.getMessage());
}
return customerID;
}
public void ejbPostCreate(CustomerDetails customerValue) throws CreateException
{
System.out.println("CustomerBean ejbPostCreate");
}
public java.util.Collection ejbFindByCondition(String condition) throws FinderException {
System.out.println("CustomerBean ejbFindByCondition");
Collection result;
try {
result = selectByCondition(condition);
} catch (Exception ex) {
throw new EJBException("ejbFindByCondition " +
ex.getMessage());
}
return result;
}
public void ejbRemove() throws RemoveException {
System.out.println("CustomerBean ejbRemove");
try {
deleteRow(customerID);
} catch (Exception ex) {
throw new EJBException("ejbRemove: " +
ex.getMessage());
}
}//ejbRemove
public java.lang.String ejbFindByPrimaryKey(java.lang.String primaryKey) throws FinderException {
System.out.println("CustomerBean ejbFindByPrimaryKey");
boolean result;
try {
result = selectByPrimaryKey(primaryKey);
} catch (Exception ex) {
throw new EJBException("ejbFindByPrimaryKey: " +
ex.getMessage());
}
if (result) {
return primaryKey;
}
else {
throw new ObjectNotFoundException
("Row for id " + primaryKey + " not found.");
}
}
public void ejbLoad() {
System.out.println("CustomerBean ejbLoad");
try {
loadCustomer();
} catch (Exception ex) {
throw new EJBException("ejbLoad: " +
ex.getMessage());
}
}//ejbLoad
public void ejbStore()
{
System.out.println("CustomerBean ejbStore");
try {
storeCustomer();
} catch (Exception ex) {
throw new EJBException("ejbStore: " +
ex.getMessage());
}
}//ejbStore
public void ejbActivate()
{
System.out.println("CustomerBean ejbActivate");
//
customerID = (String)entityContext.getPrimaryKey();
}
public void ejbPassivate()
{
System.out.println("CustomerBean ejbPassivate");
customerID= null;
}
public void unsetEntityContext()
{
System.out.println("CustomerBean ussetEntiyContext");
this.entityContext = null;
}
public void setEntityContext(EntityContext entityContext) {
System.out.println("CustomerBean setEntiyContext");
this.entityContext = entityContext;
}
/******************************util methods*************************/
private void makeConnection() {
System.out.println("CustomerBean makeConnection in");
try
{
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup("DBSource");
con = ds.getConnection();
}
catch (Exception ex)
{
throw new EJBException("Unable to connect to database. " +
ex.getMessage());
}
System.out.println("CustomerBean makeConnection out");
} // makeConnection
private void releaseConnection() {
System.out.println("CustomerBean releaseConnection in");
try {
con.close();
} catch (SQLException ex) {
throw new EJBException("releaseConnection: " + ex.getMessage());
}
System.out.println("CustomerBean releaseConnection out");
} // releaseConnection
private void insertRow () throws SQLException {
System.out.println("CustomerBean insertRow in");
makeConnection();
String insertStatement =
"insert into Customer values ( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? )";
PreparedStatement prepStmt =
con.prepareStatement(insertStatement);
prepStmt.setString(1, customerID);
prepStmt.setString(2, name);
prepStmt.setString(3, account);
prepStmt.setString(4, password);
prepStmt.setInt(5, sexual);
prepStmt.setDate(6,birthDay);
prepStmt.setString(7, city);
prepStmt.setString(8, profession);
prepStmt.setString(9,iDCardNum);
prepStmt.setString(10,address);
prepStmt.setString(11, zip);
prepStmt.setString(12, phone);
prepStmt.setString(13, eMail);
prepStmt.setInt(14,rank);
prepStmt.executeUpdate();
prepStmt.close();
releaseConnection();
System.out.println("CustomerBean insertRow out");
}//insertRow
private Collection selectByCondition(String condition) throws SQLException {
System.out.println("CustomerBean selectByCondition in");
makeConnection();
String selectStatement =
"select CustomerID " +
"from Customer where "+condition;
System.out.println(selectStatement);
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(selectStatement);
ArrayList a = new ArrayList();
while (rs.next())
{
a.add(rs.getString(1));
}
stmt.close();
releaseConnection();
System.out.println("CustomerBean selectByCondition out");
return a;
}//selectByCondition
private boolean selectByPrimaryKey(String primaryKey) throws SQLException {
//本函数主要目的还在于确保customer_id在数据库表中是存在的
System.out.println("CustomerBean selectByPrimaryKey in");
makeConnection();
//a question here
String selectStatement =
"select CustomerID " +
"from Customer where CustomerID = ? ";
PreparedStatement prepStmt =
con.prepareStatement(selectStatement);
prepStmt.setString(1, primaryKey);
ResultSet rs = prepStmt.executeQuery();
boolean result = rs.next();
prepStmt.close();
releaseConnection();
System.out.println("CustomerBean selectByPrimaryKey out");
return result;
}//selectByPrimaryKey
private void loadCustomer() throws SQLException {
System.out.println("CustomerBean loadCustomer in");
makeConnection();
String selectStatement =
"select Name, Account, Password,Sexual,BirthDay, City, Profession, IDCardNum,Address,Zip, Phone, EMail ,Rank " +
"from Customer where CustomerID = ? ";
PreparedStatement prepStmt =
con.prepareStatement(selectStatement);
prepStmt.setString(1, customerID);
ResultSet rs = prepStmt.executeQuery();
if (rs.next())
{
name = rs.getString(1);
account = rs.getString(2);
password = rs.getString(3);
sexual = rs.getInt(4);
birthDay=rs.getDate(5);
city = rs.getString(6);
profession = rs.getString(7);
iDCardNum=rs.getString(8);
address=rs.getString(9);
zip = rs.getString(10);
phone = rs.getString(11);
eMail = rs.getString(12);
rank=rs.getInt(13);
prepStmt.close();
releaseConnection();
}
else {
prepStmt.close();
releaseConnection();
throw new NoSuchEntityException("loadCustomer:Row for id " +
customerID + " not found in database.");
}
System.out.println("CustomerBean loadCustomer out");
}//loadCustomer
private void storeCustomer() throws SQLException {
System.out.println("CustomerBean storeCustomer in");
makeConnection();
String updateStatement =
"update Customer " +
"set Name = ? , Account = ? , Password = ? , Sexual = ? ,"+
"BirthDay=?,City = ? ,Profession= ?,IDCardNum= ? ,"+
"Address = ? ,Zip= ? ,Phone = ? ,EMail = ? ,Rank = ?" +
"where CustomerID = ? ";
PreparedStatement prepStmt =
con.prepareStatement(updateStatement);
prepStmt.setString(1, name);
prepStmt.setString(2, account);
prepStmt.setString(3, password);
prepStmt.setInt(4, sexual);
prepStmt.setDate(5,birthDay);
prepStmt.setString(6, city);
prepStmt.setString(7,profession);
prepStmt.setString(8,iDCardNum);
prepStmt.setString(9,address);
prepStmt.setString(10, zip);
prepStmt.setString(11, phone);
prepStmt.setString(12, eMail);
prepStmt.setInt(13,rank);
prepStmt.setString(14, customerID);
int rowCount = prepStmt.executeUpdate();
prepStmt.close();
releaseConnection();
if (rowCount == 0) {
throw new EJBException("storeCustomer:Storing row for id " + customerID + " failed.");
}
System.out.println("CustomerBean storeCustomer out");
}//storeCustomer
private void deleteRow(String id) throws SQLException {
System.out.println("CustomerBean deleteRow in");
makeConnection();
String deleteStatement =
"delete from customer where CustomerID = ? ";
PreparedStatement prepStmt =
con.prepareStatement(deleteStatement);
prepStmt.setString(1, id);
prepStmt.executeUpdate();
prepStmt.close();
releaseConnection();
System.out.println("CustomerBean deleteRow out");
}//deleteRow
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -