How to Make Database connection in Liferay-5.2.3
In this example we are going to see the database connectivity with MySQL and a simple example of it. Here the Prerequisite for doing connection with database.
• Install the JDK and set the Path JAVA_HOME variable .
• Set the Path for Ant ANT_HOME variable .
• Install MySQL (remember the port number, username and pasward).
• Make a Folder For your Workspace where the Liferay folders you are going to keep like EXT and Tomcat etc.
• You must know the how to make simple struts Portlet with action file.
Make sure that above things have been done accordingly and check your Liferay is working properly.Now follow the following steps to do a connection with database.
Step 1 : Set the following code in portal-ext.properties under path(~\liferay-portal-ext-5.2.3\ext-impl\src\ portal-ext.properties) in your Liferay folder.
This step you may have done at time of liferay setup if you have then check it out.
Code :
jdbc.default.driverClassName=com.mysql.jdbc.Driver
jdbc.default.url=jdbc:mysql://localhost:3309/test?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.default.username=root
jdbc.default.password=root
Make sure that the port number you have given is correct in default url and the username, password, and database/schema name is correct.
Step 2 : Now ant deploy from ext-impl and from ext, make sure that it’s done successfully now restart the tomcat and check in MySQL database name as you given in this example we used the name test.
Step 3 : Make the table with your need for this example we are taking the table for testing called book the following statement is for creating the table.
create table Book (
bookId bigint(10) not null primary key,
title VARCHAR(75) null,
dateAdded datetime
);
Write the above statement in MySQL screen than perform the transaction, refresh the Database and check the table.
Step 4 : Make service.xml on this /ext/ext-impl/src/com/ext/portlet/library path here the Library is the folder of your portlet where this file you have to place. Write the following is the service.xml
Library
Step 5 : Now do the following steps to build the service for above table.
• Go to "/ext/ext-impl"
• Execute "ant build-service -Dservice.file=src/com/ext/portlet/library/service.xml"
• Make sure the build is successful
• Confirm all the service layer files are generated as per the Spring/Hibernate framework. check under
o ext/ext-impl/src/com/ext/portlet/library
o ext/ext-service/src/com/ext/portlet/library
Step 6 : Updating service layer class – 1
file location : ext\ext-
impl\src\com\ext\portlet\library\service\impl\BookLocalServiceImpl.java
now write the functionality for database insert. insert this method.
import com.ext.portlet.library.model.Book;
import com.ext.portlet.library.model.impl.BookImpl;
import java.rmi.RemoteException;
import com.liferay.portal.PortalException;
import com.liferay.portal.SystemException;
import com.liferay.counter.service.CounterServiceUtil;
public Book create(String title)
throws PortalException, SystemException, RemoteException {
// create a primary key
long bookId = CounterServiceUtil.increment(Book.class.getName());
// instantiate an empty object
Book book = new BookImpl();
book.setBookId(bookId);
book.setTitle(title);
// persist the book object
book = bookPersistence.update(book, false);
// return the object to caller
return book;
}
Step 7 : Run ant build-service again to re-generate the corresponding Util interfaces. Do the following steps.
• execute "ant build-service -Dservice.file=src/com/ext/portlet/library/service.xml"
• execute "ant compile" to compile all the generated file and make sure the build is SUCCESSFUL.
Step 8 : Now Update the action file which is made at location "ext/ext-impl/src/com/ext/portlet/library/action". The following is the code to written in Action file name as AddBookAction.java
package com.ext.portlet.library.action;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.liferay.portal.struts.PortletAction;
import com.liferay.portal.kernel.util.Validator;
import com.ext.portlet.library.service.BookLocalServiceUtil;
public class AddBookAction extends PortletAction {
public void processAction(
ActionMapping mapping, ActionForm form, PortletConfig config,
ActionRequest req, ActionResponse res)
throws Exception {
String bookTitle = req.getParameter("book_title");
if (Validator.isNull(bookTitle)) {
setForward(req, "portlet.ext.library.failure");
} else {
// new line to be inserted
BookLocalServiceUtil.create(bookTitle);
setForward(req, "portlet.ext.library.success");
}
}
public ActionForward render(ActionMapping mapping, ActionForm form,
PortletConfig config, RenderRequest req, RenderResponse res)
throws Exception {
if (getForward(req) != null && !getForward(req).equals("")) {
return mapping.findForward(getForward(req));
} else {
return mapping.findForward("portlet.ext.library.view");
}
}
}
Step 9 : Now do run "ant deploy" from "ext" for this do the following steps.
• go back to /ext folder and execute "ant deploy"
• make sure the build is SUCCESSFUL.
• check the newly generated jar files "ext-impl.jar" and "ext-service.jar"
Step 10 : Start tomcat and check the portlet. try to add some books and make sure the values are getting saved in the database.
Note : the structs portlet steps was not shown in this section so do that steps by ur self before doing this steps.
_________________________________________________________________________________ Laliwala IT Services
Mangal Girdhar Compund, Nr. B.G.Tower, Dehli Darwaja,
Ahmedabad - 380004
Gujarat, India
Email : imran@laliwalait.com
Telephone: +91-9904245322
In this example we are going to see the database connectivity with MySQL and a simple example of it. Here the Prerequisite for doing connection with database.
• Install the JDK and set the Path JAVA_HOME variable .
• Set the Path for Ant ANT_HOME variable .
• Install MySQL (remember the port number, username and pasward).
• Make a Folder For your Workspace where the Liferay folders you are going to keep like EXT and Tomcat etc.
• You must know the how to make simple struts Portlet with action file.
Make sure that above things have been done accordingly and check your Liferay is working properly.Now follow the following steps to do a connection with database.
Step 1 : Set the following code in portal-ext.properties under path(~\liferay-portal-ext-5.2.3\ext-impl\src\ portal-ext.properties) in your Liferay folder.
This step you may have done at time of liferay setup if you have then check it out.
Code :
jdbc.default.driverClassName=com.mysql.jdbc.Driver
jdbc.default.url=jdbc:mysql://localhost:3309/test?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.default.username=root
jdbc.default.password=root
Make sure that the port number you have given is correct in default url and the username, password, and database/schema name is correct.
Step 2 : Now ant deploy from ext-impl and from ext, make sure that it’s done successfully now restart the tomcat and check in MySQL database name as you given in this example we used the name test.
Step 3 : Make the table with your need for this example we are taking the table for testing called book the following statement is for creating the table.
create table Book (
bookId bigint(10) not null primary key,
title VARCHAR(75) null,
dateAdded datetime
);
Write the above statement in MySQL screen than perform the transaction, refresh the Database and check the table.
Step 4 : Make service.xml on this /ext/ext-impl/src/com/ext/portlet/library path here the Library is the folder of your portlet where this file you have to place. Write the following is the service.xml
Step 5 : Now do the following steps to build the service for above table.
• Go to "/ext/ext-impl"
• Execute "ant build-service -Dservice.file=src/com/ext/portlet/library/service.xml"
• Make sure the build is successful
• Confirm all the service layer files are generated as per the Spring/Hibernate framework. check under
o ext/ext-impl/src/com/ext/portlet/library
o ext/ext-service/src/com/ext/portlet/library
Step 6 : Updating service layer class – 1
file location : ext\ext-
impl\src\com\ext\portlet\library\service\impl\BookLocalServiceImpl.java
now write the functionality for database insert. insert this method.
import com.ext.portlet.library.model.Book;
import com.ext.portlet.library.model.impl.BookImpl;
import java.rmi.RemoteException;
import com.liferay.portal.PortalException;
import com.liferay.portal.SystemException;
import com.liferay.counter.service.CounterServiceUtil;
public Book create(String title)
throws PortalException, SystemException, RemoteException {
// create a primary key
long bookId = CounterServiceUtil.increment(Book.class.getName());
// instantiate an empty object
Book book = new BookImpl();
book.setBookId(bookId);
book.setTitle(title);
// persist the book object
book = bookPersistence.update(book, false);
// return the object to caller
return book;
}
Step 7 : Run ant build-service again to re-generate the corresponding Util interfaces. Do the following steps.
• execute "ant build-service -Dservice.file=src/com/ext/portlet/library/service.xml"
• execute "ant compile" to compile all the generated file and make sure the build is SUCCESSFUL.
Step 8 : Now Update the action file which is made at location "ext/ext-impl/src/com/ext/portlet/library/action". The following is the code to written in Action file name as AddBookAction.java
package com.ext.portlet.library.action;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.liferay.portal.struts.PortletAction;
import com.liferay.portal.kernel.util.Validator;
import com.ext.portlet.library.service.BookLocalServiceUtil;
public class AddBookAction extends PortletAction {
public void processAction(
ActionMapping mapping, ActionForm form, PortletConfig config,
ActionRequest req, ActionResponse res)
throws Exception {
String bookTitle = req.getParameter("book_title");
if (Validator.isNull(bookTitle)) {
setForward(req, "portlet.ext.library.failure");
} else {
// new line to be inserted
BookLocalServiceUtil.create(bookTitle);
setForward(req, "portlet.ext.library.success");
}
}
public ActionForward render(ActionMapping mapping, ActionForm form,
PortletConfig config, RenderRequest req, RenderResponse res)
throws Exception {
if (getForward(req) != null && !getForward(req).equals("")) {
return mapping.findForward(getForward(req));
} else {
return mapping.findForward("portlet.ext.library.view");
}
}
}
Step 9 : Now do run "ant deploy" from "ext" for this do the following steps.
• go back to /ext folder and execute "ant deploy"
• make sure the build is SUCCESSFUL.
• check the newly generated jar files "ext-impl.jar" and "ext-service.jar"
Step 10 : Start tomcat and check the portlet. try to add some books and make sure the values are getting saved in the database.
Note : the structs portlet steps was not shown in this section so do that steps by ur self before doing this steps.
_________________________________________________________________________________
Mangal Girdhar Compund, Nr. B.G.Tower, Dehli Darwaja,
Ahmedabad - 380004
Gujarat, India
Email : imran@laliwalait.com
Telephone: +91-9904245322
I am not able to see any blogs from feb 2011 tag
ReplyDeletecapture your screen please.
ReplyDeleteDo you have a spam issue on this blog; I also am
ReplyDeletea blogger, and I was wondering your situation; we
have created some nice practices and we are looking to exchange solutions with others, please shoot
me an e-mail if interested.
Look at my blog ... diet that works
This design is wicked! You definitely know
ReplyDeletehow to keep a reader amused. Between your wit and your videos, I was almost moved to start my own blog (well, almost.
..HaHa!) Wonderful job. I really loved what you had to say, and more than that, how
you presented it. Too cool!
Review my webpage :: auto repair in imperial beach
It's amazing in favor of me to have a website, which is useful for my experience. thanks admin
ReplyDeleteFeel free to visit my web site: toilet repair in farmington hills
When I initially left a comment I seem to have clicked on the -Notify me when new comments are added- checkbox and from now on every time a comment is added I recieve
ReplyDelete4 emails with the same comment. Perhaps there is a way you
are able to remove me from that service? Kudos!
Feel free to visit my weblog: re-roof in encinitas
Hi there Dear, are you in fact visiting this site daily, if
ReplyDeleteso afterward you will absolutely get pleasant knowledge.
My page dmv in mission valley
Hello There. I discovered your blog using msn. That is a really neatly written article.
ReplyDeleteI'll be sure to bookmark it and come back to read more of your helpful info. Thanks for the post. I will certainly comeback.
my website ... bathroom remodeling in des moines
What a data of un-ambiguity and preserveness of
ReplyDeleteprecious knowledge on the topic of unexpected emotions.
Feel free to visit my blog tax preparation in bothell
Excellent pieces. Keep posting such kind of info on your blog.
ReplyDeleteIm really impressed by your blog.
Hey there, You have performed a fantastic job. I'll definitely digg it and in my opinion suggest to my friends. I'm sure they'll be benefited from this web site.
My weblog ... pool repair in palm beach
Greetings! Very helpful advice within this post! It's the little changes which will make the greatest changes. Thanks for sharing!
ReplyDeletemy homepage; plumber in murrieta
My web page :: plumbing contractor in fallbrook
Hi every one, here every person is sharing these kinds of knowledge,
ReplyDeleteso it's nice to read this weblog, and I used to visit this webpage all the time.
Take a look at my site ... certified signing agent in riverside
Hi there mates, how is the whole thing, and what you want to say concerning this post, in my
ReplyDeleteview its really awesome designed for me.
Also visit my weblog - auto window tinting in aurora
Also see my webpage: residential window tinting in arvada
I am really enjoying the theme/design of your weblog.
ReplyDeleteDo you ever run into any browser compatibility problems?
A small number of my blog visitors have complained about
my site not working correctly in Explorer but looks great in Chrome.
Do you have any ideas to help fix this problem?
Here is my web site roadside assistance in claude
Pretty nice post. I just stumbled upon your weblog and wished to
ReplyDeletemention that I've truly enjoyed browsing your weblog posts. In any case I'll be subscribing on your
rss feed and I am hoping you write once more very soon!
Feel free to surf to my weblog ... carpet installation in philadelphia
I love what you guys are usually up too.
ReplyDeleteThis kind of clever work and coverage! Keep up the superb works guys
I've you guys to blogroll.
My web page: garage remodeling in sumner
Thanks for sharing your info. I truly appreciate your efforts and I
ReplyDeletewill be waiting for your further write ups thanks once again.
Also visit my web blog - smart forex Signals
Hі thеre cοlleagues, hоw iѕ all, and whаt уou dеsire to say аbout thiѕ pοst,
ReplyDeletein my vіeω its tгulу аmаzing designеd for me.
Нeге іѕ mу wеb-site:
raspberry ketone uk
It's really very difficult in this busy life to listen news on Television, so I just use internet for that purpose, and get the most up-to-date information.
ReplyDeleteAlso visit my blog post ... vitamin shoppe coupons
Link exchange iѕ nothing else еxсeрt it is simрly plaсing thе οther
ReplyDeletepеrѕon's weblog link on your page at appropriate place and other person will also do similar in support of you.
My page - vistaprint coupon code
Thanks for sharing such a wonderful piece of content. It was very informative. PHP Services
ReplyDeleteEcommerce Service Provider
Magento Service Providers
Data Extraction Services
Data Extraction Services Company
Digital payment gateway
düzce
ReplyDeletesakarya
tunceli
van
bayburt
ZWXB
görüntülüshow
ReplyDeleteücretli show
M7DTN
https://titandijital.com.tr/
ReplyDeletemalatya parça eşya taşıma
bilecik parça eşya taşıma
antalya parça eşya taşıma
hakkari parça eşya taşıma
YPO
ankara parça eşya taşıma
ReplyDeletetakipçi satın al
antalya rent a car
antalya rent a car
ankara parça eşya taşıma
HVJ41S
istanbul evden eve nakliyat
ReplyDeletezonguldak evden eve nakliyat
adıyaman evden eve nakliyat
bilecik evden eve nakliyat
ankara evden eve nakliyat
CG35XV
ordu evden eve nakliyat
ReplyDeletebursa evden eve nakliyat
konya evden eve nakliyat
osmaniye evden eve nakliyat
bitlis evden eve nakliyat
AH6UU
19156
ReplyDeleteMuğla Evden Eve Nakliyat
Çankırı Evden Eve Nakliyat
sarms
Kalıcı Makyaj
Kars Evden Eve Nakliyat
Rize Evden Eve Nakliyat
buy steroids
Ardahan Evden Eve Nakliyat
oxandrolone anavar
9EF5C
ReplyDeleteMalatya Parça Eşya Taşıma
Bibox Güvenilir mi
Tekirdağ Fayans Ustası
Hatay Parça Eşya Taşıma
Çerkezköy Çamaşır Makinesi Tamircisi
Kars Lojistik
Eskişehir Lojistik
Ünye Çekici
Yalova Evden Eve Nakliyat
AB6C4
ReplyDeletegümüşhane en iyi ücretsiz sohbet uygulamaları
aksaray mobil sohbet et
samsun canlı sohbet bedava
uşak yabancı sohbet
niğde görüntülü sohbet uygulama
uşak tamamen ücretsiz sohbet siteleri
parasız sohbet
adıyaman mobil sohbet odaları
düzce mobil sohbet et
44A66
ReplyDeletekilis mobil sesli sohbet
bingöl canlı sohbet et
igdir random görüntülü sohbet
hatay telefonda sohbet
tokat ücretsiz görüntülü sohbet
antalya rastgele görüntülü sohbet uygulaması
edirne sohbet siteleri
izmir sesli sohbet
ardahan goruntulu sohbet
CC803
ReplyDeleteTwitter Beğeni Satın Al
Kripto Para Kazanma Siteleri
Binance Sahibi Kim
Btcst Coin Hangi Borsada
Bitcoin Nasıl Üretilir
Likee App Beğeni Satın Al
Soundcloud Dinlenme Hilesi
Bonk Coin Hangi Borsada
Coin Nasıl Oynanır
0F94A
ReplyDeleteyearn
uwu lend
metamask
dextools
ellipal
ledger desktop
looksrare
layerzero
arculus
685EF
ReplyDeleteKaraisalı
Erzincan
Akçadağ
İliç
Sarıoğlan
Bozdoğan
Karaman
Şuhut
Yenipazar