Tribune

Home » Liferay Portal » English » 3. Development

Vista Combinata Vista Piatta Vista ad Albero
Discussioni [ Precedente | Successivo ]
toggle
Abid Ali
retrive data from database
17 aprile 2012 4.16
Risposta

Abid Ali

Punteggio: Junior Member

Messaggi: 90

Data di Iscrizione: 30 marzo 2012

Messaggi recenti

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
17 aprile 2012 7.36
Risposta

David H Nebinger

Community Moderator

Punteggio: Liferay Legend

Messaggi: 11060

Data di Iscrizione: 1 settembre 2006

Messaggi recenti

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
17 aprile 2012 23.09
Risposta

Abid Ali

Punteggio: Junior Member

Messaggi: 90

Data di Iscrizione: 30 marzo 2012

Messaggi recenti

can u tell me elaborately. how i will write in service builder
Ravi Kumar Gupta
RE: retrive data from database
17 aprile 2012 23.14
Risposta
Abid Ali
RE: retrive data from database
18 aprile 2012 2.16
Risposta

Abid Ali

Punteggio: Junior Member

Messaggi: 90

Data di Iscrizione: 30 marzo 2012

Messaggi recenti

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
18 aprile 2012 3.05
Risposta

Priyanka Dhingra

Punteggio: Liferay Master

Messaggi: 501

Data di Iscrizione: 19 dicembre 2011

Messaggi recenti

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
18 aprile 2012 4.05
Risposta

Abid Ali

Punteggio: Junior Member

Messaggi: 90

Data di Iscrizione: 30 marzo 2012

Messaggi recenti

its difficult to understand. please easy way to under stand.
Ravi Kumar Gupta
RE: retrive data from database
18 aprile 2012 6.52
Risposta

Ravi Kumar Gupta

Punteggio: Liferay Legend

Messaggi: 1303

Data di Iscrizione: 23 giugno 2009

Messaggi recenti

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
18 aprile 2012 22.55
Risposta

Abid Ali

Punteggio: Junior Member

Messaggi: 90

Data di Iscrizione: 30 marzo 2012

Messaggi recenti

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
18 aprile 2012 23.50
Risposta

Ravi Kumar Gupta

Punteggio: Liferay Legend

Messaggi: 1303

Data di Iscrizione: 23 giugno 2009

Messaggi recenti

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
19 aprile 2012 0.28
Risposta

Anil T

Punteggio: Expert

Messaggi: 292

Data di Iscrizione: 14 gennaio 2012

Messaggi recenti

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
19 aprile 2012 2.43
Risposta

Abid Ali

Punteggio: Junior Member

Messaggi: 90

Data di Iscrizione: 30 marzo 2012

Messaggi recenti

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
19 aprile 2012 3.11
Risposta

Ravi Kumar Gupta

Punteggio: Liferay Legend

Messaggi: 1303

Data di Iscrizione: 23 giugno 2009

Messaggi recenti

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
10 agosto 2012 1.57
Risposta

devaraj s

Punteggio: Regular Member

Messaggi: 206

Data di Iscrizione: 21 maggio 2012

Messaggi recenti

Hi abid,,
I have attached a PDF file go throgh that you will get a solution..
Allegati: liferay-portlet-development.pdf (609,7k)
Meera Prince
RE: retrive data from database
12 luglio 2013 3.21
Risposta

Meera Prince

Punteggio: Liferay Legend

Messaggi: 1110

Data di Iscrizione: 8 febbraio 2011

Messaggi recenti

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
12 luglio 2013 4.31
Risposta

David Ilechukwu

Punteggio: Regular Member

Messaggi: 128

Data di Iscrizione: 7 giugno 2010

Messaggi recenti

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
Jan Geißler
RE: retrive data from database
28 maggio 2015 1.12
Risposta

Jan Geißler

Community Moderator

Punteggio: Liferay Master

Messaggi: 735

Data di Iscrizione: 5 luglio 2011

Messaggi recenti

Please open a new Thread for a new question.
Thanks.