繁体   English   中英

Tomcat6和MySQL问题

[英]Tomcat6 and MySQL issues

我使用Tomcat6重新安装了Ubuntu 11.10服务器。 我正在使用Eclipse Indigo制作一个Java Servlet,该Java Servlet从MySQL服务器中提取数据。 我已经将最新的Connector / J(mysql-connector-java-5.1.18-bin.jar)放在我的服务器CATALINA_HOME / lib目录中。 在Eclipse中,我创建了一个动态Web项目,然后在其中创建了一个servlet。 Servlet的Java代码为:

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

/**
 * Servlet implementation class TestServlet
 */
public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public TestServlet() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    Context initCtx = null;
    try {
        initCtx = new InitialContext();
    } catch (NamingException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
    Context envCtx = null;
    try {
        envCtx = (Context) initCtx.lookup("java:comp/env");
    } catch (NamingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    DataSource ds = null;
    try {
        ds = (DataSource) envCtx.lookup("jdbc/TestDB");
    } catch (NamingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    Connection conn = null;
    try {
        conn = ds.getConnection();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Statement stat = null;
    try {
        stat = conn.createStatement();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ResultSet rs = null;
    try {
        rs = stat.executeQuery("SELECT * FROM users");
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        while(rs.next()) {
            System.out.println("in");
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

我的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>TestServlet</display-name>
  <resource-ref>
    <description>TestDB</description>
    <res-ref-name>jdbc/TestDB</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>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>
  <servlet>
    <description></description>
    <display-name>TestServlet</display-name>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>TestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/TestServlet</url-pattern>
  </servlet-mapping>
</web-app>

我必须在META-INF中创建context.xml,但这是我的META-INF / context.xml的上下文

<?xml version="1.0" encoding="UTF-8"?>
<Context>
  <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="myuser" password="mypass" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/test1"/>
</Context>

我导出到战争,部署到我的tomcat服务器。 当我浏览到servlet时,出现以下错误:

java.lang.NullPointerException
TestServlet.doGet(TestServlet.java:67)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

TestServlet.java的第67行是conn.createStatement();。 线。 抱歉,我的代码很乱,我使用了单独的try catch块,因为当我执行一个大try catch时,虽然它仍未连接到MySQL数据库,但没有收到此错误。 我确定我在这里错过了一个相当简单的步骤,但是我似乎在任何地方都找不到它,我对Tomcat和Servlets有点陌生。 万分感谢。

根据Tomcat 6上下文文档

上下文元素可以明确定义:

  • 仅当$ CATALINA_BASE / conf / [enginename] / [hostname] /中的应用程序的上下文文件不存在时,该文件才位于应用程序文件内/META-INF/context.xml的单个文件中。 如果将Web应用程序打包为WAR,则/META-INF/context.xml将被复制到$ CATALINA_BASE / conf / [enginename] / [hostname] /并重命名以匹配应用程序的上下文路径。 该文件存在后,如果将具有更新的/META-INF/context.xml的新WAR放置在主机的appBase中,则将不会替换该文件。

尝试删除Tomcat的conf目录中的副本,然后查看是否提取了context.xml文件。

问题出在我的/etc/mysql/my.conf 我已经将我的bind-address设置为服务器的IP地址,这导致Tomcat无法连接。 为什么Tomcat不能但PHP可以,但我却无能为力。 我将绑定地址更改为0.0.0.0,一切都很好。

暂无
暂无

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

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