Solved

JSF DB Access

Posted on 2006-10-20
14
789 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
  • 5
  • 4
  • 4
14 Comments
 
LVL 10

Expert Comment

by:jaggernat
ID: 17777842
0
 

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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 400 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 100 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
library class in java 1 93
immutable object concept 5 127
iterator/ListIterator approach 17 39
null output 3 35
Starting your own business is always a daunting process, and for most people it is brand new experience. Avoid the common pitfalls by following these tips to start on the road to success.
The advancement in technology has been a great source of betterment and empowerment for the human race, Nevertheless, this is not to say that technology doesn’t have any problems. We are bombarded with constant distractions, whether as an overload o…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 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