Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
dave ch
retrieving data from mysql
March 27, 2012 9:39 PM
Answer

dave ch

Rank: Regular Member

Posts: 164

Join Date: February 6, 2012

Recent Posts

Hi all,

I already connected mysql with liferay 6.0. Can any body please tell me in depth that, how to retrieve data from mysql database to liferay 6.0. I am a newbie in liferay so i need some depth information.
Juhi Kumari
RE: retrieving data from mysql
March 27, 2012 10:17 PM
Answer

Juhi Kumari

Rank: Expert

Posts: 347

Join Date: December 12, 2011

Recent Posts

Hi deb,
If you want to retrieve all records from a table, suppose you want to retrieve all records from user_ table then use
1List<User> userList = UserLocalServiceUtil.getUsers(0, UserLocalServiceUtil.getUsersCount());

Or any particular records you can use
1User user = UserLocalServiceUtil.getUser(userId);
depends on your requirement.


Regards
Juhi
dave ch
RE: retrieving data from mysql
March 28, 2012 12:39 AM
Answer

dave ch

Rank: Regular Member

Posts: 164

Join Date: February 6, 2012

Recent Posts

thank you..
David H Nebinger
RE: retrieving data from mysql
March 30, 2012 7:00 AM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 7887

Join Date: September 1, 2006

Recent Posts

Juhi Kumari:
1UserLocalServiceUtil.getUsers(0, UserLocalServiceUtil.getUsersCount());


This is wrong and results in an extra database call.

Use
1UserLocalServiceUtil.getUsers(QueryUtil.ALL_POS, QueryUtil.ALL_POS);
instead.
Priyanka Dhingra
RE: retrieving data from mysql
March 27, 2012 10:25 PM
Answer

Priyanka Dhingra

Rank: Liferay Master

Posts: 501

Join Date: December 19, 2011

Recent Posts

I used

List<Employee> empList = new ArrayList<Employee>();
empList = EmployeeLocalServiceUtil.findAll();


and liferay ui:search container for displaying the records.

Regards
Priyanka
dave ch
RE: retrieving data from mysql
March 28, 2012 12:42 AM
Answer

dave ch

Rank: Regular Member

Posts: 164

Join Date: February 6, 2012

Recent Posts

Thank you Priyanka..
RathnaDevi Chellaiah
RE: retrieving data from mysql
March 28, 2012 2:20 AM
Answer

RathnaDevi Chellaiah

Rank: Junior Member

Posts: 29

Join Date: March 26, 2012

Recent Posts

Hi Priyanka,
Can u tell me how to retrieve data using servicebuilder created in one portlet to another portlet.? i already created a portlet with service builder to insert data into database thereafter created another portlet and copied the firs portlet's jar(WEB-INF/lib) to the new one's lib.. still its not working ? after searching some forum, i copied the jar to tomcat's lib..Even that doesn't work.... Is there any solution??????? am using liferay-plugins-sdk-6.0.6-20110225 and liferay-portal-6.0.6 tomcat for my application.

Thanks in advance.
Jitendra Rajput
RE: retrieving data from mysql
March 28, 2012 3:20 AM
Answer

Jitendra Rajput

Rank: Liferay Master

Posts: 824

Join Date: January 7, 2011

Recent Posts

Rathna ,

If i am not wrong then you want to share your services between plugin portlets.
You can do it via two ways ..

1)
You created a service for producer portlet named producer-service.jar .
Now remove this jar from your portlet/WEB-INF/lib and if its placed inside any other location remove from thr also .
and place inside tomcat/lib/ext

restart your server . Now you can easily access produce service jar inside any other portlet as its placed inside global class path


2) Copy your produce-service.jar and place inside portlet's WEB - INF / lib which want to access this services.

make sure your produce portlet is deployed other wise you might rcv beanlocator is null kind of exception

look into below wiki for more info
http://www.liferay.com/community/wiki/-/wiki/Main/Using+Class+Loader+Proxy+classes+to+share+plugins+services
David H Nebinger
RE: retrieving data from mysql
March 30, 2012 7:02 AM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 7887

Join Date: September 1, 2006

Recent Posts

Jitendra Rajput:
Rathna ,

If i am not wrong then you want to share your services between plugin portlets.
You can do it via two ways ..

1)
You created a service for producer portlet named producer-service.jar .
Now remove this jar from your portlet/WEB-INF/lib and if its placed inside any other location remove from thr also .
and place inside tomcat/lib/ext

restart your server . Now you can easily access produce service jar inside any other portlet as its placed inside global class path


2) Copy your produce-service.jar and place inside portlet's WEB - INF / lib which want to access this services.

make sure your produce portlet is deployed other wise you might rcv beanlocator is null kind of exception

look into below wiki for more info
http://www.liferay.com/community/wiki/-/wiki/Main/Using+Class+Loader+Proxy+classes+to+share+plugins+services



This is also wrong. Add the producer-portlet as a required deployment context for the subscriber portlet. The service jar will automatically be included. Do not manually remove and/or copy jars around on your own.
Priyanka Dhingra
RE: retrieving data from mysql
March 28, 2012 3:38 AM
Answer

Priyanka Dhingra

Rank: Liferay Master

Posts: 501

Join Date: December 19, 2011

Recent Posts

hi RathnaDevi Chellaiah,

You can create 2 portlets in one liferay portlet project. and as per your requirement use Inter Portlet Communication(IPC). You'll be able to achieve what you wanna do

Regards
Priyanka
RathnaDevi Chellaiah
RE: retrieving data from mysql
March 28, 2012 10:14 PM
Answer

RathnaDevi Chellaiah

Rank: Junior Member

Posts: 29

Join Date: March 26, 2012

Recent Posts

hi,
Thanks a lot.. it worked well. i have another problem.. i have created and deployed a registration form say employee details in a portlet where i used service builder to store in lportal db. it stored successfully. my problem is- based on the registration form i need to create a login for the employee who have registered. this login should be integrated with liferay user login as shown in the attached pic and it should create a user account page for the particular user in liferay...? how to implement this...??????
Attachment

Attachments: snap.png (41.6k)
Jitendra Rajput
RE: retrieving data from mysql
March 29, 2012 12:14 AM
Answer

Jitendra Rajput

Rank: Liferay Master

Posts: 824

Join Date: January 7, 2011

Recent Posts

You should create different thread for different question.

and regarding your question .
When even you are saving employee details at the same time if you have complete user information then you can save inside user_ table.
Using UserLocalServiceUtil.addUser() you can add new user in Liferay database.

Once user is added they can access your system.
RathnaDevi Chellaiah
RE: retrieving data from mysql
March 29, 2012 4:03 AM
Answer

RathnaDevi Chellaiah

Rank: Junior Member

Posts: 29

Join Date: March 26, 2012

Recent Posts

k. i did as u said still user not created in liferay.. can u send me a sample appli based on it..?
RathnaDevi Chellaiah
RE: retrieving data from mysql
March 29, 2012 5:38 AM
Answer

RathnaDevi Chellaiah

Rank: Junior Member

Posts: 29

Join Date: March 26, 2012

Recent Posts

here is the code...
Attachments: Springport-portlet.zip (129.2k)
RathnaDevi Chellaiah
RE: retrieving data from mysql
March 30, 2012 6:53 AM
Answer

RathnaDevi Chellaiah

Rank: Junior Member

Posts: 29

Join Date: March 26, 2012

Recent Posts

hi priyanka
i need ur help....am facing an issue regarding the creation of user using user form data.. my scenario is i have to create a registration form that is used by service builder and based on that it stores the value into db(lportel).. at the same time the value in the form should create a user into the liferay user table.
i have achieved upto storing of db.. but facing issue with creating user.. the form data is not fetched and stored into the predefined attributes of userlocalservice..


public void processAction(ActionRequest request,ActionResponse response) {

try {
long userId=CounterLocalServiceUtil.increment();
User user=UserLocalServiceUtil.createUser(userId);
ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
long creatorId=0;
long companyId = themeDisplay.getCompanyId();
user.setCompanyId(companyId);
boolean autoPassword = true;

String password1 = "";
user.setPassword(password1);
String password2 = "";
user.setPassword(password2);
boolean autoScreenName = true;
String screenName = "MyProfile1";
user.setScreenName(screenName);
String compemail = request.getParameter("cemail");// from the view.jsp page

user.setEmailAddress(compemail);
long facebookId = 0;
user.setFacebookId(facebookId);
String openId = "";
user.setOpenId(openId);
// Locale locale = themeDisplay.getLocale();
String firstName = request.getParameter("fname");// from the view.jsp page
user.setActive(true);
user.setFirstName(firstName);

String middleName = "";
String lastName = request.getParameter("lname");// from the view.jsp page
user.setLastName(lastName);
int prefixId = 0;
int suffixId = 0;
boolean male = true;
int birthdayMonth = 0;
int birthdayDay = 0;
int birthdayYear = 0;
String designation =request.getParameter("designation");// from the view.jsp page
user.setJobTitle(designation);
long[] groupIds = null;
long[] organizationIds = null;
long[] roleIds = null;
long[] userGroupIds = null;

boolean sendEmail = true;

com.liferay.portal.service.ServiceContext serviceContext = ServiceContextFactory.getInstance(User.class.getName(), request);

UserLocalServiceUtil.addUser(creatorId,
themeDisplay.getCompanyId(), autoPassword, password1, password2,
autoScreenName, screenName, compemail, facebookId, openId,
LocaleUtil.getDefault(), firstName, middleName, lastName, prefixId,
suffixId, male, birthdayMonth, birthdayDay, birthdayYear, designation,
groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
serviceContext);

}
catch(Exception e){
e.printStackTrace();
}
}
i have tried many code posted in forum but nothing worked.......... please give me guide on this issue..
David H Nebinger
RE: retrieving data from mysql
March 30, 2012 7:05 AM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 7887

Join Date: September 1, 2006

Recent Posts

RathnaDevi Chellaiah:
i have tried many code posted in forum but nothing worked.......... please give me guide on this issue..


Many of the solutions in the forum, while incomplete, will work. Do you have any exceptions spit out from the try/catch block? How do you define "nothing worked"? User is not in the users list (did you try reindexing), or what?
RathnaDevi Chellaiah
RE: retrieving data from mysql
March 30, 2012 7:30 AM
Answer

RathnaDevi Chellaiah

Rank: Junior Member

Posts: 29

Join Date: March 26, 2012

Recent Posts

hi
ya i tried using reindexing too.. my exception is null pointer exception.. it doesn't take value from view.jsp ...
David H Nebinger
RE: retrieving data from mysql
March 30, 2012 7:43 AM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 7887

Join Date: September 1, 2006

Recent Posts

Then that is a completely different issue than the "nothing worked" thing that you said before...
RathnaDevi Chellaiah
RE: retrieving data from mysql
April 1, 2012 9:54 PM
Answer

RathnaDevi Chellaiah

Rank: Junior Member

Posts: 29

Join Date: March 26, 2012

Recent Posts

k is this the right way to take data from html form and put in to the parameter of addUser??????? if yes,why doesn't it takes the value from form data and assign...?????
String compemail = request.getParameter("cemail");
Jitendra Rajput
RE: retrieving data from mysql
April 1, 2012 10:33 PM
Answer

Jitendra Rajput

Rank: Liferay Master

Posts: 824

Join Date: January 7, 2011

Recent Posts

Any error in log ?
Hitoshi Ozawa
RE: retrieving data from mysql
April 1, 2012 11:14 PM
Answer

Hitoshi Ozawa

Rank: Liferay Legend

Posts: 7952

Join Date: March 23, 2010

Recent Posts

ya i tried using reindexing too.. my exception is null pointer exception.. it doesn't take value from view.jsp ...


He's saying he's getting null exception. I think it's because he've set organization to null and I thought that was a required field. Unfortunately, Liferay API documentation doesn't specify which fields are required and this problem has come up often. I think every developer has encountered it once.
Ravi Kumar Gupta
RE: retrieving data from mysql
April 9, 2012 4:03 AM
Answer

Ravi Kumar Gupta

Rank: Liferay Legend

Posts: 1238

Join Date: June 23, 2009

Recent Posts

It should not happen.. you should be getting values properly.. just check if u r using right names for parameters...

Also, if you can attach the latest portlet code.. that would help us understanding more..
-Ravi
Priyanka Dhingra
RE: retrieving data from mysql
April 10, 2012 1:11 AM
Answer

Priyanka Dhingra

Rank: Liferay Master

Posts: 501

Join Date: December 19, 2011

Recent Posts

RathnaDevi Chellaiah:
k is this the right way to take data from html form and put in to the parameter of addUser??????? if yes,why doesn't it takes the value from form data and assign...?????
String compemail = request.getParameter("cemail");



It does work....this is to be done in your processAction() method...
RathnaDevi Chellaiah
RE: retrieving data from mysql
April 11, 2012 1:16 AM
Answer

RathnaDevi Chellaiah

Rank: Junior Member

Posts: 29

Join Date: March 26, 2012

Recent Posts

hi,
It worked.. thank u for ur reply. Now am able to create user in the user_ table of liferay but mail is not shot to the created user? what to do?
Hitoshi Ozawa
RE: retrieving data from mysql
March 30, 2012 8:11 AM
Answer

Hitoshi Ozawa

Rank: Liferay Legend

Posts: 7952

Join Date: March 23, 2010

Recent Posts

long[] organizationIds = null;


Have you tried specifying an organization.
RathnaDevi Chellaiah
RE: retrieving data from mysql
April 2, 2012 2:14 AM
Answer

RathnaDevi Chellaiah

Rank: Junior Member

Posts: 29

Join Date: March 26, 2012

Recent Posts

ya u r right.. i have kept long[] organizationIds = null;
what should i replace instead of null.. please tell me..
MICHAIL MOUDATSOS
RE: retrieving data from mysql
April 2, 2012 4:56 AM
Answer

MICHAIL MOUDATSOS

Rank: Regular Member

Posts: 110

Join Date: October 4, 2011

Recent Posts

1long[] organizationIds = new long[]{};
RathnaDevi Chellaiah
RE: retrieving data from mysql
April 2, 2012 6:09 AM
Answer

RathnaDevi Chellaiah

Rank: Junior Member

Posts: 29

Join Date: March 26, 2012

Recent Posts

thank u MICHAIL, even though its not working...
Jitendra Rajput
RE: retrieving data from mysql
April 1, 2012 11:57 PM
Answer

Jitendra Rajput

Rank: Liferay Master

Posts: 824

Join Date: January 7, 2011

Recent Posts

it doesn't take value from view.jsp



You mean to say you are not able to retrieve form values from request ?.

If so then check enctype attribute of form .. If you are using multipart/form-data in enctype then try to retrieve form values from UploadRequest.
Joel Peterson
RE: retrieving data from mysql
April 4, 2012 3:45 PM
Answer

Joel Peterson

Rank: New Member

Posts: 22

Join Date: February 18, 2010

Recent Posts

RathnaDevi Chellaiah:
hi
ya i tried using reindexing too.. my exception is null pointer exception.. it doesn't take value from view.jsp ...


In your view.jsp, are you prefacing your form inputs' name attributes with the portlet namespace? If so, then you should probably use Liferay's ParamUtil method to retrieve the form values in your processAction method.

1<input name="<portlet:namespace/>myInputName" type="text" />

1String myInputValue = com.liferay.portal.kernel.util.ParamUtil.getString(request,"myInputName","my default value");
RathnaDevi Chellaiah
RE: retrieving data from mysql
April 4, 2012 10:55 PM
Answer

RathnaDevi Chellaiah

Rank: Junior Member

Posts: 29

Join Date: March 26, 2012

Recent Posts

No. see the following view.jsp of mine

<html>
<head>
</head>
<body>
<form action='<portlet:actionURL/>' method="post">
<table cellpadding="20" cellspacing="20"> <tr>
<td><label>First Name</label><sup>*</sup></td>
<td> <input type="text" name="fname"></td> </tr>
<tr>
<td><label>Last Name</label><sup>*</sup></td>
<td><input type="text" name="lname"></td>
</tr>
<tr><td><label>Company Email-ID</label><sup>*</sup></td>
<td><input type="text" name="cemailid"></td></tr>
<tr><td><label>Password</label><sup>*</sup></td>
<td><input type="text" name="password1"></td></tr>
<tr><td><label>Confirm Password </label><sup>*</sup></td>
<td><input type="text" name="password2"></td></tr>
<tr><td><input type="submit" value="Submit"></td>
<td><input type="Reset"></td></tr>
</table>
</form>
</body>
</html>
Kalpesh Sangani
RE: retrieving data from mysql
April 9, 2012 3:31 AM
Answer

Kalpesh Sangani

Rank: New Member

Posts: 4

Join Date: November 21, 2011

Recent Posts

Through DynamicQuery you can also retrieve data if you have some special condition as below

DynamicQuery query = DynamicQueryFactoryUtil.forClass(ContactUs.class, PortletClassLoaderUtil.getClassLoader())
.add(PropertyFactoryUtil.forName("contactId").eq(new Long("3")))
.addOrder(OrderFactoryUtil.asc("contactId"));

List <ContactUs>results = ContactUsLocalServiceUtil.dynamicQuery(query, -1, -1);