- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 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/
getConnection 异常 javax.naming.NoInitialContextException:需要在环境或系统属性中指定类名,或者作为小程序参数,或者在应用程序资源文件中指定类名:j
我正在为我的 EJB 编写一个客户端,在尝试执行它时,我得到以下异常: javax.naming.NoInitialContextException: Need to specify class na
我一直试图在这里找到解决方案,但我不能...我有以下代码,但出现此错误。 我错过了什么吗?谢谢:) 代码 package src; import java.sql.DriverManager; imp
这个问题在这里已经有了答案: The meaning of NoInitialContextException error (13 个答案) 关闭 6 年前。 这是我的代码: import java
有人可以告诉我为什么会出现以下错误吗? javax.naming.NoInitialContextException: Need to specify class name in environmen
我们拥有在 Servicemix 5 上完美运行的 osgi 应用程序。但由于某些原因,我们必须迁移到 Servicemix 4.5。我尝试了一下,得到了这个 Error parsing SQL Ma
我需要将 java 应用程序连接到 MBean 服务器,但我在使 JMXConnector 工作时遇到问题。 当我尝试运行此命令时,出现 NoInitialContextException: try
对于学校,他们让我们通过普通的旧 dao 和 tomcat 连接到 postgresDB。但是,给定的代码无法正常工作,我现在已经被困在这里很长一段时间了。 就这样吧。 给定的connectionda
我有一个使用 JNDI 的应用程序,首先它可以工作,在我创建一个新项目后,由于与 SVN 冲突,它现在抛出 NoInitialContextException。 这是它抛出的异常: NoInitial
我在 Tomcat 服务器上有一个带有 Java Servlet 的网络服务。在我的 Servlet 中,我使用这样的数据库池: envContext = new InitialContext();
这个问题在这里已经有了答案: The meaning of NoInitialContextException error (13 个答案) 关闭 9 年前。 如标题所示,“javax.naming
我正在使用 Apache Commons 电子邮件 API 尝试以下代码,它在上下文中抛出错误。有什么帮助吗? 导入java.util.Properties; 导入 javax.mail.Passwo
我是 Hibernate 新手。使用 Eclipse indigo 中的 Hibernate 3.0。 这里讨论了这个话题,答案没有帮助,Hibernate: javax.naming.NoIniti
尝试连接到 MySQL 数据库 MysqlDataSource mysqlDs = new MysqlDataSource(); Properties prop = new Properties();
我正在尝试使用 JNDI 连接 mysql。但它显示异常 javax.naming.NoInitialContextException:需要在环境或系统属性中指定类名,或者作为小程序参数,或者在应用程
我想在 Jetty 6 中使用连接池,但到目前为止它给我带来了很多麻烦。 我愿意 InitialContext context = new InitialContext(); 它没有抛出异常,但是当我
我目前面临的问题是,我将 api 打包为 WAR(使用 eclipse + maven)后无法在 Jetty 上启动。从 Eclipse 启动时工作正常。 我查看了 Jetty 的日志并发现: org
我正在尝试编写一个 JMS 应用程序,我使用 glassFish 管理页面构建了一个 ConnectionFactory 和 Qeueu,我想知道如何让我的应用程序知道服务器上构建的 jndi 以便能
我正在使用 cargo maven 插件将我的 .war 部署到 Tomcat 8 实例以进行集成测试。该应用程序是一个基于 jersey 的 REST web 界面。我的问题出现在底层数据库连接池中
我有一个 JSF 网络应用程序,我想使用嵌入式 tomcat 运行它。到目前为止它一直在工作 [包括 JDBCRealm,在以下代码片段的 context.xml 中指定],除了登录后我的代码无法实际
我是一名优秀的程序员,十分优秀!