?
Solved

how to retrieve all the rows of database table in an array using java

Posted on 2012-09-05
16
Medium Priority
?
394 Views
Last Modified: 2012-09-10
I am doing an application in java.
I want all the rows of my database table in an array.
As i want to pass this array to a jsp file..
(basically i want to display all the rows of my table)
Please provide me a code or link to do the same..
0
Comment
Question by:daydreamer6565
  • 7
  • 4
  • 3
  • +2
16 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 38367097
0
 
LVL 26

Expert Comment

by:Tomas Helgi Johannsson
ID: 38367351
Hi!

Here are two examples using Apache Commons library

http://www.java2s.com/Code/Java/Database-SQL-JDBC/DbUtilsUseBeanORM.htm
http://www.java2s.com/Code/Java/Database-SQL-JDBC/DbUtilsUseMapagainstMySQL.htm

Note that it can be used against any database.

Regards,
     Tomas Helgi
0
 

Author Comment

by:daydreamer6565
ID: 38367446
Thanks for the reply..
I tried the link given by expert CHEJ ..
but i think i have made some mistake as output is null..
I am attaching the codes pls check it and guide me...
list.txt
jsp.txt
DBConnect.txt
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:Sharon Seth
ID: 38367498
DisplayTag is another option you might want to try
0
 
LVL 12

Expert Comment

by:Sharon Seth
ID: 38367525
Object[] rslt_ar = rows.toArray(); - Move this line outside the while loop
0
 

Author Comment

by:daydreamer6565
ID: 38367576
I have moved - Object[] rslt_ar = rows.toArray();  outside the while loop still no change...
I am getting null value..
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 38368458
These examples are a little over-complicated.  Simply loop through the rows and display them in the jsp page, or put them in an array, and loop through the array in the jsp page.

So

rs = stmt.executeQuery("Select field1, field2, field3 from mytable");
while( rs.next() ) {
%>
<tr><td><%=rs.getString("field1")%></td><td><%=rs.getString("field2")%></td><td><%=rs.getString("field2")%></td></tr>
<%
}

Or put field1, 2, 3 into elements of an array, and loop through the array in your jsp page.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38368716
I tried the link given by expert CHEJ ..
but i think i have made some mistake as output is null..
Well yes, because you didn't actually use my code as much as copy it and change it, but you did that wrongly. Not sure why you felt the need to change it...
0
 
LVL 12

Expert Comment

by:Sharon Seth
ID: 38371185
Just after the for loop , print and see what's in newRow
After the while loop , print what's in rows .
I suspect the fetch from database is empty and hence you are not seeing any data
0
 

Author Comment

by:daydreamer6565
ID: 38378895
My code outputs Object array but the function should return String array..
I think this could be the possible reason why i am getting null as output instead of array..
will any of the experts help me out on how to convert the object array to a string array..
Please note that my database table also contains non string values.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 2000 total points
ID: 38379075
All you need is something like the following in your JSP:

List<List<Object>> allRows = DBUtils.resultSetToNestedList(resultSet);
JSONArray jsonObj=new JSONArray();
for(List<Object> row : allRows) {
        jsonObj.put(row);
}
out.println(jsonObj);

Open in new window

0
 

Author Comment

by:daydreamer6565
ID: 38379150
Ok thanks .. And what return type should I provide in my function?
I mean what does the function return ..I am confused what changes i have to make in my function will you please guide me what changes are to be made in my function..
0
 

Author Comment

by:daydreamer6565
ID: 38379170
Thanks CHEJ ..
I Got the output..
0
 

Author Comment

by:daydreamer6565
ID: 38381920
Expert CHEJ..  I have one more issue left with this question..
Is it possible to output the Json in key-value pairs?
some thing like-

["id": 37,
"name": "Aathira",
"alianme": "aathu",
"chrg": 9000],
["id": 18,
"name": "Aayisha",
"alianme": "shan",
"chrg": 900],

(As i want to populate a json grid and the grid takes only key-value pairs..)

I am attaching the output which i am getting now..Please do consider this question..
output.txt
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38382411
Is it possible to output the Json in key-value pairs?

I'd need to consider that as a new question
0
 

Author Comment

by:daydreamer6565
ID: 38382435
Ok i will post this as a new question please do consider it ..
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month17 days, 9 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question