我正在尝试此代码,以便了解如何将 MS SQL 2005 连接到 Eclipse 和 Tomcat 7 作为我的服务器。我是制作 Java Web 应用程序的新手,所以请多多包涵。在下面的错误中,我如何追踪错误的原因在哪里?该项目有2个jsp和3个类文件。index.jsp
, result.jsp
,和. SQLConnection.java
_Items.java
ItemController.java
错误是这样的,
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause
java.lang.NullPointerException
connection.SQLConnection.execSelectQuery(SQLConnection.java:35)
model.Item.lastItems(Item.java:10)
org.apache.jsp.index_jsp._jspService(index_jsp.java:83)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
SQLConnection.java 和 index.jsp 的代码如下;
SQLConnection.java
package connection;
import java.sql.*;
public class SQLConnection {
protected Connection con=null;
protected ResultSet rs=null;
public SQLConnection(String address)
{
try
{
String conString = "jdbc:sqlserver://localhost:1433;databaseName=Chingdb; integratedSecurity=true;";
con = DriverManager.getConnection(conString);
}catch(SQLException sqlError){
//ERROR MANAGEMENT HERE
}
}
public boolean execQuery(String sql)
{
boolean result = false;
try
{
Statement stmt = con.createStatement();
stmt.execute(sql);
result = true;
}catch(SQLException sqlError){
//ERROR MANAGEMENT HERE
result = false;
}
return result;
}
public void execSelectQuery(String sql)
{
try
{
Statement stmt = con.createStatement();
this.rs = stmt.executeQuery(sql);
}catch(SQLException sqlError){
//ERROR MANAGEMENT HERE
}
}
public void disconnect()
{
try
{
con.close();
}catch(SQLException sqlError){
//ERROR MANAGEMENT HERE
}
}
}
索引.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org /TR/html4/loose.dtd">
<%@page import="model.Item"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Todo-List App</title>
</head>
<body>
<h2>Add a new item</h2>
<form action="addItem" method="post">
<table>
<tr>
<td><input type="text" value="I have to..." onClick="this.value('')" name="todo"/></td>
<td><input type="submit" value="Send" name="send"/> </td>
</tr>
</table>
</form>
<h2>Todo-List</h2>
<%
Item item = new Item();
String[] lastest = item.lastItems();
for(int i=0;i<lastest.length;i++)
{%>
<%=lastest[i] %> <br>
<%}item.disconnect();
%>
</body>
</html>