Displaying Database Rows in JSP using Java Bean

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>

19 Responses to Displaying Database Rows in JSP using Java Bean

  1. dhinesh says:

    goog example

  2. Aavas says:

    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!

  3. Prashanth Yeredkeremath says:

    Thanks a lot !!

  4. Steve says:

    Thank you for a wonderful listing …. saved me tons of time. 🙂
    Steve

  5. nello says:

    Hi,
    i encountered an exception report: Unable to compile class for JSP

  6. prakash says:

    thanks for the code , realy helped !!

  7. tangara says:

    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.

  8. harish says:

    thanks for this article with jsp nad bean

  9. Poonam says:

    yess.. its very helpfull.

  10. rathina says:

    thanks

  11. ravi says:

    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

  12. Debo says:

    please you used a package. is it neccesarily i should use a package as well?

  13. Debo says:

    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

  14. Sushant says:

    Hey Really very nice example……thanx

  15. MNMohal says:

    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

Leave a comment