• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 428
  • Last Modified:

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

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
daydreamer6565
Asked:
daydreamer6565
  • 7
  • 4
  • 3
  • +2
2 Solutions
 
Tomas Helgi JohannssonCommented:
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
 
daydreamer6565Author Commented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Sharon SethCommented:
DisplayTag is another option you might want to try
0
 
Sharon SethCommented:
Object[] rslt_ar = rows.toArray(); - Move this line outside the while loop
0
 
daydreamer6565Author Commented:
I have moved - Object[] rslt_ar = rows.toArray();  outside the while loop still no change...
I am getting null value..
0
 
mrcoffee365Commented:
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
 
CEHJCommented:
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
 
Sharon SethCommented:
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
 
daydreamer6565Author Commented:
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
 
CEHJCommented:
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
 
daydreamer6565Author Commented:
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
 
daydreamer6565Author Commented:
Thanks CHEJ ..
I Got the output..
0
 
daydreamer6565Author Commented:
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
 
CEHJCommented:
Is it possible to output the Json in key-value pairs?

I'd need to consider that as a new question
0
 
daydreamer6565Author Commented:
Ok i will post this as a new question please do consider it ..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 7
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now