Problem Solution Discussion Moving Around Within a Result Set

rs.close ; close result set } else { there is no result set, just print the row count System.out.println s.getUpdateCount + rows were affected; } s.close ; close statement } catch Exception e { Cookbook.printErrorMessage e; } Closing JDBC Statement and Result Set Objects The JDBC query- issuing exam ples in t his sect ion close t he st at em ent and result set obj ect s explicit ly when t hey are done wit h t hose obj ect s. Som e Java im plem ent at ions close t hem aut om at ically when you close t he connect ion. However, buggy im plem ent at ions m ay fail t o do t his properly, so it s best not t o rely on t hat behavior. Close t he obj ect s yourself when youre done wit h t hem t o avoid difficult ies.

2.6 Moving Around Within a Result Set

2.6.1 Problem

You want t o it erat e t hrough a result set m ult iple t im es, or t o m ove t o arbit rary rows wit hin t he result .

2.6.2 Solution

I f your API has funct ions t hat provide t hese capabilit ies, use t hem . I f not , fet ch t he result set int o a dat a st ruct ure so t hat you can access t he rows however you please.

2.6.3 Discussion

Som e API s allow you t o rewind a result set so you can it erat e t hrough it s rows again. Som e also allow you t o m ove t o arbit rary rows wit hin t he set , which in effect gives you random access t o t he rows. Our API s offer t hese capabilit ies as follows: • Perl DBI and Pyt hon DB- API dont allow direct posit ioning wit hin a result set . • PHP allows row posit ioning wit h t he mysql_data_seek funct ion. Pass it a result set ident ifier and a row num ber in t he range from 0 t o mysql_num_rows - 1 . Subsequent calls t o row- fet ching funct ions ret urn rows sequent ially beginning wit h t he given row. PHP also provides a mysql_result funct ion t hat t akes row and colum n indexes for random access t o individual values wit hin t he result set . However, mysql_result is slow and norm ally should not be used. • JDBC 2 int roduces t he concept of a scrollable result set , along wit h m et hods for m oving back and fort h am ong rows. This is not present in earlier versions of JDBC, alt hough t he MySQL Connect or J driver does happen t o support next and previous m et hods even for JDBC 1.12. Whet her or not a part icular dat abase- access API allows rewinding and posit ioning, your program s can achieve random access int o a result set by fet ching all rows from a result set and saving t hem int o a dat a st ruct ure. For exam ple, you can use a t wo-dim ensional array t hat st ores result rows and colum ns as elem ent s of a m at rix. Once youve done t hat , you can it erat e t hrough t he result set m ult iple t im es or use it s elem ent s in random access fashion however you please. I f your API provides a call t hat ret urns an ent ire result set in a single operat ion, it s relat ively t rivial t o generat e a m at rix. Perl and Pyt hon can do t his. Ot herwise, you need t o run a row- fet ching loop and save t he rows yourself.

2.7 Using Prepared Statements and Placeholders in Queries