Friday June 22, 2007
Dynamic Data in jMaki Widgets Using JPA
![]() |
jMaki provides a rich set of data widgets that can be embedded in a web application. For most of the widgets to be useful, they need to be tied a database backend. For example consider a Table widget displaying data about your favorite stock tickers. This blog explains the steps to create such a Web application, deployed on GlassFish V2, that contains a jMaki-wrapped Yahoo Data Table widget pulling data from JavaDB. |
If you are using a jMaki build higher than 0.96 (or dated after jul 30) then some steps in this entry need to be updated and described here. These steps are marked with "SEE THE UPDATED ENTRY".
Web Application' project and
name it as 'jmaki-jpa'.
jMaki Ajax Framework' by clicking on 'Next' button while
creating the project.Standard' layout as shown below:
Finish'.Runtime' tab, expand Databases, connect to the
default database (with the URL 'jdbc:derby://localhost:1527/sample [app
on APP]'). Specify the username 'app' and password 'app'.Execute
Command...' and enter the following query to create the table definition:create table COMPANY (id int,
companyName varchar(255),
price float,
change float,
percentChange float,
lastUpdated varchar(50),
PRIMARY KEY (id))Refresh' to see the
newly created table in the Tables tree. Select the 'COMPANY' table,
right-click and select 'Execute Command...' and enter:insert into COMPANY values (1, 'A Co', 71.72, 0.02, 0.03, 'Jan 1, 2007,
10:00am' );
insert into COMPANY values (2, 'B Inc', 29.01, 0.42, 1.47, 'Feb 1, 2007,
10:00am' );
insert into COMPANY values (3, 'C Group Inc', 83.81, 0.28, 0.34, 'Mar 1,
2007, 10:00am' );
insert into COMPANY values (4, 'D Company', 52.55, 0.01, 0.02, 'Apr 1,
2007, 10:00am' );
jmaki-jpa', right-click
and select 'New' and choose 'Entity Classes From Database...'.jdbc/sample' as 'Data Source'.COMPANY' in 'Available Tables' and click on 'Add' and enter
the values as shown below:
Next'.server' as shown below:
Create Persistence Unit...' to create the persistence unit
and enter the values as shown below:
Create'.and click on 'Finish'.
Configuration Files' and open 'persistence.xml'.Add Class' button and click on 'Cancel'
button. For some reason the entity classes are not loaded during the first
time.Add Class' button and choose 'server.Company'
class and click 'OK'. This will ensure that the generated
entity class is explicitly recognized by the EntityManagerFactory.Web Pages', select 'New' and then 'JSP...'.
Give the name as 'data' as shown:
Finish'.data.jsp' with the following:<%@ page import="java.util.*" %>
<%@ page import="server.Company" %>
<%@ page import="javax.persistence.*" %>
<%
EntityManagerFactory emf = Persistence.createEntityManagerFactory("jmaki-jpaPU");
EntityManager em = emf.createEntityManager();
List<Company> list = em.createQuery("select c from Company
c").getResultList();
out.println("[");
for (int i=0; i<list.size(); i++) {
Company c = list.get(i);
out.println("['" + c.getCompanyname()
+ "'," +
c.getPrice()
+ "," + c.getChange() + "," +
c.getPercentchange() + ",'" + c.getLastupdated() +
"']");
if (i < list.size()-1)
out.println(",");
}
out.println("]");
%>index.jsp', drag-and-drop a 'Yahoo Data Table'
widget from the jMaki Palette in the 'Main Content Area'.<a:widget name="yahoo.dataTable" args="{
columns :[
{title : 'Company', width : 200, locked:false},
{title : 'Price', width : 75, renderer: 'usMoney'},
{title : 'Change', width : 75, renderer: 'change'},
{title : '% Change', width : 75, renderer: 'pctChange'},
{title : 'Last Updated', width : 85, renderer: 'italic'}
]}"
value="[
['A Co',71.72,0.02,0.03,'9/1 12:00am'],
['B Inc',29.01,0.42,1.47,'9/1 12:00am'],
['C Group Inc',83.81,0.28,0.34,'9/1 12:00am'],
['D Company',52.55,0.01,0.02,'9/1 12:00am']
]" /><a:widget name="yahoo.dataTable" args="{
columns :[
{title : 'Company', width : 200, locked:false},
{title : 'Price', width : 75, renderer: 'usMoney'},
{title : 'Change', width : 75, renderer: 'change'},
{title : '% Change', width : 75, renderer: 'pctChange'},
{title : 'Last Updated', width : 85, renderer: 'italic'}
]}"
service="data.jsp" />
data.jsp' instead
of the static data.
UPDATED: SEE THE UPDATED ENTRY - Based upon a user request, a NetBeans project for this sample can be opened via Java WebStart here. Alternatively, you can download the project and view at your own ease. Thanks to Geertjan for the tip!
Technorati: jmaki glassfish jpa netbeans
Posted by Arun Gupta in web2.0 | Comments[33]
|
|
|
|
|
Today's Page Hits: 2002
Total # blog entries: 1002
Posted by Watsh Rajneesh on June 25, 2007 at 05:47 AM PDT #
Posted by Daniel on July 06, 2007 at 04:33 PM PDT #
Posted by Arun on July 06, 2007 at 04:53 PM PDT #
Posted by Daniel on July 06, 2007 at 06:24 PM PDT #
Posted by Arun on July 10, 2007 at 02:39 PM PDT #
Posted by David Van Couvering on July 11, 2007 at 10:25 PM PDT #
Posted by vlk on July 12, 2007 at 05:32 AM PDT #
Posted by Arun on July 16, 2007 at 05:21 PM PDT #
Posted by Muhammad Ali Sabir on July 17, 2007 at 08:58 AM PDT #
Posted by Vlk on July 17, 2007 at 10:08 PM PDT #
Posted by Arun on July 17, 2007 at 10:21 PM PDT #
Posted by yanglilibaobao on July 19, 2007 at 12:33 PM PDT #
Posted by yanglilibaobao on July 19, 2007 at 12:33 PM PDT #
Posted by Arun Gupta on July 31, 2007 at 07:20 AM PDT #
Posted by Ludovic Champenois's Blog on July 31, 2007 at 06:09 PM PDT #
Posted by Arun Gupta's Blog on August 24, 2007 at 06:08 AM PDT #
Posted by Arun Gupta's Blog on August 27, 2007 at 08:50 PM PDT #
Hi, how can I use GROUP BY in this example? select c from Company c group by ..." ; doesn't work - Failed to load data: doAjax error communicating with data.jsp. Server returned status code 500.. best regards Anja
Posted by Anja on October 22, 2007 at 01:32 PM PDT #
Anja, if your query return results correctly then there is nothing specific done in the jMaki table. Can you make sure your query is returning correct results ?
Posted by Arun Gupta on October 22, 2007 at 01:45 PM PDT #
I have solved this problem. I created a view and load the data now from this table.
Thank you.
Posted by Anja on October 24, 2007 at 02:43 PM PDT #
Glad you could solve it. Do you want to post a blog entry and link it from here ? :)
Posted by Arun Gupta on October 24, 2007 at 02:49 PM PDT #
Hi people I still have the problem "An exception occured while creating a query in EntityManager" I use NETBEANS 6. Please help me I going CRAZY!
Posted by Victor on February 26, 2008 at 12:20 PM PST #
Posted by Carol McDonald's Blog on June 06, 2008 at 03:54 PM PDT #
Posted by Carol McDonald's Blog on June 06, 2008 at 08:29 PM PDT #
Posted by Carol McDonald's Blog on July 18, 2008 at 04:04 PM PDT #
HAI
I am shamsheer, MCA working in a company using netbeans JPA, and Jmaki. please give me some information and guidelines for learning Jmaki. also give me some pdf help full for devloping jmaki aplications using databases
Posted by shamsheer muhammed on August 20, 2008 at 02:33 AM PDT #
shamsheer, I posted your request to dev@ajax.dev.java.net. Stay tuned, somebody will post a detailed response.
Posted by Arun Gupta on August 21, 2008 at 04:10 PM PDT #
shamsheer, please post your question to dev@ajax.dev.java.net directly for the benefit of other users as well.
Posted by Arun Gupta on August 22, 2008 at 09:42 AM PDT #
i use mysqljdbc connector and mysql database when i try to run data.jsp
i recieve this message.
description
The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: null
Error Code: 0
root cause
Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: null
Error Code: 0
root cause
java.sql.SQLException: Error in allocating a connection. Cause: null
note The full stack traces of the exception and its root causes are available in the GlassFish/v3 logs.
Posted by abbaspour on October 14, 2008 at 06:57 PM PDT #
abbaspour, I've not tried it with GlassFish v3 so not sure if it is something related to that. Any case, I recommend posting a question to users@ajax.dev.java.net for a wider audience.
Posted by Arun Gupta on October 15, 2008 at 09:42 AM PDT #
hi Arun i will do it but tell me the url for this forum to be able to participate in i looked for it in java.net but i could not find it.
thanks Arun
Posted by abbaspour on October 16, 2008 at 05:31 PM PDT #
abbaspour, jMaki forum is at:
http://forums.java.net/jive/forum.jspa?forumID=96
Posted by Arun Gupta on October 16, 2008 at 05:34 PM PDT #
abbaspour, I've not tried it with GlassFish v3 so not sure if it is something related to that. Any case, I recommend posting a question to users@ajax.dev.java.net for a wider audience.
Posted by BATTERY on November 27, 2008 at 04:55 PM PST #