gpt4 book ai didi

mysql - javax.naming.NoInitialContextException Tomcat 数据源 JNDI intellij idea 14

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

我正在尝试在 ntellij idea 14 中使用 Tomcat 数据源和 MySQL 获取连接池。我已经完成了这些步骤:

Select "Project Structure" from the File menu

From there, select the "Facets" option. Make sure that you have a Web facet configured. If not, add one.

Once the web facet is added, select "Add Application Server specific descriptor..."

Select Tomcat Context Descriptor from the options and click OK.

我在 web 目录中得到了 META-INF/context.xml。我在 context.xml 中添加了下一行

<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/payments"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/paymentsdb"
username="root"
password="password"

maxActive="100"
maxIdle="20"
minIdle="5"
maxWait="10000"/>
</Context>

并将其放入 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--DB Connection start-->
<description>MySQL Test App</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/payments</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>

这是我获取连接的方法

public static Connection createConnection(){
Context context = null;
DataSource dataSource = null;
Connection connection = null;
try {
context = (Context) new InitialContext().lookup("java:comp/env");
dataSource = (DataSource) context.lookup("jdbc/payments");
connection = dataSource.getConnection();
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}

我得到了这个异常

dataSource = (DataSource) context.lookup("java:comp/env/jdbc/payments");

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at ua.epam.kuts.dao.MySQLDAOFactory.createConnection(MySQLDAOFactory.java:22)
at Test.main(Test.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

我使用 Tomcat 8.0.23。任何想法?我在谷歌上搜索了几个小时,但没有找到任何对我有帮助的东西。把我气死了。我检查了与 Statment 的连接并且它有效。我有 connector.jar。

最佳答案

我犯了一个愚蠢的错误。我试图测试在 public static main(String[] args) 方法中运行它的 DataSource 连接,而不是在服务器上运行它。这就是 InitialContext 没有被初始化的原因。找到答案 here在第二篇文章中。

关于mysql - javax.naming.NoInitialContextException Tomcat 数据源 JNDI intellij idea 14,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31033365/

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