我有以下代码:
Context ctx = new InitialContext(); // Set the initial context
DataSource dataSource = (DataSource) ctx.lookup("java:comp/env/" + serverURL);
conn = dataSource.getConnection();
但是,conn = dataSource.getConnection();
行抛出 java.util.NoSuchElementException
。我对此有点困惑。这是否意味着我的服务器 URL 不正确?这是我的 context.xml:
<Context>
<Resource name=serverURL auth="Container" type="javax.sql.DataSource"
driverClassName="com.ibm.db2.jcc.DB2Driver" url="jdbc:db2://"
username="" password="" maxActive="100" maxIdle="30" maxWait="10000" />
</Context>
这是我的 web.xml:
<resource-ref>
<res-ref-name>serverURL</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Application</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
谁知道怎么回事?
最佳答案
试试吧-
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup(serverURL);
Connection conn = ds.getConnection();
// ...
关于getConnection() 上的 java.util.NoSuchElementException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19326168/