Wednesday, 26 June 2013

Retrieve Data from Database in Liferay.

Hi friends today i am going to show you how to retrieve data from database.In my previous post i have shown you how to insert data in database.Here are the steps to retrieve data:

Finder Method


1)Open Service.xml and write the finder method by which field you want to retrieve data.As shown below i have searched for title so code is as below.

             <finder name="title" return-type="Collection">
<finder-column name="title" />
             </finder>


2)Now open FoolocalServiceImpl.java an write down the below Method in it:-
       
     public List<Foo> getBytitle(String title) throws SystemException {
return FooUtil.findBytitle(title);

}

3)Build Service from ant.

4)Now open your Java Class and write this following code to retrieve data:-

                java.util.List<Foo> vv=FooLocalServiceUtil.getBytitle(title);

System.out.println("sdfsef"+vv);

4)Compile your java class and deploy it .If everything goes fine then you will see the data in your console.

By this method you can retrieve data from database in console.The other Method to retrieve the data in your JSP page is as follows

Data in view.jsp

1)Open your jsp page where you wish to show the data and write the below code

<%@page import="com.control.controller"%>
<%@page import="java.util.*" %>
<portlet:defineObjects />

<% int usercount = FooLocalServiceUtil.getFoosCount();
List<Foo>users = FooLocalServiceUtil.getFoos(0, usercount);
for(Foo user:users){
out.print("name:" + user.getUserName());
out.print("\ntitle:" + user.getTitle());
}

%>

2)Now Deploy the project and you will see that your whole data from database is being shown in your liferay page.
On inserting data in database it will show the data in your liferay page .


If there is any problem plz do comment or email us at
chiragmsc007it@gmail.com
mehdisunasara@gmail.com