2

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

5
  • How would you want the data to be represented? Commented Oct 27, 2009 at 22:20
  • String data = "value1, value2, value3" but not using getString("") or similar methods and concatenating them! Commented Oct 27, 2009 at 22:21
  • 2
    Why don't you want to concatenate or use StringBuffer to create your own String? Commented Oct 27, 2009 at 22:27
  • Thanks, looking for another solution. Commented Oct 27, 2009 at 22:34
  • 1
    BTW if you are having performance problems, bear in mind that in most situations, the problem is not in the information processing, but the time spent on the network. So, instead of trying to tweak your row processing, you should better figure out how to send less info at a time. If you don't believe me, see how much does a java program takes to process 2 mb or data vs. sending 2 mb of data through the net. Commented Oct 27, 2009 at 22:47

3 Answers 3

6

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();
Sign up to request clarification or add additional context in comments.

1 Comment

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.
3

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();

1 Comment

This is very close but needs a little bit of tweaking. It should be wrapped in while(rs.next())
3

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.

10 Comments

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?
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.
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.
@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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.