[英]unable to connect database in Servlet JDBC Connection Pool using META-INF/context.xml file
我正在尝试使用context.xml文件使用Servlet连接mysql数据库。 通过参考网站上提供的示例:
http://viralpatel.net/blogs/2009/09/database-connection-pooling-tomcat-eclipse-db.html
但是我收到异常:javax.naming.NameNotFoundException:jdbc未绑定
之后,我在web.xml中添加了标记,然后它也没有起作用,并给出了另一个异常,例如:
部署期间发生错误; -嵌套的throwable:(javax.naming.NamingException:resource-ref:jdbc / mytest没有有效的JNDI绑定。请检查jboss-web / resource-ref。)
(注意:mytest是我的数据库名称)
其他详细信息:我为引用 context.xml 添加了我的配置详细信息 :
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<!-- Specify a JDBC datasource -->
<Resource name="jdbc/mytest" auth="Container"
type="javax.sql.DataSource" username="root" password="mysql"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mytest"
maxActive="10" maxIdle="4" />
</Context>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>testjdbc</display-name>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mytest</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<servlet>
<description></description>
<display-name>TestServlet</display-name>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.mytest.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>
</web-app>
servlet.java:
public void init() throws ServletException
{
try
{
// Get DataSource
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/jdbc/mytest");
dataSource = (DataSource)envContext.lookup("jdbc/mytest");
}
catch (NamingException e)
{
e.printStackTrace();
}
}
您提到的教程是针对Tomcat的,但是您现在正在使用jboss-web。
每个服务器配置JNDI数据源的方式可能略有不同。有关jboss-web的信息,请参考this 。
当在context.xml中使用name =“ jdbc:testdb”定义数据源资源时,您必须在InitialContext.lookUp()方法中使用相同的名称进行查找。 数据库名称(mytest)应在连接URL中给出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.