做了个界面从数据库中读取数据显示在页面上,但是JSP就出现了空指针错误,我搞了好半天都没搞好,希望大家帮帮忙,我万分感谢!
我是在JSP页面中用usebean调用操作数据库的class文件,需要指出的是单独测试class时可以正常读取,放到JSP页面中就不行了。以下是代码:
DBManage //连接数据库
package com.accp.codevitamin;
import java.sql.*;
public class DBManager{
public static Connection getConnection() {
Connection con=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(
"jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=myaccess.mdb");
//con=DriverManager.getConnection("jdbc:odbc:myaccess");
}
catch (Exception e) {
System.out.println(e.getMessage());
}
return con;
}
}
-----------------------------------------------------------
ActionDB //从数据库中读取数据,返回ResultSet型
package com.accp.codevitamin;
import java.sql.*;
public class ActionDB {
public Connection conn=null;
public ActionDB() {
}
public ResultSet selectDB(String str){
conn=DBManager.getConnection();
ResultSet rs=null;
try{
Statement st=conn.createStatement();
rs=st.executeQuery(str);
}
catch(Exception e){
}
return rs;
}
/* public static void main(String[] args){ //单独测试可以正常读取
try{
ActionDB ac=new ActionDB();
ResultSet rs=ac.selectDB("select top 10 * from news order by id DESC");
while(rs.next()){
System.out.println(rs.getString("news"));
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
} */
}
-----------------------------------------------------------------------------------
JSP页面:
<%@ page contentType="text/html; charset=GBK" language="java" import="java.sql.*" errorPage="linkerror.jsp" %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<style type="text/css">
<!--
body {
background-image: url(image/back.gif);
margin-top: 0px;
}
-->
</style>
<link href="css.css" rel="stylesheet" type="text/css">
<script language="javascript">
window.status="您的支持是我最大的动力,请给我一个肯定的眼神 :)";
window.document.title="";
</script>
<style type="text/css">
<!--
.STYLE4 {font-size: x-large}
-->
</style>
</head><body>
<jsp:useBean id="news" class="com.accp.codevitamin.ActionDB" scope="page" />
<table width="800" border="0" align="center" cellpadding="0" cellspacing="0" class="css">
<!--DWLayoutTable-->
<tr>
<td height="13" colspan="4" valign="top" bgcolor="#CCCCCC">
<marquee behavior="alternate" scrollamount="3"><a href="list.asp?info=cuxiao">欢迎来到图书网上销售系统!请点击查看本期促销图书信息!</a></marquee> </td>
</tr>
<tr>
<td height="70" colspan="4" align="center" valign="middle" bgcolor="#CCCCCC"><span class="STYLE4">图书网上销售系统</span></td>
</tr>
<tr>
<td height="12" colspan="4" align="center" valign="middle"><!--DWLayoutEmptyCell--> </td>
</tr>
<tr>
<td height="12" colspan="3" align="center" valign="middle" bgcolor="#CCCCCC">公告</td>
<td width="587" valign="top" bgcolor="#CCCCCC">图书新闻:</td>
</tr>
<tr>
<td width="11" rowspan="3" valign="top" bgcolor="#CCCCCC"><!--DWLayoutEmptyCell--> </td>
<td width="191" rowspan="3" valign="top"><!--DWLayoutEmptyCell--> </td>
<td width="11" rowspan="3" valign="top" bgcolor="#CCCCCC"><!--DWLayoutEmptyCell--> </td>
<td height="12" valign="top"><!--DWLayoutEmptyCell--> </td>
</tr>
<%
int i=0;
ResultSet newsrs=news.selectDB("select top 10 * from news order by id DESC");
while(newsrs.next()&&i<10){ //这里出现空指针
%>
<tr>
<td height="12" valign="top"><%=newsrs.getString("news")%></td>
</tr>
<%
i=i+1;
}
newsrs.close();
news.conn.close();
%>
<tr>
<td height="143"> </td>
</tr>
<tr>
<td height="12" colspan="3" valign="top" bgcolor="#CCCCCC"><!--DWLayoutEmptyCell--> </td>
<td></td>
</tr>
<tr>
<td height="480"> </td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</body>
</html>
-----------------------------------------------------------------------------------
错误提示:
type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause java.lang.NullPointerException
org.apache.jsp.index_jsp._jspService(org.apache.jsp.index_jsp:113)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.14 logs.-------------------------------------------------------------------
帮帮忙啦,只要问题解决,再加分都可以 :(
我是在JSP页面中用usebean调用操作数据库的class文件,需要指出的是单独测试class时可以正常读取,放到JSP页面中就不行了。以下是代码:
DBManage //连接数据库
package com.accp.codevitamin;
import java.sql.*;
public class DBManager{
public static Connection getConnection() {
Connection con=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(
"jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=myaccess.mdb");
//con=DriverManager.getConnection("jdbc:odbc:myaccess");
}
catch (Exception e) {
System.out.println(e.getMessage());
}
return con;
}
}
-----------------------------------------------------------
ActionDB //从数据库中读取数据,返回ResultSet型
package com.accp.codevitamin;
import java.sql.*;
public class ActionDB {
public Connection conn=null;
public ActionDB() {
}
public ResultSet selectDB(String str){
conn=DBManager.getConnection();
ResultSet rs=null;
try{
Statement st=conn.createStatement();
rs=st.executeQuery(str);
}
catch(Exception e){
}
return rs;
}
/* public static void main(String[] args){ //单独测试可以正常读取
try{
ActionDB ac=new ActionDB();
ResultSet rs=ac.selectDB("select top 10 * from news order by id DESC");
while(rs.next()){
System.out.println(rs.getString("news"));
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
} */
}
-----------------------------------------------------------------------------------
JSP页面:
<%@ page contentType="text/html; charset=GBK" language="java" import="java.sql.*" errorPage="linkerror.jsp" %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<style type="text/css">
<!--
body {
background-image: url(image/back.gif);
margin-top: 0px;
}
-->
</style>
<link href="css.css" rel="stylesheet" type="text/css">
<script language="javascript">
window.status="您的支持是我最大的动力,请给我一个肯定的眼神 :)";
window.document.title="";
</script>
<style type="text/css">
<!--
.STYLE4 {font-size: x-large}
-->
</style>
</head><body>
<jsp:useBean id="news" class="com.accp.codevitamin.ActionDB" scope="page" />
<table width="800" border="0" align="center" cellpadding="0" cellspacing="0" class="css">
<!--DWLayoutTable-->
<tr>
<td height="13" colspan="4" valign="top" bgcolor="#CCCCCC">
<marquee behavior="alternate" scrollamount="3"><a href="list.asp?info=cuxiao">欢迎来到图书网上销售系统!请点击查看本期促销图书信息!</a></marquee> </td>
</tr>
<tr>
<td height="70" colspan="4" align="center" valign="middle" bgcolor="#CCCCCC"><span class="STYLE4">图书网上销售系统</span></td>
</tr>
<tr>
<td height="12" colspan="4" align="center" valign="middle"><!--DWLayoutEmptyCell--> </td>
</tr>
<tr>
<td height="12" colspan="3" align="center" valign="middle" bgcolor="#CCCCCC">公告</td>
<td width="587" valign="top" bgcolor="#CCCCCC">图书新闻:</td>
</tr>
<tr>
<td width="11" rowspan="3" valign="top" bgcolor="#CCCCCC"><!--DWLayoutEmptyCell--> </td>
<td width="191" rowspan="3" valign="top"><!--DWLayoutEmptyCell--> </td>
<td width="11" rowspan="3" valign="top" bgcolor="#CCCCCC"><!--DWLayoutEmptyCell--> </td>
<td height="12" valign="top"><!--DWLayoutEmptyCell--> </td>
</tr>
<%
int i=0;
ResultSet newsrs=news.selectDB("select top 10 * from news order by id DESC");
while(newsrs.next()&&i<10){ //这里出现空指针
%>
<tr>
<td height="12" valign="top"><%=newsrs.getString("news")%></td>
</tr>
<%
i=i+1;
}
newsrs.close();
news.conn.close();
%>
<tr>
<td height="143"> </td>
</tr>
<tr>
<td height="12" colspan="3" valign="top" bgcolor="#CCCCCC"><!--DWLayoutEmptyCell--> </td>
<td></td>
</tr>
<tr>
<td height="480"> </td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</body>
</html>
-----------------------------------------------------------------------------------
错误提示:
type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause java.lang.NullPointerException
org.apache.jsp.index_jsp._jspService(org.apache.jsp.index_jsp:113)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.14 logs.-------------------------------------------------------------------
帮帮忙啦,只要问题解决,再加分都可以 :(
你测试下newsrs有没有取到值,是不是NULL.
直接连接,D:/test.mdb 是数据库位置,tset,test是用户名密码,没有就设为空import java.sql.*;public class access{
public static void main(String[] args) throws Exception{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
String url="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=D:/test.mdb";
Connection conn = DriverManager.getConnection(url,"test","test");
Statement stmt=conn.createStatement();
stmt.execute("insert into t_test(name) values('yinlei')"); ResultSet res = stmt.executeQuery("select * from t_test"); while(res.next()){
System.out.println(" "+res.getString("name"));
}
res.close();
stmt.close();
conn.close();
}
}
<table class="monthtable" border="0" align="center" cellpadding="0" cellspacing="1">
<tr valign="bottom" align="center" height="25">
<th width="70"><c:out value="${admin.day}"/></th>
<th width="70"><c:out value="${admin.weekday}"/></th>
<th width="110"><c:out value="${admin.onWorkTime}"/></th>
<th width="110"><c:out value="${admin.outWorkTime}"/></th>
<th width="110"><c:out value="${admin.workTime}"/></th>
<th width="110"><c:out value="${admin.restWorkTime}"/></th>
<th width="110"><c:out value="${admin.workStatus}"/></th>
<th width="110"><c:out value="${admin.note}"/></th>
</tr>
<c:forEach items="${QueryForm.dayList}" var="dayitem" varStatus="s">
<tr id='<c:out value="${dayitem.id}"/>' align="center" bgcolor="<c:out value="${dayitem.trcolor}"/>" ondblclick="openwin(this)">
<td height="25" align="center">
<c:if test="${dayitem.picflag ==0 }"><font color="red"></font></c:if>
<font color="<c:out value="${dayitem.colorname}"/>"><c:out value="${dayitem.dayid}"/></font></td>
<td><c:out value="${dayitem.weekday}"/></td>
<td><c:out value="${dayitem.beginTime}"/></td>
<td><c:out value="${dayitem.endTime}"/></td>
<td><c:out value="${dayitem.timeLength}"/></td>
<td><c:out value="${dayitem.restTime}"/></td>
<td><c:out value="${dayitem.workStatus}"/></td>
<td><c:out value="${dayitem.note}"/></td>
</tr>
</c:forEach>
</table>
奇怪的是单独测试CLASS却完全正常
可结果还是一样,一到JSP界面就什么也读不到了
错误显示: java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1
而楼主<jsp:useBean id="news" class="com.accp.codevitamin.ActionDB" scope="page" />你的news是一个对数据库操作的bean. 你这样就会把对数据库的操作带到jsp页面里来.
(看到你下面的sql语句放到了jsp里 就证明了这点)但是jsp只用来"展现"数据的,所以在j2ee里面,是把jsp定位为"展现层组件",展现就是把数据列出来,而不要select 他.
你还是按照 chuan122345() 的方法来吧.