@Override
protected ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
DataSource dataSource = (DataSource) new InitialContext().lookup("java:jboss/STRUTS-DS");
dataSource.getConnection();
SessionBean sessionBean = new SessionBean();
sessionBean.testConnection();
return mapping.findForward("list");
}
在行動
,可與查詢,發生在SessionBean
如何注入依賴於數據源與EJB3和註釋
@Stateless
public class SessionBean {
@PersistenceContext(???)
EntityManager entityManager;
@Resource(???)
DataSource dataSource;
public void testConnection() {
PreparedStatement preparedStatement = null;
Connection connection = null;
try {
connection = dataSource.getConnection();
preparedStatement = connection.prepareStatement("drop table test");
preparedStatement.execute();
preparedStatement = connection.prepareStatement("CREATE TABLE example (id INT,data VARCHAR(100));");
preparedStatement.execute();
System.out.println("Done");
} catch (SQLException sqlE) {
throw new EJBException(sqlE);
} finally {
try {
if (preparedStatement != null) {
preparedStatement.close();
}
} catch (Exception e) {}
try {
if (connection != null) {
connection.close();
}
} catch (Exception e) {}
}
}
}
我試圖注入了這個問題。 在我的數據源從未注入,我放在資源?
遺憾的是:Lorem存有悲坐阿梅德,consectetuer adipiscing ELIT,sed的直徑nonummy NIBH euismod tincidunt UT laoreet dolore麥格納aliquam ERAT volutpat。
如果你正在實例化你的'SessionBean',你不能使用任何注入 - 爲此,它必須由EJB容器創建。 – andersschuller
你說得對,問題是我不能使用註釋EJB和Inject in Action [錯誤:註釋EJB不允許用於此位置] – arr90rj