tags:

views:

153

answers:

3

Is there a direct method to get the all the elements in a row from the ResultSet as String? JDBC

+4  A: 

I've got to ask - why would you need something like that?

And no, it's not possible - you would need to call getString() at least once no matter what. The best you can do is to concatenate your fields in SQL, e.g.

SELECT col1 || ', ' || col2 || ', ' || col3 ... FROM my_table

and call resultSet.next().getString(1) to get that.

Update (based on comment) I don't think (that depends on your JDBC driver and database, really - you'll have to measure to find out) that there is a big difference in data volume between sending data (from DB to your app) by columns vs one concatenated string. In fact, the latter may even be more expensive if you have numbers / dates because they'll likely occupy more bytes formatted as text.

ChssPly76
Sometimes requirements are hard to believe! but they still exist :)I need to stream out the data line by line using tcp/ip socket and was looking for the most efficient solution.Will this be more efficient then concatenating at the java side using StringBuffer?
Vishal
If you want to have them really performant I would suggest using an stored procedure, and return the whole thing as a clob/blob or whatever it applies.
OscarRyz
But you're streaming data from your **application**, right? Read it from ResultSet field by field and format it into strings in Java. It may or may not be faster than doing the concatenation on DB side (only profiling will tell) but it'll certainly be cleaner. Issues like number / date formats among others come to mind, for example.
ChssPly76
@ChssPly76 I must say that I *hate* when people respond with non-answers like "Well why on earth would you wanna do that!?". Vishal said it best: sometimes you have requirements that are hard to believe, but they are requirements just the same. As it so happens, Microsoft's ADO Recordset API has just such a function, so obviously they saw a need for it there. Is it so unreasonable to ask if an equivalent exists on the Java side?
NobodyMan
@NobodyMan - do you also hate when people don't read before they comment? Because I very much responded to the question; however - since it's rather unusual - I wanted to know what prompted such a requirement.
ChssPly76
@ChssPly76 the answer was fine (and the reason I didn't vote you down). The snide commentary we can do without.
NobodyMan
@NobodyMan - let's be honest here. The reason you didn't vote me down is because with reputation at 65 you can't :-) How's that for a snide commentary? The one in my answer was a question; but I'll edit it to make it less ...umm... snide.
ChssPly76
Asking for the underlying reason for a given approach to a solution is perfectly valid in order to clarify if it is a given, strict requirement or just the first possible way to do it that popped in the askers mind. In the latter case other possible ways may be much, much better.
Thorbjørn Ravn Andersen
@ChssPly76 fair enough. Thanks for the edit. Apparently I *can* vote you up... so I did.
NobodyMan
@NobodyMan. I hate that too, but that's why here at stackoverflow there is a difference between answers and comments. In answers you'll usually just respond to the question( Just like ChssPly76 did ) In comments you can say whatever you want ( just like I'm doing right now ) http://bit.ly/S6Kdh
OscarRyz
+1  A: 

There's not a single method call to do it, but you can use something like this to build a String:

StringBuilder sb = new StringBuilder();
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
for (int i = 1; i <= numberOfColumns; i++) {
    sb.append(rs.getString(i));
    if (i < numberOfColumns) {
        sb.append(", ");
    }
}
String data = sb.toString();
Jason Day
+2  A: 

You may use BasicRowProcessor from Apache commons, Dbutils

ResultSet rs = ....
RowProcessor rp = new BasicRowProcessor();
Map m = rp.toMap( rs );

String s = m.toString();

And you'll have then as:

{ id = 1, name = Oscar, lastName = Reyes }

etc.

If you want to get rid of the columNames:

String s = m.values().toString();
OscarRyz
While I most certainly agree with your comment re: data throughput vs processing times I have to say that I found `BasicRowProcessor.toMap()` (and subsequent code to access that map) an absolute performance killer on large datasets. `toArray()` is better, raw ResultSet access is better yet.
ChssPly76
That's correct too!
Vishal