-2

Is there any way where I can display all columns of a result set without iterating over column.

Basically, I need to save the processing time consumed by the for loop part.

Sujeet kumar
  • 75
  • 1
  • 1
  • 7
  • 1
    You want a magical way of iterating without iterating? Why do you think this is possible? How do you know iteration is the expensive operation? Hint, iteration is not expensive. It's what you code inside the loop that takes the time. Do you even _know for sure_ that there is a performance problem? How did you decide this? What evidence to you have? As it stands, the question is not answerable. – Jim Garrison May 11 '18 at 21:47
  • @JimGarrison you just answered his question with 6 more questions :( – TwiN May 11 '18 at 21:52
  • It’s called asking for clarification, which is what comments are for. – Jim Garrison May 12 '18 at 03:09
  • My code basically reads data from DB (sql is configured in XML file) and writes data in a file. Initially I used two iterations, first while reading and second while writing. It was taking more than 4 hrs for 200 million records. Then I removed the second iteration by introducing a stringbuilder in read process and store all the 250 columns in that string separated by pipe. Now it's taking a bit more than an hour. I used some other performance enhancement processes like thread and running the job in parallel. I am wondering if performance can be enhanced more. I hope this answers all ur ques. – Sujeet kumar Jun 01 '18 at 00:23

2 Answers2

0

You HAVE to iterate over the resultset row, to get the columns,there is no way around it, even if you use any wrapper which may give you all column's, internally it would still iterate on the row of each resultset to get that data.

in case you want a generic way to iterate for all column's of a table you can use this approach

ResultSetMetaData md = rs.getMetaData(); 
int colCount = md.getColumnCount();  

for (int i = 1; i <= colCount ; i++){  
String col_name = md.getColumnName(i);  
System.out.println(col_name);  
}
user1933888
  • 2,897
  • 3
  • 27
  • 36
0

Even if your code doesn't iterate over the rows, whatever solution you're looking for will.

You can't print what you haven't read.

Update

Now that I'm thinking about it, there is actually a way, but it's not good practice. However, as the question is if you can do it and not if you should, here goes nothing:

You can modify your SQL query to combine all rows in 1 row, in which case you could display all columns of a result set without iterating over the rows.

Keep in mind that not only is it bad practice, you'd just be iterating somewhere else.

Using SQL Server: SQL Server: combining multiple rows into one row

Using MySQL: Can I concatenate multiple MySQL rows into one field?

Using Oracle: SQL Query to concatenate column values from multiple rows in Oracle

TwiN
  • 3,554
  • 1
  • 20
  • 31
  • Can not do that. Reason is I have a logic to transform few columns dynamically in the code. If I use one column with all columns appended, I won't be able to use the transformation logic. – Sujeet kumar Jun 01 '18 at 00:25