Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Abid Ali
retrive data from database
April 17, 2012 4:16 AM
Answer

Abid Ali

Rank: Junior Member

Posts: 89

Join Date: March 30, 2012

Recent Posts

hi,
I successfully stored datas in database(mysql) using liferay,
now i want to retrive data using primerykey from mysql.
how i will do it
David H Nebinger
RE: retrive data from database
April 17, 2012 7:36 AM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 6823

Join Date: September 1, 2006

Recent Posts

Hmm, there's jdbc, a little higher up from that is Hibernate, and if you're doing things the Liferay way then you've been using ServiceBuilder and the methods already exist.
Abid Ali
RE: retrive data from database
April 17, 2012 11:09 PM
Answer

Abid Ali

Rank: Junior Member

Posts: 89

Join Date: March 30, 2012

Recent Posts

can u tell me elaborately. how i will write in service builder
Ravi Kumar Gupta
RE: retrive data from database
April 17, 2012 11:14 PM
Answer
Abid Ali
RE: retrive data from database
April 18, 2012 2:16 AM
Answer

Abid Ali

Rank: Junior Member

Posts: 89

Join Date: March 30, 2012

Recent Posts

Ravi,
u gave example for store the data in database. but stored successfully using persistence Api. but i want to retrive data using primarykey.
i want to know how ?
Priyanka Dhingra
RE: retrive data from database
April 18, 2012 3:05 AM
Answer

Priyanka Dhingra

Rank: Liferay Master

Posts: 501

Join Date: December 19, 2011

Recent Posts

Hi Abid,
You want to fetch all the data through primary key or you want it to be like, you enter some primary key and then u get that particular data???

Anyhow I used <finder> in service.xml to fetch the content and display in portlet

try: http://www.liferay.com/community/wiki/-/wiki/Main/Finder+tags+in+service.xml
Abid Ali
RE: retrive data from database
April 18, 2012 4:05 AM
Answer

Abid Ali

Rank: Junior Member

Posts: 89

Join Date: March 30, 2012

Recent Posts

its difficult to understand. please easy way to under stand.
Ravi Kumar Gupta
RE: retrive data from database
April 18, 2012 6:52 AM
Answer

Ravi Kumar Gupta

Rank: Liferay Legend

Posts: 1145

Join Date: June 23, 2009

Recent Posts

Abid,

If you are using service builder, then you can get data from XXXLocalServiceUtil.java or XXXServiceUtil.java(this will need permission check as well). I hope you understand this one.

If you are not using service builder, I would suggest-start using. Anyways.. the way you have inserted data to database, the same persistence api won't provide methods to retrieve data?

I am kind of not getting you here properly, can you please share the code/portlet here.
Abid Ali
RE: retrive data from database
April 18, 2012 10:55 PM
Answer

Abid Ali

Rank: Junior Member

Posts: 89

Join Date: March 30, 2012

Recent Posts

here I develop code for storing data in database from portal.
1.view.jsp
---------------

<%@page import="javax.portlet.PortletContext"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>


<portlet:actionURL name="" var="actionUrl"></portlet:actionURL>


<aui:layout>
<aui:form name="fm" action="<%=actionUrl.toString() %>" method="post">
<aui:column>
<aui:input name="id" type="text"></aui:input>
<aui:input name="name" type="text"></aui:input>
<aui:input name="phone" type="text"></aui:input>
<aui:button value="submit" type="submit"></aui:button>
</aui:column>

</aui:form>
</aui:layout>


2.Service.xml
-------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.0.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_0_0.dtd">
<service-builder package-path="com.test">
<author>sourceone</author>
<namespace>Abid</namespace>

<entity name="Table1" local-service="true" remote-service="false" >

<!-- PK fields -->

<!-- Audit fields -->

<!-- Other fields -->

<!-- Order -->
<column name="id" primary="true" type="long"></column>
<column name="Name" type="String"></column>
<column name="Phone" type="long"></column>


</entity>
</service-builder>
3.InsertData.java(which is extends MVCPortlet)
----------------------------------------------------------------
package com.test;



import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;

import com.sun.xml.internal.bind.v2.runtime.Name;
import com.test.model.impl.*;
import com.test.service.*;

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
* Portlet implementation class InsertData
*/
public class InsertData extends MVCPortlet {


public void processAction(ActionRequest request,ActionResponse response)
{

long id =ParamUtil.getLong(request,"id");
String ename=ParamUtil.getString(request,"name");
long phoneno =ParamUtil.getLong(request,"phone");
try {
Table1LocalServiceUtil.add(id,ename, phoneno);
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}



}
4.Table1LocalServiceImpl.java
------------------------------------------

package com.test.service.impl;

import com.liferay.portal.kernel.exception.SystemException;
import com.test.model.Table1;
import com.test.model.impl.Table1Impl;
import com.test.service.Table1LocalServiceUtil;
import com.test.service.base.Table1LocalServiceBaseImpl;
import com.test.service.persistence.Table1Persistence;


public class Table1LocalServiceImpl extends Table1LocalServiceBaseImpl {
public Table1 add(long id, String Name,long Phone) throws SystemException
{
Table1 t = new Table1Impl();
t = new Table1Impl();
t.setId(id);
t.setName(Name);
t.setPhone(Phone);
try{
t = table1Persistence.update(t, false);
}
catch(SystemException e)
{
e.printStackTrace();
}
return t;

}

}
// this code is working properly.
but i want to fetch the data from database and show it in view.jsp
but i have done it in servlet but i want to do it in Liferay.
please give me that briefly.
Ravi Kumar Gupta
RE: retrive data from database
April 18, 2012 11:50 PM
Answer

Ravi Kumar Gupta

Rank: Liferay Legend

Posts: 1145

Join Date: June 23, 2009

Recent Posts

Did you try these methods which are automatically created there..
Table1LocalServiceUtil.getXXX()

Use this in doview/render() or jsp to get data.
Anil T
RE: retrive data from database
April 19, 2012 12:28 AM
Answer

Anil T

Rank: Regular Member

Posts: 194

Join Date: January 14, 2012

Recent Posts

Hi Abid,

I added your above code in my new portlet and it was deployed successfully. I found one method getTable1(long id) in Table1LocalServiceUtil.
You can get the value by passing your id to above class like Table1LocalServiceUtil.getTable1(id). Try this one in jsp page.
Abid Ali
RE: retrive data from database
April 19, 2012 2:43 AM
Answer

Abid Ali

Rank: Junior Member

Posts: 89

Join Date: March 30, 2012

Recent Posts

hi ravi,
I am using below code in view.jsp for getting data from database and for that i put jar file of before portlet in this portlet.
view.jsp
______
<%@page import="com.test.model.Table1" %>
<%@page import="com.test.service.*" %>
<%@page import="java.util.*" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<portletefineObjects />

<%
int userCount = Table1LocalServiceUtil.getTable1sCount();
List<Table1> users = Table1LocalServiceUtil.getTable1s(0, userCount);

for (Table1 user:users)
{
out.print("ID: " + user.getId());

out.print("\nName: " + user.getName());

out.print("\nphone: " + user.getPhone());
}
%>
// but my issue is getting data according to primery key one by one and print it as a form format
how i will do it plz give the code and with description. because i new in liferay
Ravi Kumar Gupta
RE: retrive data from database
April 19, 2012 3:11 AM
Answer

Ravi Kumar Gupta

Rank: Liferay Legend

Posts: 1145

Join Date: June 23, 2009

Recent Posts

I am not getting what you want to say by "but my issue is getting data according to primery key one by one and print it as a form format"

But if u want to get only one record.. and u have the key/id then there are other methods also.. like for a Foo Entity
FooLocalServiceUtil.getFoo(fooId)

I hope you get this.. if not, please do let us know in a more clear way.. emoticon
devaraj s
RE: retrive data from database
August 10, 2012 1:57 AM
Answer

devaraj s

Rank: Regular Member

Posts: 206

Join Date: May 21, 2012

Recent Posts

Hi abid,,
I have attached a PDF file go throgh that you will get a solution..
Attachments: liferay-portlet-development.pdf (609.7k)
Meera Prince
RE: retrive data from database
July 12, 2013 3:21 AM
Answer

Meera Prince

Rank: Liferay Master

Posts: 814

Join Date: February 8, 2011

Recent Posts

update the data in the mysql using jsp ,i have created the form in one jsp page and remaining logic in another jsp page.....

<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@page import="javax.portlet.PortletURL"%>
<%@page import="javax.portlet.RenderResponse"%>
<%@page import="javax.portlet.ActionRequest"%>
<%@ include file="/html/book/init.jsp" %>
<h1>Add / Edit Form</h1>

<portlet:renderURL var="renderURL ">
<portlet:param name="jspPage" value="/html/book/show.jsp"/>

</portlet:renderURL>

<aui:form name="fm" method="POST" action="<%=renderURL.toString()%>">
<aui:input name="bookId" label="enter book id"/>
<aui:input name="bookTitle" label="Enter Book Title"></aui:input>
<aui:input name="author"></aui:input>
<aui:button type="submit" value="save"></aui:button>

</aui:form>

<a href="<portlet:renderURL/>">&laquo; Go Back</a>




<%@page import="com.book.slayer.model.impl.BMSBOOKImpl"%>
<%@page import="com.liferay.taglib.portlet.RenderURLParamsTag"%>
<%@page import="com.book.slayer.service.BMSBOOKLocalService"%>
<%@page import="com.book.slayer.model.BMSBOOK"%>
<%@page import="com.book.slayer.service.BMSBOOKLocalServiceUtil"%>
<%@ include file="/html/book/init.jsp" %>
<%@ page import="java.util.*" %>
<%@ page import="com.book.slayer.*" %>
<%@ include file="/html/book/init.jsp" %>

<%
String bookTitle=request.getParameter("bookTitle");
String author=request.getParameter("author");
String bid=request.getParameter("bookId");
long bookId=Long.parseLong(bid);
BMSBOOK book=new BMSBOOKImpl();
book.setBookTitle(bookTitle);
book.setAuthor(author);
book.setBookId(bookId);
book.setDateAdded(new Date());

try{
BMSBOOKLocalServiceUtil.updateBMSBOOK(book);

}
catch (com.liferay.portal.kernel.exception.SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
%>
<h2>Data modified successfully</h2>
<h2>please click back to see the list of books. </h2>
<br/><a href="<portlet:renderURL/>">&laquo; Go Back</a>
David Ilechukwu
RE: retrive data from database
July 12, 2013 4:31 AM
Answer

David Ilechukwu

Rank: Regular Member

Posts: 114

Join Date: June 7, 2010

Recent Posts

I think Ravi Gupta's suggestion above is what you need to do.

Table1LocalServiceUtil.getXXX()
. These methods are autogenerated by ServiceBuilder for you.
If you need a step by step guide on how to get them generated on how to use them, please respond with the exact steps you have taken so far, wo that we can guide you more appropriately.

Regards