0

Hello I have following problem: Whenever I try to connect to MySQL DB I get this error:

com.pbs.web.jdbc.ParkingBookSystem.BookControllerServlet doGet
SEVERE: null
**java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null'**
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2224)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2104)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1563)
    at com.pbs.web.jdbc.ParkingBookSystem.BookDbUtil.doesBookExist(BookDbUtil.java:91)
    at com.pbs.web.jdbc.ParkingBookSystem.BookControllerServlet.addBook(BookControllerServlet.java:117)
    at com.pbs.web.jdbc.ParkingBookSystem.BookControllerServlet.doGet(BookControllerServlet.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:770)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

**Caused by: java.sql.SQLException: No suitable driver
    at java.sql.DriverManager.getDriver(Unknown Source)**
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2209)
    ... 29 more

I have context.xml placed in the the: C:\Users\userName\eclipse-workspace\WebAppName\src\main\resources\META-INF Here is the code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Context>
<Resource 
    name="ParkingBookSystem" 
    auth="Container"
    type="javax.sql.DataSource"
    maxTotal="20"
    maxIdle="5"
    maxWaitMillis="10000"
    username="sql2226123"
    password="cccccccc"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://sql2.freemysqlhosting.net:3306/sql2226123"
/>
</Context>

and my web.xml (placed at C:\Users\userName\eclipse-workspace\WebAppName\src\main\webapp\WEB-INF ):

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<description>ParkingBookSystem</description>
<resource-ref>
    <description>DB Connection</description>
    <res-ref-name>/ParkingBookSystem</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

Here is my class constructor to make DB connection:

// constructor
public BookDbUtil() {
    try {
        Context initContext = new InitialContext();
        Context envContext = (Context) initContext.lookup("java:/comp/env");
        dataSource = (DataSource) envContext.lookup("ParkingBookSystem");
    } catch (NamingException e) {
        throw new IllegalStateException("Could not initialize database", e);
    }
}

I'm using Maven, Eclipse and Tomcat this is what I've tried so far:

  • I put the mysql-connector-java-5.1.38-bin to the tomcat lib
  • Manualy put the context.xml to C:\Program Files\Apache Software Foundation\Tomcat 9.0\conf\Catalina\localhost
  • as above but changed name from context.xml to projectName.xml
  • Put all content into the server.xml (and commented out default resource what was here)
  • I found also that evey change in tomcat files need admin previleage so I added full controll for each user to make sure that is not the case.

I have no more ideas, any one is able to help?

Luke_Nuke
  • 461
  • 2
  • 6
  • 23
  • Try adding the `Resource`element at `C:\Program Files\Apache Software Foundation\Tomcat 9.0\conf\context.xml`, remember to add `jdbc/` in the JNDI name. Also, how are you starting tomcat, from Eclipse? – LMC Aug 30 '18 at 19:46
  • Yes exactly from the eclipse – Luke_Nuke Aug 30 '18 at 19:56
  • 1
    This could help: https://stackoverflow.com/a/40557337/2834978 – LMC Aug 30 '18 at 20:00
  • ...and remember to change your password, unless the one you post here isn't your real one. – Olaf Kock Aug 30 '18 at 21:42

2 Answers2

1

please;

put the mysql connector jar file in WEB-INF/lib directory

and change in the context.xml

<Context>
    <Resource name="jdbc/ParkingBookSystem" ...

and change in the web.xml

<res-ref-name>jdbc/ParkingBookSystem</res-ref-name>

and change lookup

Context initContext = new InitialContext();
Context envContext  = (Context)initContext.lookup("java:/comp/env");
dataSource = (DataSource)envContext.lookup("jdbc/ParkingBookSystem");
guleryuz
  • 2,714
  • 1
  • 15
  • 19
  • I've put mysql-connector-java-5.1.38-bin to: C:\Users\userName\eclipse-workspace\WebAppName\src\main\webapp\WEB-INF\lib and edited context.xml, web.xml, and lookup but still have the same error :( – Luke_Nuke Aug 30 '18 at 16:18
  • did you `clean` `install` and restart the server? – guleryuz Aug 30 '18 at 17:17
  • I did. I stopped server then I clicked right mouse on the project then run as Maven build than as goal: I provide: clean install. Then run server again and still there is the same error :( – Luke_Nuke Aug 30 '18 at 19:09
-1

Ok I found solution... The thing is my META-INF with context.xml was in C:\Users\lukas\eclipse-workspace\ParkingBookSystem\src\main\resources\META-INF\context.xml when I put it into the: C:\Users\lukas\eclipse-workspace\ParkingBookSystem\src\main\webapp it started to work.

Luke_Nuke
  • 461
  • 2
  • 6
  • 23
  • 2
    If this is an answer to your question (e.g. it solved your problem), you might mark this as accepted answer (even when you gave it yourself). If this is another question, don't post it in an answer, because here it won't be answered by anyone. Plus, YOU HAVE TO change your password that you originally posted in the question. Preferably before someone else does. – Olaf Kock Sep 01 '18 at 11:09