| |

VerySource

 Forgot password?
 Register
Search
View: 900|Reply: 7

I only show the total number of pages, why can't they be displayed? Please help me!

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 United States

Post time: 2020-2-4 10:30:04
| Show all posts |Read mode
No error is explained, only blank.

<% @ 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> Untitled document </ 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 ();
Ranch
rs = sql.executeQuery ("select * from student");
rs.last ();
int lastRow = rs.getRow ();
pageCount = (lastRow% pageSize == 0)? (lastRow / pageSize) :( lastRow / pageSize + 1);
Ranch
Ranch
    %>
<table border = "1" width = "300" align = "center">
<tr align = "center" bgcolor = "pink">
<td> Student ID </ td>
<td> name </ td>
<td> age </ 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> Total <% = pageCount%> pages </ td>
</ tr>
</ table>
<%
con.close ();
}
catch (SQLException e1) {}
%>
</ body>
</ html>
Reply

Use magic Report

0

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-22 13:45:02
| Show all posts
Change select * from student to select count (*) as num from student.
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-23 10:00:01
| Show all posts
rs.last () is at the end, there is no next, rs.previous ()
I do n’t know if that ’s right, I ’m also a novice, haha,
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-3-25 16:00:01
| Show all posts
rs.last () moves the cursor to the last line
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-26 17:15:01
| Show all posts
You should encapsulate the result set and then traverse it in the following jsp page ...
I think it's still the result set
Reply

Use magic Report

0

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 Invalid IP Address

Post time: 2020-3-27 14:30:01
| Show all posts
Should be something wrong with rs.last ()
You print it (rs.getrow ()) and see if you get the value

Solution, move the pointer forward rs.previous () once
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-29 13:45:01
| Show all posts
When using JDBC (including Oracle JDBC extensions), there is no direct (that is, standard) way to use ResultSet or RowSet to get the number of rows returned by a query. But you can get this result with a few lines of code using Scrollable ResultSet or Cached RowSet. The details of the different methods that can be used are listed below.

One way is to execute "SELECT COUNT (*) ..." before the actual query.
This means that the database engine must analyze the same data twice (once for counting and once for the data itself).


The second method uses JDBC:
One using Scrollable ResultSet
The other uses a combination of a Cached RowSet and a normal (non-scrollable) ResultSet.
The JDBC method allows us to get the number of rows of a query without having to scan all the rows or perform a separate SELECT COUNT (*). Move to the end of the Scrollable ResultSet / Cached RowSet and get its position (resultset.last () / cachedRowset.last () and resultset.getRow () / cachedRowset.getRow ()) and you're done. RowSet extends the ResultSet interface, so we can use a normal ResultSet (rather than scrollable).

Instructions for using Scrollable ResultSet:

If the ResultSet is very large, resultset.last () can be a very time-consuming operation because it will use more resources on the server side. Therefore, you should avoid this method unless you really need a scrollable result set.
The Oracle JDBC driver will use resultset.getRow () to return the correct count. But implementation methods from other vendors may return zero by resultset.getRow ().

Code snippet:
Here is the snippet of the method mentioned earlier.
Use SQL query:
... // 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 ();
...............
 
 





Using 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 ();
...
 
 




Using 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 (); .............

Find it on the oracle official website, it should be this reason
Reply

Use magic Report

0

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 Invalid IP Address

Post time: 2020-3-30 00:30:01
| Show all posts
Using sa should be the sqlserver database ~~~~ not oracle
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list