日期:2014-05-20  浏览次数:20752 次

jdbc分页问题?
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbUrl, dbUserName, dbPass);
sta = dbConn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

rs = sta.executeQuery(sqlSbs);
rs.last();
total=rs.getRow();
rs = sta.executeQuery(sqlSbs+" where rownum >"+start+" and rownum<"+limit);








rs = sta.executeQuery(sqlSbs+" where rownum >"+start+" and rownum<"+limit);
这句有什么问题么?

报错:exception:String index out of range: -1

------解决方案--------------------
应该不是这行的错吧。。。
应给类似下面的代码。。。

String s = "abc";
System.out.println(s.substring(2,1)) ;

会报
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
------解决方案--------------------
分页不是使用 CachedRowSet 提供的方法吗
------解决方案--------------------
就是,都不知道你写分页的代码,就像这句rs = sta.executeQuery(sqlSbs+" where rownum >"+start+" and rownum<"+limit);
sqlSbs里面的内容是什么啊
------解决方案--------------------
int sum = start +limit;
String sqls="SELECT * FROM(SELECT A.*,ROWNUM RN FROM("+sqlSbs+") A where ROWNUM <="+sum+") WHERE RN >"+start;
rs = sta.executeQuery(sqls);//只能执行变量

oracle分页特定方法~
------解决方案--------------------
重新弄一个statement吧。
sta1 = dbConn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = sta.executeQuery(sqlSbs+" where rownum >"+start+" and rownum<"+limit);
------解决方案--------------------
详情点击
http://blog.csdn.net/yao__shun__yu/article/details/7960373

Java code

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.aps.db.conn.OracleConn;

public class CopyLimit {

    private Connection conn;
    private PreparedStatement ps;
    private ResultSet rs;
    private String table;
    public String getTable() {
        return table;
    }
    public void setTable(String table) {
        this.table = table;
    }
    
    /**
     * 定义分页信息
     * pageSize 每页5条记录
     * pageNum 每页5个小分页项
     * */
    public final static String pageSize = "5";
    public final static String pageNum = "5";
    
    /********************************
     * 模仿limit
     * @param table 表名
     * @param odby  以谁排序、建议id
     * @param start 从数字几开始 1代表第一条数据
     * @param sum   取出多少数据
     * @throws SQLException 
     ********************************/
    public ResultSet doLimit(String table,String odby,String start,String sum) throws SQLException{
        /*这样保证table能被下面用*/
        this.table = table;
        setTable(table);
        String sql =     "select a.* from "+table+" a where rowid in (" +
                        "select rd from (" +
                        "select rownum rm,rd from (" +
                        "select rowid rd from "+table+" order by "+odby+") where rownum <= ?+?) where rm > ?)";
        /*获得连接*/
        conn = OracleConn.getConn();
        ps = conn.prepareStatement(sql);
        
        //起始位置
        start = String.valueOf((Integer.parseInt(start) - 1) * Integer.valueOf(pageSize));
        //赋值
        ps.setString(1, start);
        ps.setString(2, sum);
        ps.setString(3, start);
        return ps.executeQuery();
    }
    /**
     * 视图里面不能用rowid所以单写出来一个rownum分页
     * @param table
     * @param start
     * @param sum
     * @return
     * @throws SQLException
     */
    public ResultSet doVLimit(String table,String odby,String start,String sum) throws SQLException{
        /*这样保证table能被下面用*/
        this.table = table;
        setTable(table);
        String sql =     "select v.* from (" +
                        "select rownum rm,v.* from "+table+" v " +
                        "where rownum <= ?+? order by "+odby+") v where rm > ?";
        /*获得连接*/
        conn = OracleConn.getConn();
        ps = conn.prepareStatement(sql);
        
        //起始位置
        start = String.valueOf((Integer.parseInt(start) - 1) * Integer.valueOf(pageSize));
        //赋值
        ps.setString(1, start);
        ps.setString(2, sum);
        ps.setString(3, start);
        return ps.executeQuery();
    }
    /**
     * 获得总数据条数
     * @return
     * @throws SQLException
     */
    public String doCount() throws SQLException{
        String count = "";
        conn = OracleConn.getConn();
        ps = conn.prepareStatement("select count(*) from "+getTable());
        rs = ps.executeQuery();
        if(rs.next()){
            count = rs.getString(1);
        }
        
        return count;
    }
    /**********************
     * 获取到能分出来多少页
     * @throws SQLException 
     **********************
     */
    
    public String doPageS() throws SQLException{
        String pages = doCount();
        double pc = Double.parseDouble(pages)/5.0;
        if(pc*10%10 > 0){
            pc = (int)pc+1;
        }
        pages = String.valueOf((int)pc);
        return pages;
    }
}