📄 acejb.java
字号:
import java.sql.*;
import javax.sql.*;
import java.util.*;
import javax.ejb.*;
import javax.naming.*;
public class AcEJB implements EntityBean {
private String id;
private String date;
private String vc;
private String check;
private double amount;
private EntityContext context;
private Connection con;
private String dbName = "java:comp/env/jdbc/AccountDB";
public String getDate() {
return date;
}
public String getVc() {
return vc;
}
public String getCheck() {
return check;
}
public double getAmount() {
return amount;
}
public String ejbCreate(String id, String date, String vc, String check, double amount) throws CreateException {
try {
System.out.println("Inside ejbcreate");
insertRow(id, date, vc, check, amount);
System.out.println("after insert row");
} catch (Exception ex) {
throw new EJBException("ejbCreate: " +
ex.getMessage());
}
this.id = id;
this.date = date;
this.vc =vc;
this.check = check;
this.amount=amount;
return id;
}
public String ejbFindByPrimaryKey(String primaryKey)
throws FinderException {
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 ejbRemove() {
try {
deleteRow(id);
} catch (Exception ex) {
throw new EJBException("ejbRemove: " +
ex.getMessage());
}
}
public void setEntityContext(EntityContext context) {
this.context = context;
try {
System.out.println("Inside setEntityContext");
makeConnection();
} catch (Exception ex) {
throw new EJBException("Unable to connect to database. " +
ex.getMessage());
}
}
public void unsetEntityContext() {
try {
con.close();
} catch (SQLException ex) {
throw new EJBException("unsetEntityContext: " + ex.getMessage());
}
}
public void ejbActivate() {
id = (String)context.getPrimaryKey();
}
public void ejbPassivate() {
id = null;
}
public void ejbLoad() {
try {
System.out.println("Inside ejbLoad()");
loadRow();
System.out.println("After loadrow() in ejbLoad()");
} catch (Exception ex) {
throw new EJBException("ejbLoad: " +
ex.getMessage());
}
}
public void ejbStore() {
try {
System.out.println("Inside ejbStore()");
storeRow();
System.out.println("After storeRow() in ejbLoad()");
} catch (Exception ex) {
throw new EJBException("ejbLoad: " +
ex.getMessage());
}
}
public void ejbPostCreate(String id, String date, String vc, String check, double amount) { }
/*** Routines to access database ***/
private void makeConnection() throws NamingException, SQLException {
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup(dbName);
con = ds.getConnection();
}
private void insertRow (String id, String date, String vc, String check, double amount) throws SQLException {
System.out.println("Inside sql insert row");
String insertStatement =
"insert into Account_Holder_Transaction values ( ? , ? , ? , ?, ? )";
PreparedStatement prepStmt =
con.prepareStatement(insertStatement);
System.out.println("Before id");
prepStmt.setString(1, id);
System.out.println("Before date");
prepStmt.setString(2, date);
System.out.println("Before vc");
prepStmt.setString(3, vc);
System.out.println("Before check");
prepStmt.setString(4, check);
System.out.println("Before amount");
prepStmt.setDouble(5, amount);
prepStmt.executeUpdate();
prepStmt.close();
}
private void deleteRow(String id) throws SQLException {
String deleteStatement =
"delete from Account_Holder_Transaction where cAccount_id = ? ";
PreparedStatement prepStmt =
con.prepareStatement(deleteStatement);
prepStmt.setString(1, id);
prepStmt.executeUpdate();
prepStmt.close();
}
private boolean selectByPrimaryKey(String primaryKey)
throws SQLException {
String selectStatement =
"select cAccount_id " +
"from Account_Holder where cAccount_id = ? ";
PreparedStatement prepStmt = con.prepareStatement(selectStatement);
prepStmt.setString(1, primaryKey);
ResultSet rs = prepStmt.executeQuery();
boolean result = rs.next();
prepStmt.close();
return result;
}
private void loadRow() throws SQLException {
String selectStatement =
"select dDate_of_transaction, vcParticulars, cCheck_no, mAmount from Account_Holder_Transaction where cAccount_id = ? ";
PreparedStatement prepStmt =
con.prepareStatement(selectStatement);
System.out.println("after con.prep");
prepStmt.setString(1, this.id);
ResultSet rs = prepStmt.executeQuery();
System.out.println("After executeQuery()");
if (rs.next()) {
System.out.println("Inside if");
this.date = rs.getString(1);
System.out.println("After 1");
this.vc = rs.getString(2);
System.out.println("After 2");
this.check = rs.getString(3);
System.out.println("After 3");
this.amount = rs.getDouble(4);
prepStmt.close();
}
else {
prepStmt.close();
throw new NoSuchEntityException("Row for id " + id + " not found in database.");
}
}
private void storeRow() throws SQLException {
String updateStatement =
"update Account_Holder_Transaction set dDate_of_transaction = ? , vcParticulars = ? , cCheck_no = ?, mAmount = ? where cAccount_id = ?";
PreparedStatement prepStmt =
con.prepareStatement(updateStatement);
prepStmt.setString(1, date);
prepStmt.setString(2, vc);
prepStmt.setString(3, check);
prepStmt.setDouble(4, amount);
prepStmt.setString(5, id);
int rowCount = prepStmt.executeUpdate();
prepStmt.close();
if (rowCount == 0) {
throw new EJBException("Storing row for id " + id + " failed.");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -