How to get number of rows return in SELECT query
i'm very new in java, i have a question:
- How to get number of rows return in SELECT query?
(i use SQL Server 2000 Driver for JDBC and everything are done, i only want to know problems above)
Thanks.
make the result set scroll insensitve, do rs.last(), get the row num, and call rs.beforeFirst(), then you can process the result set like you currently do.
String sql = "select * from testing";
PreparedStatement ps =
con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = ps.executeQuery();
rs.last();
System.out.println("Row count = " + rs.getRow());
rs.beforeFirst();~Tim
NOTE: Ugly, but does the trick.
Similar Messages
-
How to restrict number of rows returned in a query
Hi frnds,
I'd like to restrict number of rows returned by my query to some 10 rows. how to do that.When I try doing with the rownum<10 its giving results for a particular dept and that too some 6 rows only...btw I'm grouping my table and includes joins from many a table and am ordering the table results by a column.. How to do this..776317 wrote:
Hi frnds,
I'd like to restrict number of rows returned by my query to some 10 rows. how to do that.When I try doing with the rownum<10 its giving results for a particular dept and that too some 6 rows only...btw I'm grouping my table and includes joins from many a table and am ordering the table results by a column.. How to do this..
TELL ME HOW MANY ROWS YOU HAVE IN TABLE?
Because you have only *6 rows* in you column, if you less than 10 rows then it displays only containied/exist rows. nothing much
select ename,empno from emp where rownum < 10;Thanks -
Hi!
Can someone tell me how to obtain the number of rows returned by a query?
I tried to use getFetchSize() but it seems to always return 0 for my query.
THANKS in advance!
ResultSet testRS = Stmt.executeQuery(testStmt);
int size = testRS.getFetchSize();
System.out.println("size = " + size);
Wo LayTo get the number of rows returned, you have to loop
through the result set, or do a "select count" (which
is more efficient since it doesn't have to pass the
rows across the network) ...Looping around the entire result set is not very performance efficient. Then agin if you use count (*) you actually execute an extra query.
The best thing you can do is:// assume res is your result set.
res.last();
int rows = res.getRow();This way you only have to execute one query and you don't really need to loop around the entire result set. Of course you will have to have a scrollable result set, if this is supported by your DB.
Hope that helped.
afotoglidis -
Number of rows returned in a query
Is there a way to get the number of rows returned from a query without itterating through the itterator?
Thanks,
YonNo.
Because rows are not all returned until you start interating.
Some times I have seen people do a count query before the normal query, this helps out if you need a count to set things up. -
How to get number of rows and columns in a two dimensional array ?
Hello,
What would be the simplest way to get number of rows and columns in a two dimensional array represented as integers ?
I'm looking for another solution as For...Each loop in case of large arrays.
Regards,
PetriHi Petri,
See a attached txt file for obtaining two arrays with upper and lower index values
Regards
Ray
Regards
Ray Farmer
Attachments:
Get2DArrayIndex.txt 2 KB -
Get Number of rows from a sql query.
I am reading data from a sql query in a BLS transaction and I would like to know the number of rows returned.
Is there an easy way to do this without looping through the table?
Thanks JasperHi Jasper,
You can use the XPATH count function similar to this:
GetTagList.Results{count(/Rowsets/Rowset/Row)}
Kind Regards,
Diana Hoppe -
How to get number of rows in tableview
Hello,
Could anybody help me on how to get the number of rows in a tableview? Sort of using DESCRIBE TABLE in classic ABAP.
Thanks,
RickyThere is an attribute of the class CL_HTMLB_TABLEVIEW named
ROWCOUNT.
Inside an event u need to get this info use this code:
Replace 'Monitor' for the ID of your tableview
DATA: tv TYPE REF TO CL_HTMLB_TABLEVIEW,
r_count type i,
tv ?= CL_HTMLB_MANAGER=>GET_DATA(
request = runtime->server->request
name = 'tableView'
id = 'Monitor' ).
IF tv IS NOT INITIAL.
DATA: tv_data TYPE REF TO CL_HTMLB_EVENT_TABLEVIEW.
tv_data = tv->data.
r_count = tv_data->rowcount.
ENDIF. -
How to restrict number of rows returned in BIP
Hi Friends..
How to restrict no of rows displayed by the report to some 10 rows for example.. in BIPIf its in RTF you can use position to restrict.
<?for-each:ROW[position()<11]?>
You can also restrict it in your sql query using ROWNUM. -
Output the number of rows returned in a query
How do I find out the number of rows that are returned in a query?
COUNT function I guess can only be used when there is only one table involved. How do I do it for multi table query like JOIN?
For example
SELECT emp.first_name, emp.last_name, jobs.age FROM employees emp, jobs
WHERE emp.employee_id=jobs.employee_idHi,
You can get the num of rows info
select count(*) from (SELECT emp.first_name, emp.last_name, jobs.age FROM employees emp, jobs
WHERE emp.employee_id=jobs.employee_id) ;
regards
Jafar
http://www.oracledbarea.blogspot.com -
Need to limit number of rows returned by a query
Hi. Is there a way to limit the rowsets returned to me in a query? I need to get the first 100, then the next hundred, etc until there are no more rows left to retrieve. Someone told me that I should use a cursor to do this. Does anyone have a specific example? Can this be done with a combination of SQL/JDBC?
Thanks in advance...bbetta
nullif you are talking about limiting the number of rows to be returned to the calling program to a managable number of rowa ...
for example the result set size would be 5000 rows and you want to get them in batches of 50 or 100 ..
see the attached link :
JDBC Code Templates
http://technet.oracle.com/sample_code/tech/java/sqlj_jdbc/htdocs/templates.htm#BatchSize
if you want the server side transaction to only return the first 50 or 100 rows even though the result set has more ... then
another approach is required -- possibly as suggested above ... -
Number of rows returned by SELECT LAST(column)
I have about 50,000 rows in my MS Acess database table so I have used SELECT LAST(column) AS newField FROM table... to retrieve the last data in the column however when I check the number of rows in the resultset using
resultset.last();
int rowcount = rs.getRow();
rowcnt returns the total no.rows (about 50,000) in the table.
I thought it should just return one.
Is it normal?Thanks again dcminta. I'll try with your code.
I just wanted to know why resultset returned the number of all records in that column when I only selected the last.
I had the �Invalid Cursor Position� error with "while(rs.next())" as I (thought I) had only one record in the resultset and I was fiddling with my code.
They are all fine now.
Thanks guys.
Booh1(old lady) -
How to find number of rows inserted by dynamic query
EXECUTE IMMEDIATE l_sql
USING p_start_date, p_end_date
RETURN sql%rowcount into l_count;
Here l_sql is is selecting record from table_1 based on p_start_date, p_end_date and inserting record in table_2.
There is some further process to be done on table_2 which I want to do only if atleast 1 record is inersted in table_2 by the select query on table_1 for this I am using RETURN sql%rowcount into l_count;
I am geting error pls-00103 for this.
Is there any way to achieve this functionality.
ThanksIs there any way to achieve this functionality.Depends on your exact form of l_sql. Here are two ways to get the rowcount out of execute immediate:
SQL> create table emp_test as select * from emp where 1=2
Table created.
SQL> begin
execute immediate 'insert into emp_test select * from emp';
dbms_output.put_line ('Count: ' || sql%rowcount);
end;
Count: 14
SQL> rollback
Rollback complete.
SQL> declare
l_count integer;
begin
execute immediate 'begin insert into emp_test select * from emp; :1 := sql%rowcount; end;' using out l_count;
dbms_output.put_line ('Count: ' || l_count);
end;
Count: 14
PL/SQL procedure successfully completed. -
How do I get last 15 rows from a select query
Hello,
I am writing a query, and I need to filter the last 15 rows of from the select query.
For example: Select id, name,join_date from emp order by join_date;
So I need to get the last 15 rows from emp table so that we can who are the last 15 employees joined the organization. I want to show the result in ascending order, otherwise I would have taken first 15 rows with order by clause as descinding order.
Thanks in advance,
Param.SQL> select ename, join_date from test222
2 where rownum < 6
3 order by join_date
4 /
ENAME JOIN_DATE
CNC5461177794 14-FEB-02
OR91551178218 14-FEB-02
TUB9894676495 14-FEB-02
SG15431177117 14-FEB-02
PIB9384674017 14-FEB-02
SQL> SELECT * from
2 (select ename, join_date from test222
3 order by join_date desc)
4 WHERE rownum < 6
5 /
ENAME JOIN_DATE
II61111208401 19-MAR-02
II61451208038 18-MAR-02
PI20014184983 01-MAR-02
BNF2221148037C 28-FEB-02
UO78902581 18-FEB-02
Can you see the problem now ?
You can test this concept with your own table.
have a good day,
Sri -
Getting the number of rows returned from ResultSet
Hi,
Does anyone know a method to get the number of rows returned with a query using the Resultset class?
Thanks.Hi 281080,
If your database and JDBC driver support it, in order to use the solution that da-alexj has suggested, you need to create a 'scrollable' "ResultSet" -- the javadoc for method "createStatement()" (in class "java.sql.Connection") has more details.
However, I have found with Oracle 8.1.7.4 database and Oracle (thin) JDBC driver, that part of their implementation of the "last()" method (in class "java.sql.ResultSet") is to actually iterate through the entire "ResultSet" in order to reach the last row. If your "ResultSet" is very large (I tested it with a 100,000 row "ResultSet"), this will take a long time.
Just wanted to make you aware of that.
Of-course, this may be irrelevant to you since I didn't see any mention in your post of what database and JDBC driver you are using.
Hope this has helped you, anyway.
Good Luck,
Avi. -
Limiting number of rows returned by SQVI
I've created a query in SQVI and I need to limit the number of rows returned by the query. (I'm using the query as an exploratory tool, and it's not easy to predict how many records will be returned based on the selection fields I'm using.)
Is there any way to add a 'Maximum No. of Hits' field to the SQVI selection screen similar to what is found when using SE16?
Thanks,
BobIt's not surprising that you are confused because the documentation doesn't bother to explain what the "fetch size" actually is, it just says that setFetchSize sets it and getFetchSize gets it. As I understand it from some other documents I read about JDBC, the fetch size is a number that may be used internally by the JDBC driver. Here's an example of how I understand it (others, I know you will feel free to correct me if you disagree):
When the driver produces a result set with a very large number of records, it has to generate those records and deliver them to the system that requested them. If the database is not on the same system, then those records must all travel over the network. It could be a performance problem if you had to wait for (say) 80,000 records to travel across the network. Enter the fetch size. If you set the fetch size to 100, then the driver will bring the records across in batches of 100, as the program calls for them. Now, this buffering is transparent to your program; the driver doesn't tell you that it's getting another batch and you can't tell it to get another batch. So it is not a solution to the problem that everybody has here, namely how to display your records 10 per page and allow the user to go back and forth among those pages, like search engines do.
Maybe you are looking for
-
Creative cloud subscription problem
I have just purchased Creative cloud I can install programs only as trials. Adobe app manager is showing try at almost all programs I have chated with some adobe reps but they could'nt solve the problem Any ideas?
-
HELP!!!!
-
How to insert comments in the file resource_language.properties
Hi all, I am using Jdeveloper 11.1.1.0. I want to do the localization of my application, so I have defined the files resource_language1.properties, resource_language2.properties, and so on. There is a way to insert comments in these files? How can I
-
Importing packages at the same level
Hi, I have two packages A at B (with some classes) in the same level. How do i import the classes from one packages to other.......
-
Cannot post a comment on yahoo or watch a yahoo only video using Firefox
I used to be able to post a comment and watch videos using the Yahoo website. A few weeks ago, I can no longer post a comment or watch video on only the Yahoo site. Other website I can post to and watch their videos. If I go to Google Crome, I can po