gpt4 book ai didi

java - Tomcat6 和 MySQL 问题

转载 作者:行者123 更新时间:2023-11-28 22:32:42 27 4
gpt4 key购买 nike

我新安装了带有 Tomcat6 的 Ubuntu 11.10 服务器。我正在使用 Eclipse Indigo 制作一个从我的 MySQL 服务器中提取数据的 Java Servlet。我已将最新的 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>

我导出到一个 war ,部署到我的 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 block ,因为当我进行一次大的 try catch 时我没有收到此错误,尽管它仍然没有连接到 MySQL 数据库。我确定我在这里错过了一个相当简单的步骤,但我似乎无法在任何地方找到它,我对 Tomcat 和 Servlet 有点陌生。非常感谢。

最佳答案

根据 Tomcat 6 context docs :

Context elements may be explicitly defined:

  • Only if a context file does not exist for the application in the $CATALINA_BASE/conf/[enginename]/[hostname]/, in an individual file at /META-INF/context.xml inside the application files. If the web application is packaged as a WAR then /META-INF/context.xml will be copied to $CATALINA_BASE/conf/[enginename]/[hostname]/ and renamed to match the application's context path. Once this file exists, it will not be replaced if a new WAR with a newer /META-INF/context.xml is placed in the host's appBase.

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

关于java - Tomcat6 和 MySQL 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8050467/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com