2012-10-10 4 views
0

MS SQL 2005를 Eclipse에 연결하는 방법과 Tomcat 7을 내 서버로 연결하는 방법을 이해하기 위해이 코드를 사용하려고합니다. 나는 단지 java 웹 애플리케이션을 만드는 데있어 새로운 것이므로 나와 함께 참아주십시오. 아래 오류에서 어떻게 오류의 원인을 추적 할 수 있습니까? 프로젝트에는 2 개의 JSP 파일과 3 개의 클래스 파일이 있습니다. index.jsp, result.jsp, SQLConnection.java, Items.javaItemController.java.HTTP 상태 500 오류입니다. Eclipse + Tomcat + MS SQL 2005

오류 이렇게되면

,

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 
} 

} 
} 
index.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> 
+0

여기에 관련 코드를 추가하십시오. – gks

답변

0

오류 스택 추적은 점점 있다고 NullPointerExceptionSQLConnection.java에서의 당신이 호출 할 때 execSelectQuery 방법이 예외를 얻고있다 이 방법은 Item.java입니다.

java.lang.NullPointerException 
connection.SQLConnection.execSelectQuery(SQLConnection.java:35) 

놓습니다 model.Item.lastItems(Item.java:10)에서 Item.java에서 디버그 가리킨 다음 Statement stmt = con.createStatement();

에서 나는 당신의 Connection NULL 수 있습니다 의심한다.

+0

답장을 보내 주셔서 감사합니다. 내 질문에 SQLConnection.java 및 index.jsp 코드를 추가했습니다. 당신이 나를 도울 수 있기를 바랍니다.^_^ – Ching29

+0

@ Ching29 : 코드 디버깅을 시도하고 연결이 null이 아닌지 확인하십시오. –