最近都被这个问题烦恼了几天了
数据库用的是sql2000这个错java.lang.ClassCastException: java.lang.Long关键代码代码如下获取数据:
public List BrowersBook(int pageSize,int pageNo) {
Session session=MySessionFactory.getSession();
Transaction tx=null;
List list=null;
try
{
String hql="from Book as a order by a.id";
Query query=session.createQuery(hql);
query.setFirstResult((pageNo-1)*pageSize);
query.setMaxResults(pageSize);
tx=session.beginTransaction();
list=query.list();
tx.commit();
if (!Hibernate.isInitialized(list))
Hibernate.initialize(list);
}
catch(Exception e)
{
if(tx!=null)
tx.rollback();
System.out.println(e);
System.out.println("在BookServiceImpl中的BrowersBook方法出错");
}
finally
{
MySessionFactory.closeSession();
}
return list;
} public boolean addBook(Book book) {
Session session=MySessionFactory.getSession();
Transaction tx=null;
boolean resuilt=false;
try
{
tx=session.beginTransaction();
session.save(book);
tx.commit();
resuilt=true;
}
catch(Exception e)
{
System.out.println(e);
System.out.println("在BookServiceImpl中的loadBook方法出错");
if(tx!=null)
{
tx.rollback();
}
}
finally
{
MySessionFactory.closeSession();
}
return resuilt;
}
获取记录数
public int BookCountRecord()
{
Session session=MySessionFactory.getSession();
Transaction tx=null;
int resuilt=0;
try
{
String hql="select count(*) from Book";
Query query=session.createQuery(hql);
query.setMaxResults(1);
resuilt=((Integer)query.uniqueResult()).intValue();
resuilt=(int)resuilt;
tx.commit();
}
catch(HibernateException e)
{ System.out.println(e);
System.out.println("在BookServiceImpl中的BookCountRecord方法出错");
if(tx!=null)
tx.rollback();
}
finally
{
MySessionFactory.closeSession();
}
return resuilt;
}Action中的BrowserBook 函数
public ActionForward BrowserBook(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
ActionForward forward=null;
List booklist=null;
int pageNo=1;
int pageSize=4;
int totals = 0; //记录总数
int totalPages = 0; //总页数
BookService service=new BookServiceImpl();
try
{ if(request.getParameter("pageNo")!=null)
pageNo=Integer.parseInt(request.getParameter("pageNo"));
booklist=service.BrowersBook(pageSize, pageNo);
if(booklist!=null)
request.setAttribute("booklist", booklist);
totals=service.BookCountRecord();
totalPages=totals/pageSize;
if((totals%pageSize)>0)
{ //多出一页显示剩余的
totalPages++;
}
request.setAttribute("pageNo",new Integer(pageNo).toString());
request.setAttribute("totals",new Integer(totals).toString());
request.setAttribute("totalPages",new Integer(totalPages).toString());
forward=mapping.findForward("showall");
}
catch(Exception e)
{
System.out.println(e);
System.out.println("在BrowserBook中异常 ");
}
return forward;
}jsp显示页
<%@ page language="java" pageEncoding="gb2312"%><%@ taglib uri="/struts-bean" prefix="bean" %>
<%@ taglib uri="/struts-html" prefix="html" %>
<%@ taglib uri="/struts-logic" prefix="logic" %>
<%
String action="book.do?method=BrowserBook&";
int pageNo=1,totals=1,totalPages=1,prepage=1,nextpage=1;
//设置标志位pageSize=1
int nextOk=1,preOk=1;
//获取页数
if(request.getParameter("pageNo")!=null)
pageNo=Integer.parseInt(request.getParameter("pageNo").toString());//request.getParameter("pageNo")
if(request.getParameter("totals")!=null)
totals=Integer.parseInt(request.getParameter("totals").toString());
if(request.getParameter("totalPages")!=null)
totalPages=Integer.parseInt(request.getParameter("totalPages").toString());
//计算页数
prepage=pageNo;
nextpage=pageNo;
if(pageNo>1)
prepage--;
else
prepage = 1;
if(pageNo<totalPages)
nextpage++;
else
nextpage=totalPages;
//无法向前
if(pageNo==1)prepage=0;
// 无法向后
if(pageNo==totalPages)nextpage=0;
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>ShowAll.jsp</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> <style type="text/css">
<!--
.STYLE1 {font-size: large}
.STYLE2 {
font-size: larger;
font-weight: bold;
}
-->
</style>
</head>
<body>
<logic:present name="booklist">
<logic:iterate id="book" name="booklist">
<p> </p>
<p class="STYLE2">书名:${book.bookname}<br/>
作者:${book.authod} </p>
<p class="STYLE1"><a href="book.do?method=showBook&bookid=${book.id}">查看</a></p>
</logic:iterate>
</logic:present>
<p align="center" class="STYLE1"> <a href="<%=action%>pageNo=1">第一页</a>
<% if(nextOk==1){%>
<a href="<%=action%>pageNo=<%=prepage%>">上一页</a>
<%}else{%>
<span class="grayText">上一页</span> 
<%}%>
<%if(preOk==1){%>
<a href="<%=action%>pageNo=<%=nextpage%>">下一页</a>
<%}else{%>
<span class="grayText">下一页</span> 
<%}%>
<a href="<%=action%>pageNo=<%=totalPages%>">最后一页</a>
到
<input name="pageInput" type="text" value="1" size="2">
<input type="submit" name="Submit" value="GO">
</p> <p align="center" class="STYLE1"> </p>
<p align="center" class="STYLE1"> </p>
<br>
</body>
</html:html>大家帮帮忙啊 我实在是没有办法了
数据库用的是sql2000这个错java.lang.ClassCastException: java.lang.Long关键代码代码如下获取数据:
public List BrowersBook(int pageSize,int pageNo) {
Session session=MySessionFactory.getSession();
Transaction tx=null;
List list=null;
try
{
String hql="from Book as a order by a.id";
Query query=session.createQuery(hql);
query.setFirstResult((pageNo-1)*pageSize);
query.setMaxResults(pageSize);
tx=session.beginTransaction();
list=query.list();
tx.commit();
if (!Hibernate.isInitialized(list))
Hibernate.initialize(list);
}
catch(Exception e)
{
if(tx!=null)
tx.rollback();
System.out.println(e);
System.out.println("在BookServiceImpl中的BrowersBook方法出错");
}
finally
{
MySessionFactory.closeSession();
}
return list;
} public boolean addBook(Book book) {
Session session=MySessionFactory.getSession();
Transaction tx=null;
boolean resuilt=false;
try
{
tx=session.beginTransaction();
session.save(book);
tx.commit();
resuilt=true;
}
catch(Exception e)
{
System.out.println(e);
System.out.println("在BookServiceImpl中的loadBook方法出错");
if(tx!=null)
{
tx.rollback();
}
}
finally
{
MySessionFactory.closeSession();
}
return resuilt;
}
获取记录数
public int BookCountRecord()
{
Session session=MySessionFactory.getSession();
Transaction tx=null;
int resuilt=0;
try
{
String hql="select count(*) from Book";
Query query=session.createQuery(hql);
query.setMaxResults(1);
resuilt=((Integer)query.uniqueResult()).intValue();
resuilt=(int)resuilt;
tx.commit();
}
catch(HibernateException e)
{ System.out.println(e);
System.out.println("在BookServiceImpl中的BookCountRecord方法出错");
if(tx!=null)
tx.rollback();
}
finally
{
MySessionFactory.closeSession();
}
return resuilt;
}Action中的BrowserBook 函数
public ActionForward BrowserBook(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
ActionForward forward=null;
List booklist=null;
int pageNo=1;
int pageSize=4;
int totals = 0; //记录总数
int totalPages = 0; //总页数
BookService service=new BookServiceImpl();
try
{ if(request.getParameter("pageNo")!=null)
pageNo=Integer.parseInt(request.getParameter("pageNo"));
booklist=service.BrowersBook(pageSize, pageNo);
if(booklist!=null)
request.setAttribute("booklist", booklist);
totals=service.BookCountRecord();
totalPages=totals/pageSize;
if((totals%pageSize)>0)
{ //多出一页显示剩余的
totalPages++;
}
request.setAttribute("pageNo",new Integer(pageNo).toString());
request.setAttribute("totals",new Integer(totals).toString());
request.setAttribute("totalPages",new Integer(totalPages).toString());
forward=mapping.findForward("showall");
}
catch(Exception e)
{
System.out.println(e);
System.out.println("在BrowserBook中异常 ");
}
return forward;
}jsp显示页
<%@ page language="java" pageEncoding="gb2312"%><%@ taglib uri="/struts-bean" prefix="bean" %>
<%@ taglib uri="/struts-html" prefix="html" %>
<%@ taglib uri="/struts-logic" prefix="logic" %>
<%
String action="book.do?method=BrowserBook&";
int pageNo=1,totals=1,totalPages=1,prepage=1,nextpage=1;
//设置标志位pageSize=1
int nextOk=1,preOk=1;
//获取页数
if(request.getParameter("pageNo")!=null)
pageNo=Integer.parseInt(request.getParameter("pageNo").toString());//request.getParameter("pageNo")
if(request.getParameter("totals")!=null)
totals=Integer.parseInt(request.getParameter("totals").toString());
if(request.getParameter("totalPages")!=null)
totalPages=Integer.parseInt(request.getParameter("totalPages").toString());
//计算页数
prepage=pageNo;
nextpage=pageNo;
if(pageNo>1)
prepage--;
else
prepage = 1;
if(pageNo<totalPages)
nextpage++;
else
nextpage=totalPages;
//无法向前
if(pageNo==1)prepage=0;
// 无法向后
if(pageNo==totalPages)nextpage=0;
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>ShowAll.jsp</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> <style type="text/css">
<!--
.STYLE1 {font-size: large}
.STYLE2 {
font-size: larger;
font-weight: bold;
}
-->
</style>
</head>
<body>
<logic:present name="booklist">
<logic:iterate id="book" name="booklist">
<p> </p>
<p class="STYLE2">书名:${book.bookname}<br/>
作者:${book.authod} </p>
<p class="STYLE1"><a href="book.do?method=showBook&bookid=${book.id}">查看</a></p>
</logic:iterate>
</logic:present>
<p align="center" class="STYLE1"> <a href="<%=action%>pageNo=1">第一页</a>
<% if(nextOk==1){%>
<a href="<%=action%>pageNo=<%=prepage%>">上一页</a>
<%}else{%>
<span class="grayText">上一页</span> 
<%}%>
<%if(preOk==1){%>
<a href="<%=action%>pageNo=<%=nextpage%>">下一页</a>
<%}else{%>
<span class="grayText">下一页</span> 
<%}%>
<a href="<%=action%>pageNo=<%=totalPages%>">最后一页</a>
到
<input name="pageInput" type="text" value="1" size="2">
<input type="submit" name="Submit" value="GO">
</p> <p align="center" class="STYLE1"> </p>
<p align="center" class="STYLE1"> </p>
<br>
</body>
</html:html>大家帮帮忙啊 我实在是没有办法了
解决方案 »
- 错误: 在类 StudentTest 中找不到主方法, 请将主方法定义为: public static void main(String[] args)
- 如何将游览器的后退和前进按钮改成自己定义的地址
- 后台报错Connection reset by peer: socket write error
- window.open中传参数的问题!!!
- 关于下拉菜单的问题,在线等待,急!急!急!急!急!
- Int 到String类型转换问题,在线等待
- Access数据库保存中文问题
- 求救:关于Apach+Tomcat
- tomcat的server.xml的配置问题,请大家帮帮忙!
- 如何设置dreamwaver的编辑字体,默认得太难看了
- 网页中如何使用flv视频文件
- 雷人山寨版搜索
java.lang.ClassCastException: java.lang.Long
在BrowserBook中异常
是这个函数出错
java.lang.ClassCastException: java.lang.Long
在BrowserBook中异常
在正常模式中
java.lang.ClassCastException: java.lang.Long
在BookServiceImpl中的BookCountRecord方法出错
我崩溃中……
导致了一系列的问题
如jsp页面取不到值等等请问这个函数有错嘛?
public int BookCountRecord()
{
Session session=MySessionFactory.getSession();
Transaction tx=null;
int resuilt=0;
try
{
String hql="select count(*) from Book";
Query query=session.createQuery(hql);
query.setMaxResults(1);
resuilt=((Integer)query.uniqueResult()).intValue();
resuilt=(int)resuilt;
tx.commit();
}
catch(HibernateException e)
{ System.out.println(e);
System.out.println("在BookServiceImpl中的BookCountRecord方法出错");
if(tx!=null)
tx.rollback();
}
finally
{
MySessionFactory.closeSession();
}
return resuilt;
}
id匹配的是 Integer 类型 我检查过了
没发现有Long 类型的数据和强制转换成Long 的
Integer 而在2.5以后都是返货Long 类型
我那个崩溃
可以这样,count返回的值是int或者是long是由数据库的驱动所决定的。
上面的代码不管返回的是int还是long都是可以的