首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

小弟我只做显示总页数,为什么显示不出来?请大家帮看看

2011-12-05 
我只做显示总页数,为什么显示不出来?请大家帮看看!没有说明错误,只显示空白.%@pagecontentType text/ht

我只做显示总页数,为什么显示不出来?请大家帮看看!
没有说明错误,只显示空白.

<%@   page   contentType= "text/html;   charset=gb2312 "   language= "java "   import= "java.sql.* "   errorPage= " "   %>
<%@page   import= "java.io.* "%>
<!DOCTYPE   HTML   PUBLIC   "-//W3C//DTD   HTML   4.01   Transitional//EN "   "http://www.w3.org/TR/html4/loose.dtd ">
<html>
<head>
<meta   http-equiv= "Content-Type "   content= "text/html;   charset=gb2312 ">
<title> 无标题文档 </title>
</head>

<body>
<%
          int   pageSize=5;
            int   pageCount=0;
          Connection   con;
  Statement   sql;
  ResultSet   rs;
  try{Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");}
  catch(ClassNotFoundException   e){}
  try{con=DriverManager.getConnection( "jdbc:odbc:stu ", "sa ", "sa ");
            sql=con.createStatement();
 
    rs=sql.executeQuery( "select*from   student ");
    rs.last();
    int   lastRow=rs.getRow();
    pageCount=(lastRow%pageSize==0)?(lastRow/pageSize):(lastRow/pageSize+1);
   
   
        %>
<table   border= "1 "   width= "300 "   align= "center ">
    <tr   align= "center "   bgcolor= "pink ">
            <td>   学号 </td>
    <td>   姓名 </td>
    <td>   年龄 </td>
      </tr>
      <%
              while(rs.next()){      
      String   stu_id=rs.getString(1);
String   stu_name=rs.getString(2);
String   sex=rs.getString(3);
    %>  
                <tr   align= "center "   >
                    <td> <%=stu_id%>   </td>
            <td> <%=stu_name%>   </td>
            <td> <%=sex%>   </td>
                        </tr>
        <%}%>  
</table>
        <table   align= "center ">
<tr>
      <td> 共 <%=pageCount%> 页 </td>
</tr>
</table>
<%
      con.close();
  }
  catch(SQLException   e1){}
  %>
</body>
</html>




[解决办法]
把select*from student 改成select count(*) as num from student在外边直接得到总数看一下!
[解决办法]
rs.last()已经在最后了,没有next了吧,rs.previous()


不知道说的对不对,我也是新手,呵呵,
[解决办法]
路过,学习
[解决办法]
rs.last()把游标移动到了最后一行
[解决办法]
你应该把结果集 封装起来 然后在下面的jsp页面中在 遍历出来........
我感觉 还是结果集的事
[解决办法]

应该rs.last()出了问题
你把它( rs.getrow() )打印出来看看,到底有没得到值

解决办法,指针前移rs.previous()一次
[解决办法]
使用 JDBC(包括 Oracle JDBC 扩展)时,没有直接的(即标准的)方法可以使用 ResultSet 或 RowSet 获得查询所返回的行数。但是可以通过很少几行代码使用 Scrollable ResultSet 或 Cached RowSet 来获得此结果。以下列出了可以使用的不同方法的详细内容。

一种方法是在实际查询前执行 "SELECT COUNT(*)... "。
这意味着数据库引擎必须对相同的数据进行两次分析(一次用于计数,一次用于数据本身)。


第二种方法使用 JDBC 2.0:
一种使用 Scrollable ResultSet
另一种使用 Cached RowSet 与普通(不可滚动)ResultSet 的组合。
JDBC 方法允许我们获得查询的行数而不必扫描所有的行或执行单独的 SELECT COUNT(*)。移到 Scrollable ResultSet/Cached RowSet 的尾部并获取其位置(resultset.last()/cachedRowset.last() 和 resultset.getRow()/cachedRowset.getRow()),即可完成所需的工作。RowSet 扩展了 ResultSet 接口,因此我们可以使用普通的 ResultSet(而不是可滚动的)。

使用 Scrollable ResultSet 的说明:

如果 ResultSet 非常大,则 resultset.last() 有可能是非常费时的操作,因为它将使用服务器端的更多资源。因此,除非确实需要可滚动结果集,应避免使用这种方法。
Oracle JDBC 驱动程序将使用 resultset.getRow() 返回正确的计数。但是其他供应商的实现方法可能会由 resultset.getRow() 返回零。

代码段:
以下是早先提及方法的代码段。
使用 SQL 查询:
................// Get a record count with the SQL StatementStatement stmt = connection.createStatement();ResultSet rs = stmt.executeQuery( "SELECT COUNT(*) AS rowcount FROM emp ");rs.next();// Get the rowcount column value.int ResultCount = rs.getInt(rowcount) ;rs.close() ;
...............




使用 JDBC Scrollable ResultSet: ..............

sqlString = "SELECT * FROM emp ";

// Create a scrollable ResultSet.
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);rs = stmt.executeQuery(sqlString);

// Point to the last row in resultset.
rs.last();
// Get the row position which is also the number of rows in the ResultSet.
int rowcount = rs.getRow();
System.out.println( "Total rows for the query: "+rowcount);

// Reposition at the beginning of the ResultSet to take up rs.next() call.
rs.beforeFirst();
................




使用 Oracle JDBC Cached RowSet

.........................
ResultSet rs = null;........................
// Create and initialize Cached RowSet object.
OracleCachedRowSet ocrs = new OracleCachedRowSet();

// Create a string that has the SQL statement that gets all the records.
String sqlString = "SELECT empno FROM emp ";


// Create a statement, resultset objects.
stmt = conn.createStatement();
rs = stmt.executeQuery(sqlString);
// Populate the Cached RowSet using the above Resultset.
ocrs.populate(rs);

// Point to the last row in Cached RowSet.
ocrs.last();

// Get the row position which is also the number of rows in the Cached
// RowSet.
int rowcount = ocrs.getRow();

System.out.println( "Total rows for the query using Cached RowSet: "+
rowcount);


// Close the Cached Rowset object.
if (ocrs != null)
ocrs.close();.............

在oracle官网上找的,应该是这个原因



热点排行