调用jsp页面时出现空指针异常,我找不到是哪里的问题。
package com.letousky;
import java.sql.*;
public class ConDb {
private Connection con = null;
private Statement stmt = null;
private ResultSet rst = null;
private String sql = "";
public ConDb() {
String classforname = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String username = "sa";
String password = "sa";
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=project_news";
try {
Class.forName(classforname).newInstance();
Connection con = DriverManager.getConnection(url, username, password);
Statement stmt = con.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
}
public void setSql(String sql) {
this.sql = sql;
}
public void execQuery() {
try {
rst = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
public ResultSet getRst() {
this.execQuery();
return this.rst;
}
public void conClose() {
try {
if(rst!=null)rst.close();
if(stmt!=null)stmt.close();
if(con!=null)con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
JSP相关代码:
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<jsp:useBean id="ConDbbean" class="com.letousky.ConDb">
<jsp:setProperty name="ConDbbean" property="*" />
</jsp:useBean>
.................
<tr>
<td width="15%" align="center" bgcolor="#FFFFF">
<a href="sfc.jsp" target="_self"><font color="red">胜负彩</font>
</a>
</td>
<%
String sql_1 = "select * from project_sfc_No";
ConDbbean.setSql(sql_1);
ResultSet rst_1 = ConDbbean.getRst();
try {
while (rst_1.next()) {
%>
<td align="center" bgcolor="#FFFFF">
<%=rst_1.getString("PhaseNo")%>
</td>
<td align="center" bgcolor="#FFFFF">
<font color="red"><b><%=rst_1.getString("No_1")%> </b> </font>
</td>
<td align="center" bgcolor="#FFFFF">
<font color="red"><b><%=rst_1.getString("No_2")%> </b> </font>
</td>
<td align="center" bgcolor="#FFFFF">
<font color="red"><b><%=rst_1.getString("No_3")%> </b> </font>
</td>
<td align="center" bgcolor="#FFFFF">
<font color="red"><b><%=rst_1.getString("No_4")%> </b> </font>
</td>
<td align="center" bgcolor="#FFFFF">
<font color="red"><b><%=rst_1.getString("No_5")%> </b> </font>
</td>
<td align="center" bgcolor="#FFFFF">
<font color="red"><b><%=rst_1.getString("No_6")%> </b> </font>
</td>
<td align="center" bgcolor="#FFFFF">
<font color="red"><b><%=rst_1.getString("No_7")%> </b> </font>
</td>
<td align="center" bgcolor="#FFFFF">
<font color="red"><b><%=rst_1.getString("No_8")%> </b> </font>
</td>
<td align="center" bgcolor="#FFFFF">
<font color="red"><b><%=rst_1.getString("No_9")%> </b> </font>
</td>
<td align="center" bgcolor="#FFFFF">
<font color="red"><b><%=rst_1.getString("No_10")%> </b> </font>
</td>
<td align="center" bgcolor="#FFFFF">
<font color="red"><b><%=rst_1.getString("No_11")%> </b> </font>
</td>
<td align="center" bgcolor="#FFFFF">
<font color="red"><b><%=rst_1.getString("No_12")%> </b> </font>
</td>
<td align="center" bgcolor="#FFFFF">
<font color="red"><b><%=rst_1.getString("No_13")%> </b> </font>
</td>
<td align="center" bgcolor="#FFFFF">
<font color="red"><b><%=rst_1.getString("No_14")%> </b> </font>
</td>
<%
}
} catch (SQLException e) {
out.println(e.getMessage());
}
%>
TOMCAT异常:
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
java.lang.NullPointerException
com.letousky.ConDb.execQuery(ConDb.java:37)
com.letousky.ConDb.getRst(ConDb.java:32)
org.apache.jsp.projectindex_jsp._jspService(projectindex_jsp.java:125)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
|
|