我使用Tomcat6安裝了Ubuntu 11.10服務器。我正在使用Eclipse Indigo來製作從我的MySQL服務器提取數據的Java Servlet。我已將最新的Connector/J(mysql-connector-java-5.1.18-bin.jar)放入我的服務器CATALINA_HOME/lib目錄中。在Eclipse中,我創建了一個Dynamic Web Project,然後創建一個servlet。 servlet的Java代碼是:Tomcat6和MySQL問題
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塊,因爲當我做了一個大的嘗試抓取時,我沒有得到這個錯誤,儘管它仍然沒有連接到MySQL數據庫。我確定我在這裏錯過了一個相當簡單的步驟,但我似乎無法在任何地方找到它,我對Tomcat和Servlets有點新鮮感。萬分感謝。
我意識到上面發佈的代碼正在執行'system.out.println(「in」)'而不是printwriter,錯誤是一樣的,當我提出這個問題時,我衝過我的代碼部分,它應該是無關緊要的。 – tdramble
檢查以確保您的context.xml實際正在部署;如果您使用WTP插件,則需要啓用發佈模塊上下文。 –
那麼是否有getConnection()的堆棧跟蹤? –