package friends; import java.util.*; import java.sql.*; import javax.sql.*; import java.rmi.*; import javax.rmi.*; import javax.ejb.*; import javax.naming.*; public class MyFriendsBean implements SessionBean { public HashMap[] getMyFriends( String type ) { String query = "SELECT NAME, DESCRIPTION FROM MYFRIENDS WHERE TYPE='" + type + "'"; return getHashArrayQuery( query ); } protected HashMap[] getHashArrayQuery( String query ) { HashMap[] hmArray = null; try { Connection con = this.getConnection(); PreparedStatement ps = con.prepareStatement( query ); ResultSet rs = ps.executeQuery(); ResultSetMetaData rsmd = rs.getMetaData(); int columnNum = rsmd.getColumnCount(); hmArray = new HashMap[ rs.getFetchSize() + 1 ]; int hmIdx = 0; while ( rs.next() ) { HashMap hm = new HashMap(); // Ä÷³ À̸§À¸·Î ÇØ½¬¸ÊÀ» ¸¸µì´Ï´Ù. for ( int i = 1; i <= columnNum; i++ ) if ( rs.getString( i ) != null ) hm.put( rsmd.getColumnName( i ), rs.getString( i ) ); hmArray[ hmIdx ] = hm; haIdx++; } rs.close(); ps.close(); con.close(); } catch ( Exception e ) { e.printStackTrace(); } return hmArray; } protected Connection getConnection() { Connection con = null; try { InitialContext ctx = new InitialContext(); // resources.xml ¿¡ ¹Ì¸® ALLBLOG ¶ó´Â À̸§À¸·Î // DB Resource ¸¦ ÁöÁ¤ÇØ ³õ°í JNDI ·Î ¿¬°áÇÕ´Ï´Ù DataSource ds = (DataSource) ctx.lookup("ALLBLOG"); con = ds.getConnection(); } catch ( Exception e ) { e.printStackTrace(); } return con; } }