package dao;
import java.util.ArrayList;
import datebase.DBOperException;
import datebase.DBSource;
import java.sql.*;
import struts.form.TestForm;
import vo.userVO;
import tool.Page;
public class userDAO {
String sql = null;
//控制分页的属性
int page = 1;
int maxPage = 1;
//最大记录数量
private int maxRecordNumber=0;
public int getMaxPage() {
return maxPage;
}
public int getPage() {
return page;
}
public void setMaxPage(int maxPage) {
this.maxPage = maxPage;
}
public void setPage(int page) {
this.page = page;
}
/**
* @return maxRecordNumber
*/
public int getMaxRecordNumber() {
return maxRecordNumber;
}
/**
* @param maxRecordNumber 要设置的 maxRecordNumber
*/
public void setMaxRecordNumber(int maxRecordNumber) {
this.maxRecordNumber = maxRecordNumber;
}
/**
*
* @param testForm FORM对象
* @param requestpage 被请求的页
* @param pageNum 每页的数量(显示)
* @return
* @throws DBOperException
*/
public ArrayList selectUSERBASICINFO(TestForm testForm,int requestpage,int pageNum) throws DBOperException{
//广告信息列表
ArrayList adlist = new ArrayList();
// 需要的数据库对象
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
//查询条件
sql = "select userid,userpassword,phone,email from userbasicinfo where sex=?";
// 得到连接
conn = DBSource.getConnection();
//pstmt = conn.prepareStatement(sql);修改之前的语句
pstmt = conn.prepareStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);//修改过的语句
pstmt.setString(1,testForm.getSex());
System.out.println(testForm.getSex());
//执行查询
rs = pstmt.executeQuery();
//得到最大的数量
int pageNumber = pageNum;
boolean last = rs.last();//将cursor移到最后
if(last){
int rowNumber = rs.getRow();//获取符合的数据条数
//设置列表中的显示数量
maxRecordNumber = rowNumber;
maxPage = rowNumber/pageNumber;
if(rowNumber % pageNumber > 0){
maxPage++;
}
}
//如果输入的页大于最大的页面,则选择最大的页面
if(requestpage >= maxPage){
page = maxPage;
}else{
this.page = requestpage;
}
//把查询到的历史信息组合成历史信息对象,然后添加到全部历史对象中
if(page > 1 && last){
rs.absolute(pageNumber * (page - 1));
}else if(page == 1 && last){
rs.beforeFirst(); //将cursor移到初始状态
}
//把查询到的历史信息组合成历史信息对象,然后添加到全部历史对象中
int currentPosition = 0;
//把查找到的全部广告基本信息对象放到集合对象中
while(rs.next()&& currentPosition < pageNumber && last){
//创建一个VO对象???????????到底放while外面还是放while里面
userVO uservo= new userVO();
uservo.setEmail(rs.getString("email"));
uservo.setPhone(rs.getString("phone"));
uservo.setUserid(rs.getString("userid"));
uservo.setUserpassword(rs.getString("userpassword"));
//把属性加入列表
adlist.add(uservo);
currentPosition ++ ;
}
} catch (SQLException ex) {
try {
conn.rollback();
} catch (SQLException e) {
e.printStackTrace();
}
ex.printStackTrace();
throw new DBOperException("数据库更新失败!");
} finally {
try {
// 关闭连接
rs.close();
pstmt.close();
conn.close();
} catch (SQLException ex1) {
ex1.printStackTrace();
}
}
//返回得到广告显示信息的列表
return adlist;
}
}
java.sql.SQLException: 对只转发结果集的无效操作: last
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:111)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:145)
at oracle.jdbc.driver.BaseResultSet.last(BaseResultSet.java:81)
at org.apache.tomcat.dbcp.dbcp.DelegatingResultSet.last(DelegatingResultSet.java:328)
at dao.userDAO.selectUSERBASICINFO(userDAO.java:86)
at struts.action.TestAction.execute(TestAction.java:54)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at tool.EncodeFilter.doFilter(EncodeFilter.java:42)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
2008-1-22 20:07:28 org.apache.struts.action.RequestProcessor processException
警告: Unhandled Exception thrown: class datebase.DBOperException
2008-1-22 20:07:28 org.apache.catalina.core.ApplicationContext log
信息: null
产生错误代码行:
pstmt = conn.prepareStatement(sql);
boolean last = rs.last();
将pstmt = conn.prepareStatement(sql);
改为:pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet.TYPE_SCROLL_INSENSITIVE 双向滚动,但不及时更新,就是如果数据库里的数据修改过,并不在ResultSet中反应出来。
ResultSet.TYPE_SCROLL_SENSITIVE 双向滚动,并及时跟踪数据库里的更新,以便更改ResultSet中的数据。
ResultSet.CONCUR_READ_ONLY 只读取ResultSet 。
ResultSet.CONCUR_UPDATABLE 用ResultSet更新数据库 。