尝试使用tomcat池连接数据库。
这是我的代码:
的web.xml
<resource-ref>
<description>Resource pour le pool de connexion vers tomcat</description>
<res-ref-name>jdbc/pool</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
context.xml:
<Context antiJARLocking="true" path="/CompteBancaire">
<Resource name="jdbc/pool" auth="container" type="javax.sql.Datasource">
<ResourceParams name="jdbc/pool">
<parameter>
<name>username</name>
<value>marc</value>
</parameter>
<parameter>
<name>password</name>
<name>marc</name>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:oracle:thin:@localhost:1521:xe</value>
</parameter>
<parameter>
<name>factory</name>
<value>org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory</value>
</parameter>
</ResourceParams>
</Resource>
</Context>
在我的DAO类的构造函数中:
DataSource ds;
//récupération de la source de donnée
Context initCtx = new InitialContext();
ds = (DataSource) initCtx.lookup("java:comp/env/jdbc/pool");
conn = ds.getConnection();
我有一个例子试图查找^并创建数据源:
GRAVE: null
javax.naming.NamingException: Cannot create resource instance
at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:143)
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:304)
at org.apache.naming.NamingContext.lookup(NamingContext.java:793)
at org.apache.naming.NamingContext.lookup(NamingContext.java:140)
at org.apache.naming.NamingContext.lookup(NamingContext.java:781)
at org.apache.naming.NamingContext.lookup(NamingContext.java:140)
at org.apache.naming.NamingContext.lookup(NamingContext.java:781)
at org.apache.naming.NamingContext.lookup(NamingContext.java:140)
at org.apache.naming.NamingContext.lookup(NamingContext.java:781)
at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:152)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at cb.model.dao.BankDAONatural.<init>(BankDAONatural.java:35)
at cb.model.dao.BankDAONatural.getInstance(BankDAONatural.java:42)
at cb.model.dao.DAOFactory.<init>(DAOFactory.java:31)
at cb.model.dao.DAOFactory.getInstance(DAOFactory.java:37)
at cb.control.generalDispatcher.init(generalDispatcher.java:33)
at javax.servlet.GenericServlet.init(GenericServlet.java:212)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1173)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:809)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)
谢谢。这可能是我没有得到的关于数据源的东西