Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

ResultSet and fetchSize()

Thread view: 
Cruella DeVille - 11 May 2006 10:29 GMT
Hi
I'm trying to determin the cardinality of a resultset from a mysql
database, but the method fetchSize() returns 0 (zero). I know there's
data in the database, so this result is not correct. Is there other
ways to retreive the cardinality other than fetchSize()?

My code (if needed)

public String[] getKindergartenNames(){
        db.connect();
        String[] kindergartenNames = null;
        try{
            ResultSet s = db.query("select name from kindergarten");
            int size = s.getFetchSize();
            kindergartenNames = new String[size];
            System.out.println("size: " + size); // for debugging purposes
            int antall = 0;
            while(s.next()){
                String name =s.getString("name");
                //kindergartenNames[antall] = name;
                System.out.println(antall); // debugging
                antall++;

            }
        }catch(SQLException sqlE){
           
        }
        return kindergartenNames;
       
    }
Tobias Schröer - 11 May 2006 11:00 GMT
Hi,

why don't you simply return a List? Then you don't need to know the
fetch size to create the array.

I inserted the alternative code below (don't forget the java.utilList
and java.util.ArrayList imports). You can use other List implementations
than ArrayList if needed.

Cruella DeVille schrieb:
> Hi
> I'm trying to determin the cardinality of a resultset from a mysql
[quoted text clipped - 5 lines]
>
> public String[] getKindergartenNames(){
public List getKindergartenNames() {
>         db.connect();
>         String[] kindergartenNames = null;
        List kinderGartenNames = new ArrayList();
>         try{
>             ResultSet s = db.query("select name from kindergarten");
[quoted text clipped - 5 lines]
>                 String name =s.getString("name");
>                 //kindergartenNames[antall] = name;
                kindergartenNames.add(name);
>                 System.out.println(antall); // debugging
>                 antall++;
[quoted text clipped - 6 lines]
>        
>     }

If you're using Jave 1.5 you can use a generic list to make your result
typesave (List<String>).

Otherwise, the ResultSet implementation of the MySQL JDBC driver must
implement the getFetchSize() member properly. Maybe here's the fault?
I'm not sure, because I've never used MySQL with Java yet.

hth
Tobi
dayjah - 11 May 2006 11:11 GMT
> Otherwise, the ResultSet implementation of the MySQL JDBC driver must
> implement the getFetchSize() member properly. Maybe here's the fault?
> I'm not sure, because I've never used MySQL with Java yet.

The MySQL JDBC driver does not implement getFetchSize(), I think it
also misses out other nice features such as meta data - although, if my
memory serves me right, I read that it is the db that actually does not
pass meta data back but this changes with mysql 5 so we should start to
see cooler mysql jdbc driver functionality in due time.

dayjah
Rhino - 11 May 2006 17:01 GMT
>> Otherwise, the ResultSet implementation of the MySQL JDBC driver must
>> implement the getFetchSize() member properly. Maybe here's the fault?
[quoted text clipped - 5 lines]
> pass meta data back but this changes with mysql 5 so we should start to
> see cooler mysql jdbc driver functionality in due time.

You're mistaken about the metadata. I'm using MySQL 4.0.15 and the JDBC
driver does pass back metadata. In fact, this is the one way I can get
information about tables, etc. in MySQL 4.0.x, aside from the meager
information provided by the SHOW commands.

--
Rhino
dayjah - 12 May 2006 09:32 GMT
> You're mistaken about the metadata. I'm using MySQL 4.0.15 and the JDBC
> driver does pass back metadata.

Neat! What driver are you using?
dayjah - 11 May 2006 11:09 GMT
Hi Cruella :)

I'd suggest something like

public int countRows(rs) {
   rs.afterLast();  // note 1
   int size = rs.getRow() -1; // note 2
   rs.beforeFirst();  // note 3
}

note 1: this sets you to the end of your result set.
note 2: gets the number of the row you are at, I've not tested this so
I think you need to minus one from the number baring in mind the
previous line put you *after* the last row.
note 3: you need to rewind to the beginning of the result set otherwise
when you iterate through it the process will break.

Hope this helps,

dayjah
ahjiang@gmail.com - 11 May 2006 16:20 GMT
if im not wrong..fetchsize means that if you set the number  of rows
you want to fetch from the database and not the size of the resultset..

and using moving the result  to  last  and  first..is  an  expensive
operation..
dayjah - 12 May 2006 09:31 GMT
> if im not wrong..fetchsize means that if you set the number  of rows
> you want to fetch from the database and not the size of the resultset..

I just cheked the javadoc, you're right. There doesn't seem to be a
rs.countRows() at all. A shame because it would be a very handy
addition!

> and using moving the result  to  last  and  first..is  an  expensive
> operation..

Correct! However it seems the requirement is to count the number of
rows in the result set is there a less expensive way?
Nikola Vidovic - 12 May 2006 20:45 GMT
Yes, you pass select count(*) from KinderGarten query and get its result.
Few lines of code more but inexpensive.

Zvonko

> Correct! However it seems the requirement is to count the number of
> rows in the result set is there a less expensive way?
Tobias Schröer - 13 May 2006 23:23 GMT
Nikola Vidovic schrieb:
> Yes, you pass select count(*) from KinderGarten query and get its result.
> Few lines of code more but inexpensive.

You're sure? I'd say that a database request is quite expensive,
especially if you don't use things like PreparedStatements. At least,
this depends on the data amount and should be tested. Roughly, I'd
suggest that a small data count should preferrably be processed on Java
side, whereas I'd hand over large data counts to the Database.

Or read the ResultSet to a List, as I suggested before. Lists can easily
 be converted to arrays, if needed. The ResultSet size was only used
for array initialization.

Tobi

> Zvonko
>
>> Correct! However it seems the requirement is to count the number of
>> rows in the result set is there a less expensive way?
dayjah - 15 May 2006 14:59 GMT
> You're sure? I'd say that a database request is quite expensive,
> especially if you don't use things like PreparedStatements.

you are right - submitting the count would be way way expensive.

> Or read the ResultSet to a List, as I suggested before. Lists can easily
> be converted to arrays, if needed. The ResultSet size was only used
> for array initialization.

I think the issue here is memory use - we often have result sets
comprising of 100's of very wide tupples. I know what we do is
relatively small but assigning this to a list would essentially, at a
guess, double the amount of memory needed which in Cruella's case is
*just* debug. Rather expensive - sure fast forwarding to the end of the
result set and then back to the start is not a great solution but it is
the least expensive so far mentioned.

I wonder if there is some meta data that handles this? When you submit
a query to mysql you get a lot of information back, most notably there
is the number of rows returned and the time it took the query to run.
Perhaps these can be extracted some how?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.