?? enrollsessionejb.java
字號:
//實現EnrollSession的具體功能
package enroll.ejb;
import javax.ejb.*;
import java.sql.*;
import javax.sql.*;
import java.util.*;
import javax.naming.*;
import java.rmi.RemoteException;
import javax.rmi.PortableRemoteObject;
public class EnrollSessionEJB implements SessionBean {
private Connection con;
private String dbJndi="java:comp/env/jdbc/ExampleDB";
private String dbId="guest";
private String dbPassword="guest123";
public StudentHome sHome;
public EnrollHome eHome;
public String student_id;
public String name;
public EnrollSessionEJB(){
}
public void setSessionContext(SessionContext sc){
try{
Context initial = new InitialContext();
Object objref=
initial.lookup("ejb/StudentEntityBean");
sHome=(StudentHome)PortableRemoteObject
.narrow(objref, StudentHome.class);
objref=
initial.lookup("ejb/EnrollEntityBean");
eHome=(EnrollHome)PortableRemoteObject
.narrow(objref, EnrollHome.class);
}catch (Exception ex){
throw new EJBException(
"Unable to connect to database. "+
ex.getMessage());
}
}
public void ejbCreate(String student_id)
throws CreateException{
/*
先檢查student_id的基本資料是否已在StudentTBL里,
如果不在則不允許注冊
*/
try{
Student student=sHome.findByPrimaryKey(student_id);
name=student.getName();
}catch (ObjectNotFoundException ex){
throw new CreateException("Student: "
+student_id+" not found in StudentTBL!");
}catch(Exception ex){
throw new EJBException("unenroll: "+
ex.getMessage());
}
this.student_id=student_id;
}
//取得學生姓名
public String getStudentName(){
return name;
}
//注冊選課資料
public void enroll(ArrayList courseItems){
Enroll enroll=null;
//檢查是否已經注冊過了
try{
enroll=eHome.findByPrimaryKey(student_id);
}catch( ObjectNotFoundException onfex){
}catch (Exception ex){
}
//如果已經注冊過了則更新注冊資料,否則新增注冊資料
try{
if(enroll!=null)
enroll.replaceCourseItems(courseItems);
else
eHome.create(student_id, courseItems);
}catch (Exception ex){
throw new EJBException("enroll: "+
ex.getMessage());
}
}
//移除注冊資料
public void unenroll(){
try{
Enroll enroll=eHome.findByPrimaryKey(student_id);
enroll.remove();
}catch (Exception ex){
throw new EJBException("unenroll: "+
ex.getMessage());
}
}
//移除學生基本資料和注冊資料
public void deleteStudent()
throws FinderException{
try{
Enroll enroll=
eHome.findByPrimaryKey(student_id);
Student student=
sHome.findByPrimaryKey(student_id);
enroll.remove();
student.remove();
}catch (Exception ex){
throw new EJBException("deleteStudent: "+
ex.getMessage());
}
}
//移除學生的某個課程資料
public void deleteCourse(String course_id){
PreparedStatement ps=null;
try{
getConnection();
String deleteStatement =
"delete from EnrollTBL "+
"where student_id=? and course_id=?";
ps=con.prepareStatement(deleteStatement);
ps.setString(1, student_id);
ps.setString(2, course_id);
ps.executeUpdate();
}catch (Exception ex){
throw new EJBException("deleteCourse: "+
ex.getMessage());
}finally{
try{
ps.close();
con.close();
}catch(Exception ex){
throw new EJBException("deleteCourse: "+
ex.getMessage());
}
}
}
public void ejbActivate(){
}
public void ejbPassivate(){
}
public void ejbRemove(){
sHome=null;
eHome=null;
}
//---------------------------------------------
private void getConnection()
throws NamingException, SQLException{
InitialContext ic=new InitialContext();
DataSource ds=(DataSource) ic.lookup(dbJndi);
con= ds.getConnection(dbId, dbPassword);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -