简体   繁体   English

Tomcat 7无法与MySQL 5.5建立数据库连接

[英]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: 当尝试访问从数据库读取的servlet时,出现以下错误:

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" 在Tomcat日志文件中查找,我发现:“名称testapp在此上下文中未绑定”

This is the relevant part of the server.xml 这是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: 我还修改了04webapps.policy以授予应用程序连接权限:

   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. 用户已在MySQL服务器上正确设置,我可以手动连接并选择,插入相关数据库。

What am i not doing right ? 我在做什么不对?

Change your resourceName ; 更改您的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. 首先,将jdbc代码编写为简单的Java程序,不要忘记将jdbc驱动程序添加为外部jar文件。 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. 您也可以尝试将jdbc jar文件放在tomcat的lib文件夹或jre / lib / ext文件夹中 ,这是因为,当servlet编译时,它将在第3方jar中将cse的lib jar文件用于cse。 if the jdbc connectivity code has no error it will run file after adding the jar file 如果jdbc连接代码没有错误,则它将在添加jar文件后运行文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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