Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JSF DB Access

Posted on 2006-10-20
14
Medium Priority
?
798 Views
Last Modified: 2008-01-09
What is the recommended mechanism for DB access with JSF?

Can you provide a simple example that I could work from that gives me:

Any config file(s) information I need to add
Any property file information I need to add
A sample JSF file
A sample bean (I'm guessing this will be needed)
Any other code that I would need

I'm using postgres database (I"m guessing that isn't terribly relevant other than the fact that I might be using my JDBC postgres driver) and Tomcat app server

Thanks for any quick responses I need to get going quickly on this.
0
Comment
Question by:kls1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 4
14 Comments
 

Author Comment

by:kls1
ID: 17778033
I don't see any info on interaction with databases, am I overlooking it?
0
 
LVL 10

Expert Comment

by:jaggernat
ID: 17778063
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:kls1
ID: 17778312
I had found the first one also -- it seems to be geared to MS Access and Windows applications. Mine will be Linux/Unix based.

Does the second one have the needed pieces? I don't see any sample code -- am I missing it?

Thanks...
0
 
LVL 10

Expert Comment

by:jaggernat
ID: 17778340
sample code for what ?
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17780879
Use SDO: while this isn't very popular in general, it actually works really well with JSF. It's what IBM's Rational tools use to access data. Of course, if you want to really do The Right Thing, you could provide access to the data in an SOA manner, exposing access to the data as a service.
0
 

Author Comment

by:kls1
ID: 17788835
Jaggernat -- I hope I'm not being simplistic, but I've been looking for sample code that will do DB access from JSF "the right way". I have just started JSP and JSF, so I am _very_ clueless about what I'm doing (yes, I know Java), therefore am looking for a simple example that gives me all the basic pieces -- as JSF code, bean code, and property config files. Usually I can find this stuff very easily on the net but have not been able to find something that will help me get a quick start (I have people breathing down my throat to get this done quickly and "the right way".
0
 

Author Comment

by:kls1
ID: 17788850
Bpmurray -- do you thin either one of these methods would be easy to get a quick start for a newbie in this stuff like me? (Someone who knows Java but is absolutely clueless with JSF and JSP?)

I want to do this the right way but I also need to get it done quickly. (And I want to do it cleanly, I've seen too many messes that have stuck around from people trying to write code quickly...) I guess I want my cake and eat it too.... Is it possible? Or is there a way I'm best off going with in order to satisfy my managers by getting it done fast?
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17790661
Perhaps a good way to start would be to use the beta of Rational's new version of tools: http://www-306.ibm.com/software/rational/beta/v7/ - sign up for the architect product. This will make it realy easy to create your apps.

Alternatively, since you're happy enough with DB access, encapsulate the DB access in a bean. Then you simply access it using the EL (expression language). So you get something like:
   <x:output value="#{bean.property}">

This gets translated in JSF to a call to bean.getProperty. You can tag the bean as being a "managed bean" in which case it's already know to JSF so you don't have to declare it anywhere. I'll try to throw together a basic example later and post it in the files section.
0
 
LVL 15

Accepted Solution

by:
bpmurray earned 1600 total points
ID: 17799460
Sorry - haven't been able to make time, but here's something you could use to try to understand how it all hangs together. First, here's a simple bean - you could create something equivalent that retrieves data from a database rather than this static thing:

/******* Class 1: Companies - a collection of Company object **********/
package my.bean;

import java.util.ArrayList;

public class Companies {
      // Variables used for the class
      ArrayList<Company> companiesList = new ArrayList<Company>();

      /**
       *
       */
      public Companies() {
            super();

            // Add companies
            companiesList.add(new Company("Small Wonders"));
            companiesList.add(new Company("Small Rocks"));
            companiesList.add(new Company("Smart Travellers"));
            companiesList.add(new Company("Silly"));
            companiesList.add(new Company("Sweet Stuff"));
      }

      public Company[] getCompanies() {
            Company[] co = new Company[0];
            co = (Company[]) companiesList.toArray(co);
            return co;
      }

}

/*********** Class 2 : The Company object ********/
package my.bean;

public class Company {
      String name = null;

      public Company() {
            this("");
      }

      public Company(String Name) {
            super();
            setName(Name);
      }

      /**
       * @return Returns the name.
       */
      public String getName() {
            return name;
      }

      /**
       * @param name The name to set.
       */
      public void setName(String name) {
            this.name = name;
      }
}
/****************************/


Next, for the simple page. This page has a simple table which is used to display the list of the names, one per cell in the table's single column:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%-- jsf:pagecode language="java" location="/src/pagecode/Index.java" --%><%-- /jsf:pagecode --%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="theme/stylesheet.css"      title="Style">
</head>
<f:view>
      <body>
            <h:form id="form1" styleClass="form">
                  <h:dataTable border="0" cellpadding="2" cellspacing="0"
                        columnClasses="columnClass1" headerClass="headerClass"
                        footerClass="footerClass" rowClasses="rowClass1, rowClass2"
                        id="tableEx1" styleClass="dataTableEx"
                        value="#{pc_Index.companies.companies}" var="varCompanies">
                        <h:column id="column1">
                              <h:outputText id="textName1" value="#{varCompanies.name}" styleClass="outputText"></h:outputText>
                        </h:column>
                  </h:dataTable>
            </h:form>
      </body>
</f:view>
</html>


0
 
LVL 10

Assisted Solution

by:jaggernat
jaggernat earned 400 total points
ID: 17800655
>>>>but I've been looking for sample code that will do DB access from JSF "the right way

http://www.oracle.com/technology/pub/articles/cioroianu_jsfdb.html
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 18002233
Just one more comment: I got a mail from Sun today about using JSF with the new Java Persistence APIs, which might help you. Have a look at this sample: http://communications1.sun.com/r/c/r?1.1.3J1.2Vc.12RRpW.C00W*O..H.D4F6.1aec.3KCXAZ
0
 

Author Comment

by:kls1
ID: 18021325
Thanks for the help and suggestions, I really appreciate it.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
By default Outlook 2016 displays only one time zone in the Calendar. The following article explains how to display two time zones in one calendar view.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question