org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:780)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:540)
at test.TestDB.doGet(TestDB.java:29)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:407)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:738)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:526)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NullPointerException
at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(JdbcOdbcDriver.java:507)
at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(JdbcOdbcDriver.java:476)
at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(JdbcOdbcDriver.java:307)
at java.sql.DriverManager.getDriver(DriverManager.java:253)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:773)
... 19 more
Не могу вытащить подключение к БД из контекста Tomcat.
Код:
Код:
Код:
public class TestDB extends HttpServlet {
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
DataSource ds = null;
try {
Context initContext = new InitialContext();
Context envContext = null;
envContext = (Context) initContext.lookup("java:/comp/env");
ds = (DataSource) envContext.lookup("library");
} catch (NamingException e) {
e.printStackTrace();
}
try {
Connection conn = ds.getConnection();
...
} catch (SQLException e) {
e.printStackTrace();
}
}
}
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
DataSource ds = null;
try {
Context initContext = new InitialContext();
Context envContext = null;
envContext = (Context) initContext.lookup("java:/comp/env");
ds = (DataSource) envContext.lookup("library");
} catch (NamingException e) {
e.printStackTrace();
}
try {
Connection conn = ds.getConnection();
...
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Проверил отладчиком: в envContext (в его HashMap) есть объект по ключу "library". ds - не пустой. Однако все поля ds - либо null, либо 0.
В чем проблема - понять не могу :(
Контекст:
Код:
<Context path="/">
<Resource name="library" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="sudo" password="XXX" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/libdb?autoReconnect=true"/>
</Context>
<Resource name="library" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="sudo" password="XXX" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/libdb?autoReconnect=true"/>
</Context>
Создал новый проект.
В server.xml не суюсь.
В context.xml прописал:
Код:
<Context path="/">
<Resource name="jndi/test" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="XXX" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/?autoReconnect=true"/>
</Context>
<Resource name="jndi/test" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="XXX" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/?autoReconnect=true"/>
</Context>
В web.xml:
Код:
<resource-ref>
<res-ref-name>jndi/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<res-ref-name>jndi/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
В коде сервлета:
Код:
public class Test extends HttpServlet {
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
try {
Context initContext = new InitialContext();
DataSource ds = (DataSource) initContext.lookup("java:/comp/env/jndi/test");
Connection c = ds.getConnection();
Statement s = c.createStatement();
s.execute("USE LIB");
ServletOutputStream sos = httpServletResponse.getOutputStream();
sos.println("ALL OK");
sos.flush();
sos.close();
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
try {
Context initContext = new InitialContext();
DataSource ds = (DataSource) initContext.lookup("java:/comp/env/jndi/test");
Connection c = ds.getConnection();
Statement s = c.createStatement();
s.execute("USE LIB");
ServletOutputStream sos = httpServletResponse.getOutputStream();
sos.println("ALL OK");
sos.flush();
sos.close();
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
И - чудеса на постном масле: все работает.
Отличий между способом подключения в исходном проекте и в тестовом не нашел.
Почему в исходном проекте не коннектит - не знаю.
Придется пересобирать заново :(