Results 1 to 3 of 3
Thread: ResultSet to array
- 01-06-2011, 10:41 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 21
- Rep Power
- 0
ResultSet to array
Hey folk, I'm wondering if you guys can help me. I'm from a PHP background so I'm not too sure if I'm looking at this the wrong way - but I've grabbed some results from a database and need to put them into an array which I can send back to a JComboBox.
First of all, is it not possible to send the resultset back to the JComboBox in the necessary array? Or do I have to create a seperate array variable, loop through the results adding each result into newly created array?
The code I've got so far looks like this:
PHP Code:... try { sql = "SELECT title FROM domains"; rs = exec.getResults(sql); rs.last(); totRows = rs.getRow(); String[] domainList = new String[totRows]; for (int i = 0; i < domainList.length; i++) { domainList[i] = rs.getString(i); //This line doesn't actually work at the moment? } return domainList; } catch (Exception e) { String[] domainError = { "Error returning Domains", }; return domainError; } ...
Is it not easier (or is it even possible in Java) to do the following?
Many thanks in advance!PHP Code:.. String[] domainList = new String[totRows]; while(rs.next()){ //append to domainList array } return domainList; ...Last edited by Jojomofoman; 01-06-2011 at 10:44 PM.
- 01-07-2011, 01:17 AM #2
It is usually better to not pass a result set around. it is kind of like a bookmark or a cursor, and has open handles or resources in the statement object, which in turn has open resources in the connection object.. So you should loop thru the results and populate an array. In situations where there are very very large number of results, I then do a row handler, such as implement some interface i design with an "handleRow" method (which might even pass in a result set object, as the understanding is the result set is only used for reading 1 row into a bean an doing something with it.)First of all, is it not possible to send the resultset back to the JComboBox in the necessary array? Or do I have to create a seperate array variable, loop through the results adding each result into newly created array?
To create a list of stuff from a result set in Java, maybe something like,
where if you really wanted an array of string from that, you could wrap that function withJava Code:/** * Gets the list of domain names from the domains table in the database. * @return the list of string domain names sorted alphabetically. */ public List<String> getDomainList() throws SQLException { List<String> result = new ArrayList<String>(); Connection con = null; PreparedStatement stmt = null; ResultSet rset = null; try { con = dataSource.getConnection(); // e.g. dataSource is resolved from JNDI connection pool resource, or commons-dbcp pool stmt = con.prepareStatement("select title from DOMAINS order by title"); // always used prepared statements!!! rset = stmt.executeQuery(); while (rset.next() ) { result.add(rset.getString("title")); } // while } finally { // yes, we need all of this close stuff too. if (rset != null) { try { rset.close(); } catch (SQLException ex) { /* empty */ } } if (stmt != null) { try { stmt.close(); } catch (SQLException ex) { /* empty */ } } if (con != null) { try { con.close(); } catch (SQLException ex) { /* empt */ } } } return result; }
where the real worker here is the toArray() method of the list object that converts the list contents into an array of the type of the parameter, and we cast it back to string array for our results.Java Code:String[] domainList = null; try { List<String> aList = getDomainList(); domainList = (String[]) aList.toArray(new String[0]); return domainList; } catch (Exception ex) { String[] domainError = { "Error returning Domains:" + ex.getMessage() , }; return domainError; }
- 01-08-2011, 09:10 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 21
- Rep Power
- 0
Thanks so much for your help travishein, I decided to go with your first example as the number of results returned from the MySQL query could potentially be quite a lot.
Thanks for pointing the above out to me too, being new to Java it's this kind of stuff which will help me improve!
Similar Threads
-
How do I put a ResultSet in an Object?
By Azuxard in forum JDBCReplies: 2Last Post: 03-31-2009, 11:40 PM -
Help with Resultset
By xxAlemanxx in forum JDBCReplies: 6Last Post: 06-24-2008, 11:09 AM -
ResultSet to XML
By Java Tip in forum Java TipReplies: 0Last Post: 02-14-2008, 09:50 AM -
Empty ResultSet
By Java Tip in forum Java TipReplies: 0Last Post: 02-09-2008, 08:36 PM -
ResultSet example
By Java Tip in forum Java TipReplies: 0Last Post: 01-20-2008, 08:59 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks