¿£Æ¼Æ¼ ºó(BMP)
¿öÅ©½ºÆäÀ̽º¸¦ C:/ejb ·Î ¼±ÅÃÇϰí ÀÌŬ¸³½º¸¦ ½ÇÇàÇÕ´Ï´Ù.
student ¶ó´Â »õ·Î¿î ÇÁ·ÎÁ§Æ®¸¦ »ý¼ºÇÕ´Ï´Ù.
student ÇÁ·ÎÁ§Æ®¿¡ weblogic.jar ¸¦ Add External JARs.. ¹öưÀ» ÀÌ¿ëÇÏ¿© Ãß°¡ÇÕ´Ï´Ù.
studenttable Å×À̺íÀ» »ý¼ºÇÕ´Ï´Ù.
create table studenttable ( ssn number primary key, name varchar2(255), grade number );
¿ø°Ý ÀÎÅÍÆäÀ̽º ÀÛ¼º
Student.java
package kr.co.hanbitbook.ejb.examples.student;
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
public interface Student extends EJBObject {
public Integer getSsn() throws RemoteException;
public String getName() throws RemoteException;
public int getGrade() throws RemoteException;
public void setGrade(int grade) throws RemoteException;
}
Ȩ ÀÎÅÍÆäÀ̽º ÀÛ¼º
StudentHome.java
package kr.co.hanbitbook.ejb.examples.student;
import java.rmi.RemoteException;
import java.util.Collection;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
import javax.ejb.FinderException;
public interface StudentHome extends EJBHome {
public Student create(String name, int ssn, int grade)
throws CreateException, RemoteException;
public Student findByPrimaryKey(Integer ssn)
throws FinderException, RemoteException;
public Collection findStudentsInGrade(int grade)
throws FinderException, RemoteException;
}
ºó Ŭ·¡½º ÀÛ¼º
ÀÌŬ¸³½ºÀÇ ¼Ò½º ÀÛ¼º½Ã¿¡ »ç¿ëÇÏ´Â ±âº» ±â´É°ú ¿¡µðÅÍ ºä¿¡¼ ÄÁÅØ½ºÆ® ¸Þ´º(Source - Override/Implement Methods ... )
¸¦ ÀÌ¿ëÇÏ¸é ¾Æ·¡¿Í °°Àº ¼Ò½º¸¦ ±¸ÇÒ ¼ö ÀÖ½À´Ï´Ù.
StudentBean.java
package kr.co.hanbitbook.ejb.examples.student;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.RemoveException;
public class StudentBean implements EntityBean {
/**
*
*/
private static final long serialVersionUID = -4434920310680456336L;
public void ejbActivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
public void ejbLoad() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
public void ejbPassivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
public void ejbRemove() throws RemoveException, EJBException,
RemoteException {
// TODO Auto-generated method stub
}
public void ejbStore() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
public void setEntityContext(EntityContext arg0) throws EJBException,
RemoteException {
// TODO Auto-generated method stub
}
public void unsetEntityContext() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
}
Çʵ带 Ãß°¡ÇÕ´Ï´Ù.
StudentBean.java
private Integer ssn; private String name; private int grade;
setEntityContext ¿Í unsetEntityContext ¸Þ¼Òµå¸¦ ¾Æ·¡¿Í °°ÀÌ ¼öÁ¤ÇÕ´Ï´Ù.
¸ÕÀú EntityContext ¿Í DataSource Çʵ带 Ãß°¡ÇÕ´Ï´Ù.
StudentBean.java
private EntityContext ctx;
private DataSource dataSource;
public void setEntityContext(EntityContext c) throws EJBException,
RemoteException {
ctx = c;
try {
Context envCtx = new InitialContext();
dataSource = (DataSource) envCtx.lookup("ora9");
} catch (NamingException e) {
throw new EJBException(e);
}
}
public void unsetEntityContext() throws EJBException, RemoteException {
ctx = null;
}
ejbCreate, ejbPostCreate ¿Í ejbFindByPrimaryKey ¸Þ¼Òµå¸¦ ¾Æ·¡¿Í °°ÀÌ Ãß°¡ÇÕ´Ï´Ù.
StudentBean.java
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;
try {
con = dataSource.getConnection();
ps = con.prepareStatement("insert into " + tableName + " (name, ssn, grade) values (?, ?, ?)");
ps.setString(1, name);
ps.setInt(2, ssn);
ps.setInt(3, grade);
ps.executeUpdate();
return this.ssn;
} catch (SQLException e) {
try {
ejbFindByPrimaryKey(this.ssn);
throw new DuplicateKeyException("A student with social " + "security number: " + ssn+" already exists.");
} catch (Exception e1) {}
e.printStackTrace();
throw new EJBException(e);
} finally {
try {
if ( ps != null ) ps.close();
if ( con != null ) con.close();
} catch (Exception e) {}
}
}
public void ejbPostCreate(String name, int ssn, int grade) {}
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 e) {
throw new EJBException(e);
} finally {
try {
if ( rs != null ) rs.close();
if ( ps != null ) ps.close();
if ( con != null ) con.close();
} catch ( Exception e) {}
}
}
ejbLoad ¸Þ¼Òµå¸¦ ¾Æ·¡¿Í °°ÀÌ ¼öÁ¤ÇÕ´Ï´Ù.
StudentBean.java
public void ejbLoad() throws EJBException, RemoteException {
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 e) {
throw new EJBException(e);
} finally {
try {
if ( rs != null ) rs.close();
if ( ps != null ) ps.close();
if ( con != null ) con.close();
} catch (Exception e) {
}
}
}
ejbStore ¸Þ¼Òµå¸¦ ¾Æ·¡¿Í °°ÀÌ ¼öÁ¤ÇÕ´Ï´Ù.
StudentBean.java
public void ejbStore() throws EJBException, RemoteException {
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 e) {
throw new EJBException(e);
} finally {
try {
if ( ps != null ) ps.close();
if ( con != null ) con.close();
} catch (Exception e) {}
}
}
ejbRemove ¸Þ¼Òµå¸¦ ¾Æ·¡¿Í °°ÀÌ ¼öÁ¤ÇÕ´Ï´Ù.
StudentBean.java
public void ejbRemove() throws RemoveException, EJBException,
RemoteException {
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 e) {
throw new EJBException(e);
} finally {
try {
if ( ps != null ) ps.close();
if ( con != null ) con.close();
} catch (Exception e) {}
}
}
Ȩ ÀÎÅÍÆäÀ̽º¿¡¼ Á¤ÀÇÇÑ ejbFindStudentsInGrade ¸Þ¼Òµå¸¦ ¾Æ·¡¿Í °°ÀÌ Ãß°¡ÇÕ´Ï´Ù.
StudentBean.java
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 e) {
throw new EJBException(e);
} finally {
try {
if ( rs != null ) rs.close();
if ( ps != null ) ps.close();
if ( con != null ) con.close();
} catch (Exception e) {}
}
}
¿ø°Ý ÀÎÅÍÆäÀ̽º¿¡¼ Á¤ÀÇÇÑ ¸Þ¼ÒµåµéÀ» ¾Æ·¡¿Í °°ÀÌ Ãß°¡ÇÕ´Ï´Ù.
StudentBean.java
public String getName() {
return name;
}
public Integer getSsn() {
return ssn;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
¹èÄ¡ µð½ºÅ©¸³ÅÍ ÀÛ¼º(À¥·ÎÁ÷ ºô´õ ÀÌ¿ë) ¹× ¹èÄ¡
À¥·ÎÁ÷ ºô´õ¸¦ ½ÇÇàÇϰí Open ¹öưÀ» ÀÌ¿ëÇØ¼ student ÇÁ·ÎÁ§Æ®ÀÇ bin Æú´õ¸¦ ¼±ÅÃÇÕ´Ï´Ù.
À̾îÁö´Â ȸ鿡¼ ´ÙÀ½°ú °°ÀÌ ¼³Á¤µÇ¾î ÀÖ´ÂÁö È®ÀÎÇÕ´Ï´Ù.

Resource Ç׸ñÀ» ¼±ÅÃÇÑ ÈÄ Resource Reference ¿¡¼ µ¥ÀÌÅÍ ¼Ò½º¿Í ¸®¼Ò½º ·¹ÆÛ·±½º¸¦ Ãß°¡ÇÕ´Ï´Ù.


Save ¹öưÀ» Ŭ¸¯ÇÏ¿© ÀúÀåÇÕ´Ï´Ù.
Archive...¸Þ´º¸¦ ÀÌ¿ëÇØ¼ student.jar ÆÄÀÏÀ» »ý¼ºÇÑ ´ÙÀ½ À¥·ÎÁ÷ Äֿܼ¡¼ ¹èÄ¡ÇÕ´Ï´Ù.
Å×½ºÆ®
Å×½ºÆ®¸¦ À§ÇÑ »õ·Î¿î ÇÁ·ÎÁ§Æ®¸¦ student_client ¶õ À̸§À¸·Î »ý¼ºÇÕ´Ï´Ù
¹èÄ¡½Ã »ç¿ëÇß´ø student.jar ¿Í weblogic.jar ¸¦ Add External JARs... ¹öưÀ» ÀÌ¿ëÇØ¼ video_client ÇÁ·ÎÁ§Æ®¿¡ µî·ÏÇÕ´Ï´Ù.
´ÙÀ½°ú °°ÀÌ Å¬¶óÀÌ¾ðÆ® ÆÄÀÏÀ» ÀÛ¼ºÇÕ´Ï´Ù.
StudentClient.java
import javax.rmi.*;
import javax.naming.*;
import kr.co.hanbitbook.ejb.examples.student.*;
public class StudentClient{
public static void main(String args[]){
Context ctx = null;
try{
java.util.Properties p = new java.util.Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
p.put(Context.PROVIDER_URL, "t3://localhost:7001");
ctx = new InitialContext(p);
Object h = ctx.lookup("StudentBean");
StudentHome home = (StudentHome)PortableRemoteObject.narrow(h, StudentHome.class);
Student student = home.create("È«±æµ¿", 10, 90);
System.out.println("ÀúÀåµÇ¾ú½À´Ï´Ù.");
}catch(Exception e){
System.out.println(e.toString());
}
}
}
½ÇÇàÇÑ ÈÄ¿¡ SQL*PLUS·Î È®ÀÎÇÕ´Ï´Ù.
Âü°í ¹®¼
¾Ë±â½±°Ô Ç®¾î ¾´ À¥·ÎÁ÷°ú EJB 9Àå -ÇѺû¹Ìµð¾î,±è¼º¹Ú Àú-
¿¹Á¦¿¡ ¾²¿´´ø jar,war,ear ÆÄÀÏ (¹èÄ¡½Ã ÀÍ¼Ê¼Ç ¹ß»ýÇÒ ¶§ Âü°í¿ë)
- ´ÙÀ½ : CMP(EJB-QL ÀÌ¿ë)
- ÀÌÀü : ¿£Æ¼Æ¼ ºó(CMP)
