Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can I use EJBQL for BMP?  RSS feed

 
Murali Mohan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I use EJBQL for BMP?
Thanks,
Murali
 
Sergiu Truta
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EJB-QL is made for usage inside CMP.
Inside BMP/Session Beans you have to use plain SQL. You have to get the Datasource from the container through JDBC and than get a connection and use statements. Here's some code snippet I use from a Stateless Session Bean with JBoss:
public CachedRowSet getAllUsers() throws BackEndException {
try{
con = getConnection();
//load data from the database
PreparedStatement pstm = con.prepareStatement("SELECT * FROM USERS");
CachedRowSet cache = new CachedRowSet();
cache.populate(pstm.executeQuery());
cache.beforeFirst();
pstm.close();
con.close();
return cache;
}catch (SQLException sqe) {
System.out.println("SQLException: " + sqe);
throw new BackEndException(BackEndErrors.GENERIC_SQLQUERY_ERROR + this.getClass());
}
}
private Connection getConnection() {
try{
InitialContext context = new InitialContext();
DataSource ds = (DataSource) context.lookup("java:/OracleDS");
return ds.getConnection();
}catch(Exception ex){
System.out.println("Error getting the Connection to the database!");
}
return null;
}
Hope this helps.
Cheers
 
Vishwa Kumba
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To reassert sergiu's statement, EJBQL cannot be used in BMP.
It can only be used for CMP entity beans.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!