[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[jfriends-ml 11180] EJB-SQL と Dynamic Query (2/3)
EJB-SQL と Dynamic Query (1/3) の続きです。
------------------------------------------------------------------------------
package bookejb;
import javax.ejb.*;
import java.util.*;
import java.rmi.*;
public interface BookRemoteHome extends javax.ejb.EJBHome {
public BookRemote create(Integer id, String isbn, String title)
throws CreateException, RemoteException;
public BookRemote findByPrimaryKey(Integer id)
throws FinderException, RemoteException;
public Collection execDynaSQL(String sql)
throws FinderException, RemoteException;
}
------------------------------------------------------------------------------
------------------------------------------------------------------------------
package bookejb;
import javax.ejb.*;
import java.util.*;
import java.rmi.*;
public interface BookRemote extends javax.ejb.EJBObject {
public Integer getId() throws RemoteException;
public void setIsbn(String isbn) throws RemoteException;
public String getIsbn() throws RemoteException;
public void setTitle(String title) throws RemoteException;
public String getTitle() throws RemoteException;
}
------------------------------------------------------------------------------
------------------------------------------------------------------------------
package bookejb;
import java.util.*;
import java.rmi.*;
import java.sql.*;
import javax.ejb.*;
import javax.naming.*;
import javax.rmi.*;
import javax.sql.*;
abstract public class BookBean implements EntityBean {
EntityContext entityContext;
public Integer ejbCreate(Integer id, String isbn, String title)
throws CreateException {
setId(id);
setIsbn(isbn);
setTitle(title);
return null;
}
public void ejbPostCreate(Integer id, String isbn, String title)
throws CreateException {
}
public abstract void setId(java.lang.Integer id);
public abstract void setIsbn(java.lang.String isbn);
public abstract void setTitle(java.lang.String title);
public abstract java.lang.Integer getId();
public abstract java.lang.String getIsbn();
public abstract java.lang.String getTitle();
public void ejbLoad() {
}
public void ejbRemove() throws RemoveException {
}
public void ejbStore() {
}
public void ejbActivate() {
}
public void ejbPassivate() {
}
public void unsetEntityContext() {
this.entityContext = null;
}
public void setEntityContext(EntityContext entityContext) {
this.entityContext = entityContext;
}
/**
* find by Dynamic SQL
* @param sql User defined sql statement
* @return Collection of BookRemote EJBHandle
* @throws FinderException
*/
public java.util.Collection ejbHomeExecDynaSQL(java.lang.String sql)
throws FinderException {
ArrayList array = new ArrayList();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Context context = new InitialContext();
Object bookhomeref = context.lookup("BookRemote");
BookRemoteHome bookRemoteHome =
(BookRemoteHome)PortableRemoteObject.narrow(bookhomeref,
BookRemoteHome.class);
DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/MySQL");
try {
conn = ds.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next())
{
int id = rs.getInt("ID");
BookRemote book = bookRemoteHome.findByPrimaryKey(new Integer(id));
array.add(book.getHandle());
}
}
finally {
if( rs != null ) {
try { rs.close(); } catch (SQLException ex) {}
}
if( stmt != null ) {
try { stmt.close(); } catch (SQLException ex) {}
}
if( conn != null ) {
try { conn.close(); } catch (SQLException ex) {}
}
}
}
catch(Exception ex) {
throw new FinderException(ex.getMessage());
}
return array;
}
}
------------------------------------------------------------------------------
--
高橋智宏