Displaying Database Rows in JSP using Java Bean
February 6, 2010 19 Comments
Last few hours I spend for this solution. I was not getting the exact idea to use ArrayList for an Object, and access methods of that Object from the JSP page. Later I found the solution to be like this, If you have better solution than this please to comment me.
Here is the JavaBean file were we access the data from the database and merge it to the List allData as objects of Datafields and returning the fields value using getter.
package beans; import java.sql.*; import java.util.ArrayList; import java.util.List; public class ShowDataBean { String error; List<Object> allData=new ArrayList<Object>(); public String fname,lname,email,password,gender; Object obj=new Object(); public void setObj(Object obj) { this.obj = obj; } public String getError() { return error; } public List getDb_Data() { int i=0; try { String qry; Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_eexam","root","root"); Statement s = con.createStatement(); qry="Select fname,lname,email,password,gender from tbl_user_primary"; ResultSet r=s.executeQuery(qry); while(r.next()) { DataFields d=new DataFields(r.getString(1), r.getString(2), r.getString(3),r.getString(4), r.getString(5)); allData.add(i,d); i++; } } catch(Exception ex) { error="<b>Contact Administrator :</b><br/>" + ex; System.out.println("Your query is not working" + ex); } return allData; } public String getFname() { this.fname=((DataFields)obj).fname; return this.fname; } public String getEmail() { this.email=((DataFields)obj).email; return this.email; } public String getGender() { this.gender=((DataFields)obj).gender; return this.gender; } public String getLname() { this.lname=((DataFields)obj).lname; return this.lname; } public String getPassword() { this.password=((DataFields)obj).password; return this.password; } public class DataFields { public String fname,lname,email,password,gender; public DataFields(String fname,String lname,String email,String password,String gender) { this.fname=fname; this.lname=lname; this.email=email; this.password=password; this.gender=gender; } } }
In JSP file we are accessing (getting) the fields after setting the object that is stored in each index of the List.
<%@ page import="java.util.*" %> <jsp:useBean id="showDataBean" class="beans.ShowDataBean" scope="request" /> <table width="900" cellspacing="0" cellpadding="0"> <tr> <td> Name </td> <td> Email </td> <td> PassWord </td> <td> Gender </td> </tr> <% List<Object> list=new ArrayList<Object>(); list=showDataBean.getDb_Data(); for(int i=0;i<list.size();i++) { showDataBean.setObj(list.get(i)); %> <tr> <td> <% out.print(showDataBean.getFname() +" "+ showDataBean.getLname()); %> </td> <td> <% out.print(showDataBean.getEmail()); %> </td> <td> <% out.print(showDataBean.getPassword()); %> </td> <td> <% out.print(showDataBean.getGender()); %> </td> </tr> <% }%> </table>
goog example
Hi,
This example was very helpful to me. Thanks a bunch. Displaying multiple rows from database in JSP page is quiet a difficult task for beginners. Your example servers just what is needed.
Thanks again!
You are welcome aavas…
Hello boss but instead of displaying the whole table it was displaying the same row multiple times.. Why i am getting like that could you help me please?
Thanks a lot !!
Thank you for a wonderful listing …. saved me tons of time. 🙂
Steve
Hi,
i encountered an exception report: Unable to compile class for JSP
First just try with simple jsp in localhost. Then only try with this code.
thanks for the code , realy helped !!
I tried to use your example except the package name I use another name – paginationinservlets.dto.
And I put the package folder and the StudentsDetailsDTO.java file(following your example) at the Source packages.(Using Netbeans IDE).
However, when I run the files, it gives me this error message:
Caused by: java.lang.IllegalArgumentException: Servlet mapping specifies an unknown servlet name paginationinservlets.dto.StudentsDetailsDTO
Please advise where have I gone wrong?
Thanks.
thanks for this article with jsp nad bean
yess.. its very helpfull.
thanks
why u combine bean code and database code
try to separate both is it possible
please give me separate code for both bean and db
please you used a package. is it neccesarily i should use a package as well?
i’m also asking the same. you used a package. And when i compiled and ran the program in my browser, it just showed the Colun fields and not the populated data in my database
Hey Really very nice example……thanx
thanks for your help gone through a lot of blogs and link but finally got help from your one but want to ask one thing what is the purpose of
public void setObj(Object obj) {
this.obj = obj;
}
code..
thanks once again
go through generic classes.. you will come to know….