gpt4 book ai didi

getConnection() 上的 java.util.NoSuchElementException

转载 作者:行者123 更新时间:2023-11-28 22:30:54 25 4
gpt4 key购买 nike

我有以下代码:

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();
// ...

另见 JNDI Datasource HOW-TO

关于getConnection() 上的 java.util.NoSuchElementException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19326168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com