📄 employeebean.java
字号:
package employee;import java.util.*;import javax.ejb.*;import java.sql.*;import javax.sql.*;import javax.naming.*;public class EmployeeBean implements SessionBean, SessionSynchronization { private String employeeId; private int empNo; private double salary; private SessionContext context; //or use: //private EJBContext context; private Connection con; private String dbName = "java:comp/env/jdbc/employeeDB"; public void salaryToSave(int employeeNo,double amount) throws EmployeeNoException{ empNo= employeeNo; salary = amount; double cur_Salary; try{ cur_Salary=getCurSalary(empNo); if (cur_Salary > 8000.0){ context.setRollbackOnly(); System.out.println("the salary is enough hign,don't need to add"); //throw new EmployeeNoException(); } updateSalary(empNo,salary); }catch (SQLException ex){ throw new EJBException ("Transaction failed due to SQLException: " + ex.getMessage()); } } public void ejbCreate(String id) throws CreateException{ employeeId = id; try{ makeConnection(); //checkingBalance = selectChecking(); }catch (Exception ex){ throw new CreateException(ex.getMessage()); } } public void ejbRemove(){ try{ con.close(); }catch (SQLException ex){ throw new EJBException("ejbRemove SQLException: " + ex.getMessage()); } } public void ejbActivate(){ try { makeConnection(); } catch (Exception ex){ throw new EJBException("ejbActivate Exception: " + ex.getMessage()); } } public void ejbPassivate(){ try { con.close(); } catch (SQLException ex){ throw new EJBException("ejbPassivate Exception: " + ex.getMessage()); } } public void setSessionContext(SessionContext context) { this.context = context; } public void afterBegin(){ System.out.println("afterBegin()"); } public void beforeCompletion() { System.out.println("beforeCompletion()"); } public void afterCompletion(boolean committed) { System.out.println("afterCompletion: " + committed); /*if (committed == false) { ...... }*/ } public EmployeeBean() {}/************************** Database Routines **********************/ private double getCurSalary(int emp_No) throws SQLException { double theSalary=0.0; ResultSet rs; Statement stmt=con.createStatement(); String selectStatement = "select salary from t_employee where emp_no = 2001"; rs=stmt.executeQuery(selectStatement); while(rs.next()){ for(int j=1; j<=rs.getMetaData().getColumnCount(); j++){ //System.out.println(rs.getObject(j)); theSalary=rs.getDouble(j); System.out.println(theSalary); } } stmt.close(); return theSalary; } private void updateSalary(int empNo,double salary) throws SQLException{ Statement stmt=con.createStatement(); String updateStatement = "update t_employee set salary="+salary+" where emp_no="+empNo; stmt.executeUpdate(updateStatement); System.out.println("update t_employee success!"); stmt.close(); } public double getEmpSalary() { return salary; } private void makeConnection() throws NamingException, SQLException { InitialContext ic = new InitialContext(); DataSource ds = (DataSource) ic.lookup(dbName); con = ds.getConnection(); } }//BankBean
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -