Last Modified : 2010.03.02

¼¼¼Ç ºó°ú ¿£Æ¼Æ¼ ºóÀ» ÀÌ¿ëÇÑ °Ô½ÃÆÇ ¿¹Á¦

C:/ejb ¿öÅ©½ºÆäÀ̽º¿¡ »õ·Î¿î ÇÁ·ÎÁ§Æ® sboard2 ¸¦ »ý¼ºÇϰí, Add External JARs.. ¹öưÀ» ÀÌ¿ëÇÏ¿© weblogic.jar ¸¦ Build Path ¿¡ Ãß°¡ÇÕ´Ï´Ù.
¼¼¼Ç ºóÀ» ÀÌ¿ëÇÑ °Ô½ÃÆÇ sboard ÀÇ ÀÚ¹Ù ¼Ò½º¸¦ sboard2 ¿¡ º¹»çÇÏ¿© Ãß°¡ÇÕ´Ï´Ù.

¿£Æ¼Æ¼ ºó ÀÛ¼º

1. ¿ø°Ý ÀÎÅÍÆäÀ̽º ÀÛ¼º

Board.java

package kr.co.hanbitbook.ejb.examples.board;

import java.sql.Timestamp;

import javax.ejb.EJBLocalObject;

public interface Board extends EJBLocalObject {
	
	public Integer getSeq();
	
	public void setName(String name);
	public String getName();
	
	public void setPasswd(String passwd);
	public String getPasswd();
	
	public void setTitle(String title);
	public String getTitle();
	
	public void setContent(String content);
	public String getContent();
	
	public void setRegdate(Timestamp regdate);
	public Timestamp getRegdate();
	
	public void setReadcount(int readcount);
	public int getReadcount();

}
2. Ȩ ÀÎÅÍÆäÀ̽º ÀÛ¼º

BoardHome.java

package kr.co.hanbitbook.ejb.examples.board;

import java.sql.Timestamp;

import javax.ejb.CreateException;
import javax.ejb.EJBLocalHome;
import javax.ejb.FinderException;

public interface BoardHome extends EJBLocalHome {
	
	public Board create(
			String name,
			String passwd,
			String title,
			String content,
			Timestamp regdate,
			int readcount) throws CreateException;
	
	public Board findByPrimaryKey(Integer seq) throws FinderException;
	
}

3. ºó Ŭ·¡½º ÀÛ¼º

BoardBean.java

package kr.co.hanbitbook.ejb.examples.board;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.rmi.RemoteException;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class BoardBean implements EntityBean {

	private static final long serialVersionUID = -6689027768660964193L;
	EntityContext entityContext;
	Integer seq;
	String name;
	String passwd;
	String title;
	String content;
	Timestamp regdate;
	int readcount;
	DataSource ds;
	Context initial;
	
	public void setSeq(Integer seq) {
		this.seq = seq;
	}
	public Integer getSeq() {
		return seq;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	
	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}
	public String getPasswd() {
		return passwd;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}
	public String getTitle() {
		return title;
	}
	
	public void setContent(String content) {
		this.content = content;
	}
	public String getContent() {
		return content;
	}
	
	public void setRegdate(Timestamp regdate) {
		this.regdate = regdate;
	}
	public Timestamp getRegdate() {
		return regdate;
	}
	public void setReadcount(int readcount) {
		this.readcount = readcount;
	}
	public int getReadcount() {
		return readcount;
	}
	
	public Integer ejbCreate(
			String name,
			String passwd,
			String title,
			String content,
			Timestamp regdate,
			int readcount) throws CreateException {
		
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int iseq = 0;
		
		try {
			con = ds.getConnection();
			ps = con.prepareStatement("insert into sboard values (sboard_seq.NEXTVAL, ?, ?, ?, empty_clob(), ?, 0)");
			ps.setString(1, name);
			ps.setString(2, passwd);
			ps.setString(3, title);
			ps.setTimestamp(4, regdate);
			ps.executeUpdate();
			ps.close();
			
			ps = con.prepareStatement("select sboard_seq.CURRVAL from dual");
			rs = ps.executeQuery();
			rs.next();
			iseq = rs.getInt(1);
			ps = con.prepareStatement("select content from sboard where seq = ? for update");
			ps.setInt(1, iseq);
			rs = ps.executeQuery();
			rs.next();
			Clob clob_content = rs.getClob(1);
			Writer writer = clob_content.setCharacterStream(0L);
			StringReader sr = new StringReader(content);
			char[] buffer = new char[512];
			int readCount = 0;
			while ( (readCount = sr.read(buffer)) != -1 ) {
				writer.write(buffer, 0, readCount);
			}
			sr.close();
			writer.close();
		} catch (SQLException e) {
			System.out.println("ejbCreate() : " + e.toString());
		} catch (IOException e) {
			System.out.println("ejbCreate() : " + e.toString());
		} finally {
			if(rs != null) {
				try {
					rs.close();
				}catch(Exception e){}
			}
			if(ps != null) {
				try { 
					ps.close();
				}catch(Exception e){}
			}
			if(con != null) {
				try { 
					con.close(); 
				}catch(Exception e){}
			}
		}
		
		setSeq(new Integer(iseq));
		setName(name);
		setPasswd(passwd);
		setTitle(title);
		setContent(content);
		setRegdate(regdate);
		setReadcount(0);
		
		return new Integer(iseq);
	
	}
	
	public void ejbPostCreate(
			String name,
			String passwd,
			String title,
			String content,
			Timestamp regdate,
			int readcount) throws CreateException {
		
	}

	public Integer ejbFindByPrimaryKey(Integer seq) throws FinderException {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int iseq = 0;
		
		try {
			con = ds.getConnection();
			ps = con.prepareStatement("select seq from sboard where seq = ?");
			ps.setInt(1, seq.intValue());
			rs = ps.executeQuery();
			rs.next();
			iseq = rs.getInt(1);
			
		} catch (SQLException e) {
			System.out.println("ejbFindByPrimaryKey() : " + e.toString());
		} finally {
			if ( rs != null ) {
				try {
					rs.close();
				} catch (Exception e) {}
			}
			if ( ps != null ) {
				try {
					ps.close();
				} catch (Exception e) {}
			}
			if ( con != null ) {
				try {
					con.close();
				} catch ( Exception e) {}
			}
		}
		
		return new Integer(iseq);
	}
	
	public void ejbActivate() throws EJBException, RemoteException {
		try {
			initial = new InitialContext();
			Object obj = initial.lookup("ora9");
			ds = (DataSource)obj;
		} catch (NamingException e) {
			System.out.println("ejbActivate() : " + e.toString());
		}
	}

	public void ejbLoad() throws EJBException, RemoteException {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			Integer iseq = (Integer)entityContext.getPrimaryKey();
			con = ds.getConnection();
			ps = con.prepareStatement("selct seq, name, passwd, title, content, regdate, readcount from sboard where seq = ?");
			ps.setInt(1, iseq.intValue());
			rs = ps.executeQuery();
			if ( rs.next() ) {
				int seq = rs.getInt(1);
				String name = rs.getString(2);
				String passwd = rs.getString(3);
				String title = rs.getString(4);
				Clob clob_content = rs.getClob(5);
				Timestamp regdate = rs.getTimestamp(6);
				
				int readcount = rs.getInt(7);
				Reader reader = clob_content.getCharacterStream();
				StringWriter writer = new StringWriter();
				char[] buffer = new char[512];
				int readCount = 0;
				while ( (readCount = reader.read(buffer)) != -1 ) {
					writer.write(buffer, 0, readCount);
				}
				String content = writer.toString();
				this.seq = new Integer(seq);
				this.name = name;
				this.passwd = passwd;
				this.title = title;
				this.content = content;
				this.regdate = regdate;
				this.readcount = readcount;
			}
		} catch (Exception e) {
			System.out.println("ejbLoad() : " + e.toString());
		}

	}

	public void ejbPassivate() throws EJBException, RemoteException {
		initial = null;
		ds = null;
	}

	public void ejbRemove() throws RemoveException, EJBException,
			RemoteException {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ds.getConnection();
			ps = con.prepareStatement("delete from sboard where seq = ?");
			ps.setInt(1, seq.intValue());
			ps.executeUpdate();
		} catch (SQLException e) {
			System.out.println("ejbRemove() ¸Þ¼Òµå È£Ãâ½Ã ¿À·ù " + e.toString());
		} finally {
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {}
			}
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e) {}
			}
		}
		
	}

	public void ejbStore() throws EJBException, RemoteException {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			con = ds.getConnection();
			ps = con.prepareStatement("update sboard set name = ?, title = ?, content = empty_clob() where seq = ?");
			ps.setString(1, name);
			ps.setString(2, title);
			ps.setInt(3, seq.intValue());
			ps.executeUpdate();
			ps.close();
			
			ps = con.prepareStatement("select content from sboard where seq = ? for update");
			ps.setInt(1, seq.intValue());
			rs = ps.executeQuery();
			rs.next();
			Clob clob_content = rs.getClob(1);
			Writer writer = clob_content.setCharacterStream(0L);
			StringReader sr = new StringReader(content);
			char[] buffer = new char[512];
			int readCount = 0;
			while ((readCount = sr.read(buffer)) != -1) {
				writer.write(buffer, 0, readCount);
			}
			sr.close();
			writer.close();
		} catch (SQLException e) {
			System.out.println("ejbStore() : " + e.toString());
		} catch (IOException e) {
			System.out.println("ejbStore() : " + e.toString());
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {}
			}
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {}
			}
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e) {}
			}
		}
		
	}

	public void setEntityContext(EntityContext entityContext) throws EJBException,
			RemoteException {
		this.entityContext = entityContext;
		
		try {
			initial = new InitialContext();
			Object obj = initial.lookup("ora9");
			ds = (DataSource)obj;
		} catch (Exception e) {
			System.out.println("setEntityContext() : " + e.toString());
		}
		
	}

	public void unsetEntityContext() throws EJBException, RemoteException {
		entityContext = null;
	}

}

¼¼¼Ç ºó ¼öÁ¤

BoardManagerBean.java ¼¼¼Ç ºóÀ» ¼öÁ¤ÇÕ´Ï´Ù.

addBoardData() ¸Þ¼Òµå ¼öÁ¤
public void addBoardData(BoardData boardData) {
	try {
		Object obj = initial.lookup("Board");
		BoardHome home = (BoardHome) PortableRemoteObject.narrow(obj, BoardHome.class);
		home.create(boardData.getName(), boardData.getPasswd(), boardData.getTitle(), boardData.getContent(), boardData.getCurrentTimestamp(), boardData.getReadcount());
	} catch (Exception e) {
		System.out.println("addBoardData() :" + e.toString());
	}
}
deleteBoardData() ¸Þ¼Òµå ¼öÁ¤
public void deleteBoardData(int seq) {
	try {
		Object obj = initial.lookup("Board");
		BoardHome home = (BoardHome) PortableRemoteObject.narrow(obj, BoardHome.class);
		home.remove(new Integer(seq));
	} catch (Exception e) {
		System.out.println("deleteBoardData() :" + e.toString());
	}
}
updateBoardData() ¸Þ¼Òµå ¼öÁ¤
public void updateBoardData(BoardData boardData) {
	try {
		Object obj = initial.lookup("Board");
		BoardHome home = (BoardHome) PortableRemoteObject.narrow(obj, BoardHome.class);
		Board board = home.findByPrimaryKey(new Integer(boardData.getSeq()));
		board.setName(boardData.getName());
		board.setTitle(boardData.getTitle());
		board.setContent(boardData.getContent());
	} catch (Exception e) {
		System.out.println("updateBoardData() :" + e.toString());
	}
}
getBoardData() ¸Þ¼Òµå ¼öÁ¤
public BoardData getBoardData(int seq) {
	BoardData bd = new BoardData();
	try {
		Object obj = initial.lookup("Board");
		BoardHome home = (BoardHome) PortableRemoteObject.narrow(obj, BoardHome.class);
		Board board = home.findByPrimaryKey(new Integer(seq));
		bd.setSeq(board.getSeq().intValue());
		bd.setName(board.getName());
		bd.setTitle(board.getTitle());
		bd.setContent(board.getContent());
		bd.setRegdate(board.getRegdate());
		bd.setReadcount(board.getReadcount());
	} catch (Exception e) {
		System.out.println("getBoardData() :" + e.toString());
	}
	
	return bd;
}

¹èÄ¡ µð½ºÅ©¸³ÅÍ ÀÛ¼º(À¥·ÎÁ÷ ºô´õ ÀÌ¿ë), EJB jar ÆÄÀÏ »ý¼º, ¹èÄ¡, Å×½ºÆ®

À¥·ÎÁ÷ ºô´õ¸¦ ½ÇÇàÇϰí ÄÄÆÄÀÏµÈ °æ·ÎÀÎ C:\ejb\sboard\bin ¸¦ ¼±ÅÃÇÕ´Ï´Ù.
ÁÂÃø¸Þ´º¿¡¼­ BoardManagerBean ¸¦ ¼±ÅÃÇϰí General ÅÇ¿¡¼­ JNDI Name: Ç׸ñÀÇ °ªÀÌ BoardManager ·Î ¼öÁ¤ÇÕ´Ï´Ù.
BoardManager ¼¼¼Ç ºó General ÅÇ ¼³Á¤
Resources ¸Þ´ºÀÇ Resource References ÅÇ¿¡¼­ DataSource ¸¦ ¼³Á¤ÇÕ´Ï´Ù.
BoardManager ¼¼¼Ç ºóÀÇ Resource References ÅÇ ¼³Á¤
Advanced ÅÇ¿¡¼­ Enable call by reference ¸¦ üũÇÕ´Ï´Ù.
enable-call-by-reference True
ÁÂÃø¸Þ´º¿¡¼­ BoardBean ¸¦ ¼±ÅÃÇϰí General ÅÇ¿¡¼­ JNDI Name: Ç׸ñÀÇ °ªÀÌ Board ·Î ¼öÁ¤ÇÕ´Ï´Ù.
BoardBean ¿£Æ¼Æ¼ ºó General ÅÇ ¼³Á¤
¿£Æ¼Æ¼ ºóÀ» ±¸¼ºÇϴ Ŭ·¡½º°¡ ¿Ã¹Ù¸¥Áö È®ÀÎÇÕ´Ï´Ù.
Classes ÅÇ ¼³Á¤
ÁÖ Å° Ŭ·¡½º°¡ java.lang.Integer ·Î ¿Ã¹Ù¸£°Ô ¼³Á¤µÇ¾ú´ÂÁö È®ÀÎÇÕ´Ï´Ù.
General ÅÇ ¼³Á¤
Resources ¸Þ´ºÀÇ Resource References ÅÇ¿¡¼­ DataSource ¸¦ ¼³Á¤ÇÕ´Ï´Ù.
BoardBean ¿£Æ¼Æ¼ ºóÀÇ Resource References ÅÇ ¼³Á¤
Archive... ¸Þ´ºÀ» ÀÌ¿ëÇØ¼­ sboard.jar »ý¼ºÇÕ´Ï´Ù.
À¥·ÎÁ÷ Äֿܼ¡¼­ ¼¼¼Ç ºó¸¸À» ÀÌ¿ëÇß´ø sboard.jar ¸¦ Áö¿î ´ÙÀ½ C:/ejb/sboard2/sboard.jar ¸¦ ¹èÄ¡ÇÕ´Ï´Ù. http://localhost:7001/sboard/list.jsp ¸¦ ¹æ¹®ÇÏ¿© Å×½ºÆ®ÇÕ´Ï´Ù.

Âü°í ¹®¼­

¾Ë±â½±°Ô Ç®¾î ¾´ À¥·ÎÁ÷°ú EJB 12Àå -ÇѺû¹Ìµð¾î,±è¼º¹Ú Àú-
¿¹Á¦¿¡ ¾²¿´´ø jar,war,ear ÆÄÀÏ (¹èÄ¡½Ã ÀÍ¼Ê¼Ç ¹ß»ýÇÒ ¶§ Âü°í¿ë)