[英]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.