Tuesday, July 06, 2010

Integration of Portal with SQL

TestPortalWithSQL : This example show CRUD Operation using Portal on My-SQL Database. Here in example we had used is JBOSS Portal along with JBOSS Application Server. Portal frame work is more verbose and has very good look and feel aspect for the project. It addition to Look and feel it also helps the user to maintain his/her portal via screen i.e. through GUI rather then hard coding.

TestPortalWithSQL:

package testPortalWithSQL;

import java.io.IOException;

import java.io.PrintWriter;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.Statement;

import javax.portlet.ActionRequest;

import javax.portlet.ActionResponse;

import javax.portlet.GenericPortlet;

import javax.portlet.PortletException;

import javax.portlet.PortletMode;

import javax.portlet.PortletPreferences;

import javax.portlet.PortletSecurityException;

import javax.portlet.PortletURL;

import javax.portlet.RenderRequest;

import javax.portlet.RenderResponse;

import javax.portlet.UnavailableException;

import javax.portlet.WindowState;

public class TestPortalWithSQL extends GenericPortlet {

String dbUrl = "jdbc:mysql://localhost/siddhutest_db";

String dbClass = "com.mysql.jdbc.Driver";

/* (non-Javadoc)

* @see javax.portlet.GenericPortlet#doView(javax.portlet.RenderRequest, javax.portlet.RenderResponse)

*/

@Override

protected void doView(RenderRequest request, RenderResponse response)

throws PortletException, IOException, UnavailableException {

/*response.setContentType("text/html");

PrintWriter writer = response.getWriter();

writer.write("Hello World!");

writer.close();*/

PortletPreferences pref = request.getPreferences();

String id = pref.getValue("id","");

String name = pref.getValue("name","");

response.setContentType(request.getResponseContentType());

PrintWriter writer = response.getWriter();

writer.write(" value id :" + id +" and name:" + name);

}

/* (non-Javadoc)

* @see javax.portlet.GenericPortlet#doEdit(javax.portlet.RenderRequest, javax.portlet.RenderResponse)

*/

@Override

protected void doEdit(RenderRequest request, RenderResponse response)

throws PortletException, PortletSecurityException, IOException {

// TODO Auto-generated method stub

//super.doEdit(request, response);

PortletURL actionURL = response.createActionURL();

response.setContentType(request.getResponseContentType());

PrintWriter writer = response.getWriter();

//Code to show text field for ID and NAME and three button Insert, Update and Delete.

}

/* (non-Javadoc)

* @see javax.portlet.GenericPortlet#doHelp(javax.portlet.RenderRequest, javax.portlet.RenderResponse)

*/

@Override

protected void doHelp(RenderRequest request, RenderResponse response)

throws PortletException, PortletSecurityException, IOException {

// TODO Auto-generated method stub

//super.doHelp(request, response);

response.setContentType(request.getResponseContentType());

try {

response.setContentType(request.getResponseContentType());

PrintWriter writer = response.getWriter();

writer.write("Pref Portlet Help

");

} catch (IOException e) {

throw new PortletException("PrefPortlet.doHelp exception", e);

}

}

/* (non-Javadoc)

* @see javax.portlet.Portlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)

*/

@Override

public void processAction(ActionRequest request, ActionResponse response)

throws PortletException, PortletSecurityException, IOException {

// TODO Auto-generated method stub

//super.processAction(request, response);

String id = request.getParameter("ID");

String name = request.getParameter("NAME");

//String buttonPressed;

if(request.getParameter("Insert") !=null && request.getParameter("Insert").equals("Insert1") && !request.getParameter("Insert").equals(""))

{

InsertbuttonPress(request,response);

}

if(request.getParameter("Update") !=null && request.getParameter("Update").equals("Update1")&& !request.getParameter("Update").equals(""))

{

UpdatebuttonPress(request,response);

}

if(request.getParameter("Delete") !=null && request.getParameter("Delete").equals("Delete1")&& !request.getParameter("Delete").equals(""))

{

DeletebuttonPress(request,response);

}

//String InsertbuttonPress = request.getParameter("Insert");

//String DeletebuttonPress = request.getParameter("Delete");

try {

PortletPreferences pref = request.getPreferences();

pref.setValue("id", id);

pref.setValue("name", name);

pref.store();

} catch (Exception e) {

throw new PortletException(e.getMessage());

}

response.setPortletMode(PortletMode.VIEW);

response.setWindowState(WindowState.NORMAL);

}

public void InsertbuttonPress(ActionRequest request, ActionResponse response)

{

String id = request.getParameter("ID");

String name = request.getParameter("NAME");

try {

Class.forName(dbClass).newInstance();

Connection con = DriverManager.getConnection(dbUrl,"root","");

Statement stmt = con.createStatement();

String query = "insert into test_table (id, name) values ("+id+",'"+name+"')";

int i = stmt.executeUpdate(query);

con.close();

//return "success";

} //end try

catch(Exception e) {

e.printStackTrace();

}

}

public void UpdatebuttonPress(ActionRequest request, ActionResponse response)

{

String id = request.getParameter("ID");

String name = request.getParameter("NAME");

try {

Class.forName(dbClass).newInstance();

Connection con = DriverManager.getConnection(dbUrl,"root","");

Statement stmt = con.createStatement();

String query = "update test_table set name = '"+name+"' where id ="+id;

int i = stmt.executeUpdate(query);

con.close();

//return "success";

} //end try

catch(Exception e) {

e.printStackTrace();

}

}

public void DeletebuttonPress(ActionRequest request, ActionResponse response)

{

String id = request.getParameter("ID");

String name = request.getParameter("NAME");

try {

Class.forName(dbClass).newInstance();

Connection con = DriverManager.getConnection(dbUrl,"root","");

Statement stmt = con.createStatement();

String query = "delete from test_table where id ="+id;

int i = stmt.executeUpdate(query);

con.close();

//return "success";

} //end try

catch(Exception e) {

e.printStackTrace();

}

}

}

default-object.xml:

xml version="1.0" encoding="UTF-8"?>

DOCTYPE deployments PUBLIC

"-//JBoss Portal//DTD Portal Object 2.6//EN"

"http://www.jboss.org/portal/dtd/portal-object_2_6.dtd">

<deployments>

<deployment>

<parent-ref>default.defaultparent-ref>

<if-exists>overwriteif-exists>

<window>

<window-name>TestPortalWithSQLWindowwindow-name>

<instance-ref>TestPortalWithSQLInstanceinstance-ref>

<region>centerregion>

<height>1height>

<initial-window-state>maximizedinitial-window-state>

window>

deployment>

deployments>

portlet.xml:

xml version="1.0" encoding="UTF-8"?>

<portlet-app

xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"

version="2.0">

<portlet>

<portlet-name>TestPortalWithSQLportlet-name>

<display-name>TestPortalWithSQLdisplay-name>

<portlet-class>

testPortalWithSQL.TestPortalWithSQL

portlet-class>

<supports>

<mime-type>text/htmlmime-type>

<portlet-mode>VIEWportlet-mode>

<portlet-mode>EDITportlet-mode>

<portlet-mode>HELPportlet-mode>

supports>

<portlet-info>

<title>TestPortalWithSQLtitle>

portlet-info>

portlet>

portlet-app>

portlet-instances.xml:

xml version="1.0" encoding="UTF-8"?>

DOCTYPE deployments PUBLIC

"-//JBoss Portal//DTD Portlet Instances 2.6//EN"

"http://www.jboss.org/portal/dtd/portlet-instances_2_6.dtd">

<deployments>

<deployment>

<instance>

<instance-id>TestPortalWithSQLInstanceinstance-id>

<portlet-ref>TestPortalWithSQLportlet-ref>

instance>

deployment>

deployments>

web.xml:

xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

<display-name>TestPortalWithSQLdisplay-name>

<welcome-file-list>

<welcome-file>index.htmlwelcome-file>

<welcome-file>index.htmwelcome-file>

<welcome-file>index.jspwelcome-file>

<welcome-file>default.htmlwelcome-file>

<welcome-file>default.htmwelcome-file>

<welcome-file>default.jspwelcome-file>

welcome-file-list>

web-app>
Google Assist: Portal with SQL, Simple Portal Example with MySQL, Portal with CRUD Operation,Portal-MySQL,Portal with SQL.

No comments: