简体   繁体   中英

Tomcat 7 cannot get a database connection with MySQL 5.5

When trying to access a servlet that reads from the database i get the following error:

HTTP Status 500 -
...
java.lang.NullPointerException
    com.pdq.sms.db.DatabaseAccess.getConnection(DatabaseAccess.java:39)
    com.pdq.sms.db.DataRow.Read(DataRow.java:79)
    com.kessel.controllers.Home.doPost(Home.java:50)
    com.kessel.controllers.Home.doGet(Home.java:39)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.26 logs.

Looking in the Tomcat log files i find this : "Name testapp is not bound in this Context"

This is the relevant part of the server.xml

<Host name="myserver.org" appBase="webapps/test" 
      unpackWARs="true" autoDeploy="true">
   <Context path="/" docBase="testapp" reloadable="false">
      <Resource name="jdbc/testapp" auth="Container"
          type="javax.sql.DataSource" maxActive="100" maxIdle="10" maxWait="10000"
          validationQuery="SELECT 1" driverClassName="com.mysql.jdbc.Driver"
          username="myuser" password="mypass"
          url="jdbc:mysql://localhost:3306/test_db?zeroDateTimeBehavior=convertToNull" />
    </Context>"
</Host>"

I've also modified the 04webapps.policy to give the application permission to connect:

   permission java.net.SocketPermission "127.0.0.1:3306", "resolve, connect";

User is setup properly on the MySQL server, i can manually connect and select, insert etc on the database in question.

What am i not doing right ?

Change your resourceName ;

   java:comp/env/jdbc/testapp

to

  java://comp/env/jdbc/testapp

First write a jdbc code as a simple java proagram and don't forget to add the jdbc driver as external jar file. And you can also try by putting the jdbc jar file in tomcat lib folder or jre/lib/ext folder , this is because when a servlet compiles it will take help of lib jar file in cse it in a 3rd party jar. if the jdbc connectivity code has no error it will run file after adding the jar file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM