Hello
I'm using Tomcat 6.0.26 on Windows 7 and want to establish database
access with jdbc to a mysql-DB. There is another servlet project running
in the same tomcat using the same
DB Connection and the access for this project runs fine. So I thought
that it's good to access the db like in the working servlet project step
by step, but when doing this
I get:
org.apache.tomcat.dbcp.dbcp.SQLNestedException:Hello
I'm using Tomcat 6.0.26 on Windows 7 and want to establish database
access with jdbc to a mysql-DB. There is another servlet project running
on tomcat using the same
DB Connection and the access for this project runs fine. So I thought
that it's good to access the db like in the working servlet project step
by step, but when doing this
I get:
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:1150)
at
org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
at
com.inzeit.servlet.LoginServlet2.checkCredentials(LoginServlet2.java:45)
at com.inzeit.servlet.LoginServlet2.doGet(LoginServlet2.java:82)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
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:852)
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: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:1143)
... 17 more
This is a part of the $TOMCAT_HOME/conf/server.xml
<!-- <Context docBase="C:\apache-tomcat-6.0.26\wtpwebapps\InzStartPage"
path="/InzStartPage" reloadable="true"
source="org.eclipse.jst.jee.server:InzStartPage"> -->
<Context docBase="C:\apache-tomcat-6.0.26\wtpwebapps\IZServlets"
path="/IZServlets" reloadable="true"
source="org.eclipse.jst.jee.server:IZServlets">
<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver"
maxActive="30" maxIdle="20000" maxWait="120" name="jdbc/mysqlDB"
password="XXX" type="javax.sql.DataSource"
url="jdbc:mysql://localhost:3306/inzserdb_01" username="root"/>
</Context>
The Context in the comment ist the context that is used by the working
servlet project.
This is the $TOMCAT_HOME/wtpwebapps/IZServlets/META-INF/context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/mysqlDB" auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/inzserdb_01"
username="root" password="XXX"
maxActive="30" maxIdle="20000"
maxWait="120" />
</Context>
The password is in both files correctly set.
And this is $TOMCAT_HOME/wtpwebapps/IZServlets/WEB-INF/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>IZServlets</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<res-ref-name>jdbc/mysqlDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<servlet>
<description></description>
<display-name>EmployeeServlet</display-name>
<servlet-name>EmployeeServlet</servlet-name>
<servlet-class>com.inzeit.servlet.EmployeeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EmployeeServlet</servlet-name>
<url-pattern>/EmployeeServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>LoginServlet2</display-name>
<servlet-name>LoginServlet2</servlet-name>
<servlet-class>com.inzeit.servlet.LoginServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet2</servlet-name>
<url-pattern>/LoginServlet2</url-pattern>
</servlet-mapping>
</web-app>
Finally this is the part of the servlet throwing the exception.
private void checkCredentials(HttpServletRequest request,
HttpServletResponse response)
{
String query = "SELECT role, user.id, name, firstName, email
FROM user, employees, addresses WHERE userName = ? " +
"AND employees.id = user.employeeID AND employees.addressID =
addresses.ID";
String loginName = request.getHeader("userName");
String pw = request.getHeader("passWord");
try {
Context context = new InitialContext();
DataSource dataSource = (DataSource)
context.lookup("java:comp/env/jdbc/mysqlDB");
Connection connection = dataSource.getConnection();
connection.setAutoCommit(false);
...
}
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getLocalizedMessage());
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The exact position for the error is the line with Connection connection
= dataSource.getConnection();
and the error occurs when i call
http://127.0.0.1:8080/IZServlets/LoginServlet2, it doesn't matter if i
call it from the application or from a webbrowser.
The file mysql-connector-java-5.1.13-bin.jar is in the folder
$TOMCAT_HOME/wtpwebapps/IZServlets/WEB-INF/lib/ and also in the folder
$TOMCAT_HOME/lib/
I hope I forgot nothing and when there are more questions please ask,
because i need DB access soon.
Kind regards,
Michael of class '' for connect URL 'null'
at
org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1150)
at
org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
at
com.inzeit.servlet.LoginServlet2.checkCredentials(LoginServlet2.java:45)
at com.inzeit.servlet.LoginServlet2.doGet(LoginServlet2.java:82)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
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:852)
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: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:1143)
... 17 more
This is a part of the $TOMCAT_HOME/conf/server.xml
<!-- <Context docBase="C:\apache-tomcat-6.0.26\wtpwebapps\InzStartPage"
path="/InzStartPage" reloadable="true"
source="org.eclipse.jst.jee.server:InzStartPage"> -->
<Context docBase="C:\apache-tomcat-6.0.26\wtpwebapps\IZServlets"
path="/IZServlets" reloadable="true"
source="org.eclipse.jst.jee.server:IZServlets">
<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver"
maxActive="30" maxIdle="20000" maxWait="120" name="jdbc/mysqlDB"
password="XXX" type="javax.sql.DataSource"
url="jdbc:mysql://localhost:3306/inzserdb_01" username="root"/>
</Context>
The Context in the comment ist the context that is used by the working
servlet project.
This is the $TOMCAT_HOME/wtpwebapps/IZServlets/META-INF/context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/mysqlDB" auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/inzserdb_01"
username="root" password="XXX"
maxActive="30" maxIdle="20000"
maxWait="120" />
</Context>
The password is in both files correctly set.
And this is $TOMCAT_HOME/wtpwebapps/IZServlets/WEB-INF/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>IZServlets</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<res-ref-name>jdbc/mysqlDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<servlet>
<description></description>
<display-name>EmployeeServlet</display-name>
<servlet-name>EmployeeServlet</servlet-name>
<servlet-class>com.inzeit.servlet.EmployeeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EmployeeServlet</servlet-name>
<url-pattern>/EmployeeServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>LoginServlet2</display-name>
<servlet-name>LoginServlet2</servlet-name>
<servlet-class>com.inzeit.servlet.LoginServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet2</servlet-name>
<url-pattern>/LoginServlet2</url-pattern>
</servlet-mapping>
</web-app>
Finally this is the part of the servlet throwing the exception.
private void checkCredentials(HttpServletRequest request,
HttpServletResponse response)
{
String query = "SELECT role, user.id, name, firstName, email
FROM user, employees, addresses WHERE userName = ? " +
"AND employees.id = user.employeeID AND employees.addressID =
addresses.ID";
String loginName = request.getHeader("userName");
String pw = request.getHeader("passWord");
try {
Context context = new InitialContext();
DataSource dataSource = (DataSource)
context.lookup("java:comp/env/jdbc/mysqlDB");
Connection connection = dataSource.getConnection();
connection.setAutoCommit(false);
...
}
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getLocalizedMessage());
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The exact position for the error is the line with Connection connection
= dataSource.getConnection();
and the error occurs when i call
http://127.0.0.1:8080/IZServlets/LoginServlet2, it doesn't matter if i
call it from the application or from a webbrowser.
The file mysql-connector-java-5.1.13-bin.jar is in the folder
$TOMCAT_HOME/wtpwebapps/IZServlets/WEB-INF/lib/ and also in the folder
$TOMCAT_HOME/lib/
I hope I forgot nothing and when there are more questions please ask,
because i need DB access soon and any help is appreciated.
Kind regards,
Michael