📄 studentbean.java
字号:
package sessionEntity;
import java.rmi.RemoteException;
import java.sql.*;
import java.util.*;
import javax.ejb.*;
import javax.naming.*;
import javax.sql.DataSource;
public class StudentBean implements EntityBean {
private EntityContext ctx;
private DataSource dataSource;
private String tableName;
private String name;
private Integer ssn; // primary key
private int grade;
public void setEntityContext(EntityContext c) {
ctx = c;
try {
Context envCtx = (Context) new InitialContext().lookup("java:/comp/env");
tableName = (String) envCtx.lookup("tableName");
String poolName = (String) envCtx.lookup("poolName");
dataSource = (DataSource) envCtx.lookup("/jdbc/"+poolName);
} catch (NamingException ne){
// EJB was not deployed properly
throw new EJBException(ne);
}
}
public void unsetEntityContext(){
ctx = null;
}
public Integer ejbCreate(String name, int ssn, int grade){
this.name = name;
this.ssn = new Integer(ssn);
this.grade = grade;
Connection con = null;
PreparedStatement ps = null;
Statement stmt=null;
try {
con = dataSource.getConnection();
System.out.println("Connect successful");
ps = con.prepareStatement("insert into "+tableName+
" (name,ssn,grade) values (?,?,?)");
ps.setString(1,name);
ps.setInt(2,ssn);
ps.setInt(3,grade);
ps.executeUpdate();
//when use no-transaction sataSource add follow:
//con.commit();
return this.ssn;
} catch (SQLException sqe){
try {
ejbFindByPrimaryKey(this.ssn);
throw new DuplicateKeyException("A student with social "+
"security number: "+ssn+" already exists.");
} catch (Exception Ignore){}
System.out.println("Insert failed");
sqe.printStackTrace();
throw new EJBException(sqe);
} finally {
try {
if (ps != null) ps.close();
if (con != null) con.close();
} catch (Exception ignore) {}
}
}
public void ejbPostCreate(String name, int ssn, int grade) {}
public void ejbRemove() throws RemoveException{
Connection con = null;
PreparedStatement ps = null;
try {
con = dataSource.getConnection();
ps = con.prepareStatement("delete from "+tableName+
" where ssn=?");
ps.setInt(1, ssn.intValue());
if (ps.executeUpdate() < 1) {
throw new RemoveException ("Error removing Student with ssn: "+ssn);
}
} catch (SQLException sqe) {
throw new EJBException (sqe);
} finally{
try {
if(ps != null) ps.close();
if(con != null) con.close();
} catch (Exception ignore) {}
}
}
public void ejbLoad() {
ssn = (Integer) ctx.getPrimaryKey();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = dataSource.getConnection();
ps = con.prepareStatement("select name, grade from "
+tableName+ " where ssn=?");
ps.setInt(1, ssn.intValue());
ps.executeQuery();
rs = ps.getResultSet();
if (rs.next()) {
name = rs.getString(1);
grade = rs.getInt(2);
} else {
throw new NoSuchEntityException("Student with social "+
"security number: "+ssn+" no longer exists.");
}
} catch (SQLException sqe) {
throw new EJBException(sqe);
} finally {
try {
if (rs != null) rs.close();
if (ps != null) ps.close();
if (con != null) con.close();
} catch (Exception ignore) {}
}
}
public void ejbStore(){
Connection con = null;
PreparedStatement ps = null;
try {
con = dataSource.getConnection();
ps = con.prepareStatement("update "+tableName+
" SET name=?, grade=? where ssn=?");
ps.setString(1, name);
ps.setInt(2, grade);
ps.setInt(3, ssn.intValue());
ps.executeUpdate();
} catch (SQLException sqe) {
throw new EJBException(sqe);
} finally {
try {
if (ps != null) ps.close();
if (con != null) con.close();
} catch (Exception ignore) {}
}
}
public void ejbActivate() {}
public void ejbPassivate() {}
public Integer ejbFindByPrimaryKey(Integer pk) throws ObjectNotFoundException
{
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = dataSource.getConnection();
ps = con.prepareStatement("select ssn from "+tableName+
" where ssn=?");
ps.setInt(1, pk.intValue());
ps.executeQuery();
rs = ps.getResultSet();
if (rs.next()) {
return pk;
} else {
throw new ObjectNotFoundException ("Student with social "+
"security number: "+ssn+" no longer exists.");
}
} catch (SQLException sqe) {
throw new EJBException (sqe);
} finally {
try {
if(rs != null) rs.close();
if(ps != null) ps.close();
if(con != null) con.close();
} catch (Exception ignore) {}
}
}
public Collection ejbFindStudentsInGrade(int gradeValue) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
ArrayList keys = new ArrayList();
try {
con = dataSource.getConnection();
ps = con.prepareStatement("select ssn from "+tableName+
" where grade=?");
ps.setInt(1, gradeValue);
ps.executeQuery();
rs = ps.getResultSet();
while (rs.next()) {
keys.add(new Integer(rs.getInt(1)));
}
return keys;
} catch (SQLException sqe) {
throw new EJBException (sqe);
} finally {
try {
if(rs != null) rs.close();
if(ps != null) ps.close();
if(con != null) con.close();
} catch (Exception ignore) {}
}
}
public String getName() { return name; }
public Integer getSsn() { return ssn; }
public int getGrade() { return grade; }
public void setGrade(int grade) { this.grade = grade; }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -