Sunteți pe pagina 1din 5

Steps to configure the Liferay with external Database: (This document shows how to connect with Oracle) For

this you need to have Eclipse cofigured with Liferay plugin. Step 1:Set-up the properties file to connect the External database. Create a file named portal-ext.properties under {Eclipse Workspce}bundles\tomcat6.0.26\webapps\ROOT\WEB-INF\classes [Refer Step1 in the document which I have sent you already.] The portal-ext.properties contains the following:
# # Oracle # jdbc.default.driverClassName=oracle.jdbc.driver.OracleDriver jdbc.default.url=jdbc:oracle:thin:@localhost:1521:orcl jdbc.default.username=ebms jdbc.default.password=ebms jdbc.default.jndi.name=jdbc/LiferayPool

Instead of localhost in the url-property you can specify the ip of your machine. Also the user name& password as your won. Then copy the ojdbc14.jar or suitable jar to provide the database driver under the {Eclipse Workspace}\bundles\tomcat-6.0.26\lib\ext directory. Step 2: Developing the project a) Create a new Liferay-plugin project Example: EmployeeDB b) Then create a Liferay portlet . Example: EmployeePorlet.java c) Then create a new Liferay Service ( Right in our project then new->Liferay Service). Choose the Package-path (example:com.test) and give the values for Namespace. d) An Xml file named service.xml created under docroot->WEB-INF directory.

e) Click on the file,go to the xml tab and edit the content as following:

<?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-servicebuilder_6_0_0.dtd"> <service-builder package-path="com.test"> <author>schinnap</author> <namespace>Employee</namespace> <entity name="Employees" local-service="true" remoteservice="true"> <column name="id" type="long" primary="true"></column> <column name="name" type="String"></column> <column name="age" type="int"></column> </entity> </service-builder>

f) Here the entity name refers to the table name which created under the specified Namespace. Also give the values column name ,type. [Note: The table must contain the primary-key.] g) Now is the time to build the service. Click the button Build Services which available on the top of the xml document. h) See that the classes & interfaces which are needed to perform the operations with this table are created automatically. i) Go to the directory in your workspace under {Eclipse Workspace}\bundles\tomcat6.0.26\webapps\EmployeeDB-portlet\WEB-INF\sql and execute the tables.sql file in the SQL plus command window to generate the table manually. Then give commit. [Note: Change the datatypes with respect to Oracle] j) Then go to the view.jsp under docroot->html->employeeportlet->and add the following:

<%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <%@ <%@ <%@ <%@ page import="javax.portlet.*"%> page import="com.test.service.*"%> page import="com.test.model.*"%> taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>

<portlet:defineObjects /> <%PortletPreferences prefs = renderRequest.getPreferences();%> <table> <form method="POST" action="<portlet:actionURL/>"> <tr> <td>Employee ID:</td> <td><Input type="text" name="id"/></td> </tr> <tr> <td>Name:</td> <td><Input type="text" name="name"/></td> </tr> <tr> <td>Age:</td> <td><Input type="text" name="age"/></td> </tr> <tr> <td><Input type="submit"/></td> </tr> </form> </table> <H3> No of Employees in Database : <% out.println(EmployeesLocalServiceUtil.getEmployeesesCount()); %> </H3> This is the <b>EmployeePortlet</b> portlet in View mode.

k) Then move to Employee portlet.java to write coding to add the data to the database.

package com.test; import java.io.IOException; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.PortletException; import com.liferay.util.bridges.mvc.MVCPortlet; import com.test.model.Employees; import com.test.service.EmployeesLocalServiceUtil; /** * Portlet implementation class EmployeePortlet */ public class EmployeePortlet extends MVCPortlet { public void processAction(ActionRequest request,ActionResponse response)throws PortletException,IOException { try{ Long id=Long.parseLong(request.getParameter("id")); String name=request.getParameter("name"); int age=Integer.parseInt(request.getParameter("age")); Employees employee = EmployeesLocalServiceUtil.createEmployees(id); employee.setId(id); employee.setName(name); employee.setAge(age); EmployeesLocalServiceUtil.addEmployees(employee); System.out.print("\n Employee Record Added Successfully"); } catch(Exception e){ e.printStackTrace(); } } }

l) See that here the classes,interfaces & methods are created with respect to our table name. For Example: The table name here Emploees. The interface name is EmployeesLocalServiceUtil.java and method is createEmployees() In case of a table named Customer, the interface named CustomerLocalServiceUtil.java & method is addCustomer().

m) See the interface under EmployeesLocalServiceUtil.java under docroot->WEBINF->service->com->test->service directory to know all the possible operations. n) Now the project is ready to run. o) Run build.xml and see that in the console 2 portlets for EmployeeDB-portlet are available for use then sign-in into the Liferay. p) Click that our portlet is available for use. q) Then enter the details and check for the output.

Reference: http://www.liferay.com/community/wiki/-/wiki/Main/Database+Configuration Thanks, -Suresh Kumar Chinnapillai.

S-ar putea să vă placă și