留言板

retrive data from database

thumbnail
Abid Ali,修改在12 年前。

retrive data from database

Junior Member 帖子: 88 加入日期: 12-3-31 最近的帖子
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
thumbnail
David H Nebinger,修改在12 年前。

RE: retrive data from database

Liferay Legend 帖子: 14917 加入日期: 06-9-2 最近的帖子
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.
thumbnail
Abid Ali,修改在12 年前。

RE: retrive data from database

Junior Member 帖子: 88 加入日期: 12-3-31 最近的帖子
can u tell me elaborately. how i will write in service builder
thumbnail
Abid Ali,修改在12 年前。

RE: retrive data from database

Junior Member 帖子: 88 加入日期: 12-3-31 最近的帖子
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 ?
thumbnail
Priyanka Dhingra,修改在12 年前。

RE: retrive data from database

Liferay Master 帖子: 501 加入日期: 11-12-20 最近的帖子
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
thumbnail
Abid Ali,修改在12 年前。

RE: retrive data from database

Junior Member 帖子: 88 加入日期: 12-3-31 最近的帖子
its difficult to understand. please easy way to under stand.
thumbnail
Ravi Kumar Gupta,修改在12 年前。

RE: retrive data from database

Liferay Legend 帖子: 1302 加入日期: 09-6-24 最近的帖子
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.
thumbnail
Abid Ali,修改在12 年前。

RE: retrive data from database

Junior Member 帖子: 88 加入日期: 12-3-31 最近的帖子
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.
thumbnail
Ravi Kumar Gupta,修改在12 年前。

RE: retrive data from database

Liferay Legend 帖子: 1302 加入日期: 09-6-24 最近的帖子
Did you try these methods which are automatically created there..
Table1LocalServiceUtil.getXXX()

Use this in doview/render() or jsp to get data.
thumbnail
Abid Ali,修改在12 年前。

RE: retrive data from database

Junior Member 帖子: 88 加入日期: 12-3-31 最近的帖子
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
thumbnail
Ravi Kumar Gupta,修改在12 年前。

RE: retrive data from database

Liferay Legend 帖子: 1302 加入日期: 09-6-24 最近的帖子
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
thumbnail
devaraj s,修改在11 年前。

RE: retrive data from database

Regular Member 帖子: 228 加入日期: 12-5-21 最近的帖子
Hi abid,,
I have attached a PDF file go throgh that you will get a solution..
thumbnail
Anil T,修改在12 年前。

RE: retrive data from database

Expert 帖子: 313 加入日期: 12-1-14 最近的帖子
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.
thumbnail
meera prince,修改在10 年前。

RE: retrive data from database

Liferay Legend 帖子: 1111 加入日期: 11-2-8 最近的帖子
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,修改在10 年前。

RE: retrive data from database

Regular Member 帖子: 154 加入日期: 10-6-7 最近的帖子
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
thumbnail
Jan Geißler,修改在8 年前。

RE: retrive data from database

Liferay Master 帖子: 735 加入日期: 11-7-5 最近的帖子
Please open a new Thread for a new question.
Thanks.