You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by mfaine <mf...@knology.net> on 2004/12/13 22:29:34 UTC

database code in bean constructor

I'd like to put database code in my bean constructor.

The user logs will authenticate against a domain using JCIFS but still has
the ability to update user specific fields: phone, project, center,
organization, etc.  

I'd like to do the following:

When user visits "user information" I'd like to do the following:

Query for and populate bean properties if user already has entered
information in a previous session, if not my "udpate" button on the form can
update the database for any new changes.

Problem is the form comes up blank even if there is data in the database for
the user?  The code is correct because I can print out the databse results
using log4j and it works fine?

Is there some restriction to putting this kind of code in the bean constructor?

I can't use an h:dataTable because it seems to be restricted in its layout
capabilities. I only need to return one result anyway not a whole page and I
could not get the h:dataTable tag to format other than for a list of results.

Tips, Suggestions would be appreciated.

Thanks,
-Mark




RE: database code in bean constructor

Posted by Matthias Wessendorf <ma...@matthias-wessendorf.de>.
Hi Mark,

is your ResultSet scrollable and updateable?

I tried some weeks ago this: (worked)
(code below).

It is was test-class for me. I recommend you
*not* to uses SQL inside your BackingBean.
BackingBeans are objects that live in
presentation-tier. SQL is designed to living
in integration-tier.

I recommend you to use a BusinessDelegate
that uses TransferObjekts.
BusinessDelegate is an interface,
that contains your business-Cases.

createCustomer(CustomerTO cto);
...

Create an Implementation for BusinessDelegate
(EJBDelegate, SQLDelegate etc.)
(see J2EE-Patterns-Book for more)

you have a *clear* architecture
BackingBean -> BusinessDelegate --> Storeage

if you change your backend form SQL to JDO,
EJB, what else ... your BackingBeans may work,
since you only change the Impl of your BusinessDelegate

okay, but now here is my dummy SQL-BackingBean ;-)

<code>
package de.w3l.jsf.html;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Datenbank {
	
	private ResultSet rs;
	
	public Datenbank(){
		try {
	
Thread.currentThread().getContextClassLoader().loadClass("com.mysql.jdbc
.Driver").newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		Connection con;
		try {
			con =
				DriverManager.getConnection(
					"jdbc:mysql://localhost/jsf",
					"root",
					"");
			Statement stmt = con.createStatement(
			ResultSet.TYPE_SCROLL_SENSITIVE,
			ResultSet.CONCUR_UPDATABLE
			);
			this.rs = stmt.executeQuery("SELECT * FROM
VORLESUNG");
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
	}
	public ResultSet getRs() {
		return rs;
	}
	public void setRs(ResultSet set) {
		rs = set;
	}
}
</code>

> -----Original Message-----
> From: mfaine [mailto:mfaine@knology.net] 
> Sent: Monday, December 13, 2004 10:30 PM
> To: myfaces-user@incubator.apache.org
> Subject: database code in bean constructor
> 
> 
> I'd like to put database code in my bean constructor.
> 
> The user logs will authenticate against a domain using JCIFS 
> but still has the ability to update user specific fields: 
> phone, project, center, organization, etc.  
> 
> I'd like to do the following:
> 
> When user visits "user information" I'd like to do the following:
> 
> Query for and populate bean properties if user already has 
> entered information in a previous session, if not my "udpate" 
> button on the form can update the database for any new changes.
> 
> Problem is the form comes up blank even if there is data in 
> the database for the user?  The code is correct because I can 
> print out the databse results using log4j and it works fine?
> 
> Is there some restriction to putting this kind of code in the 
> bean constructor?
> 
> I can't use an h:dataTable because it seems to be restricted 
> in its layout capabilities. I only need to return one result 
> anyway not a whole page and I could not get the h:dataTable 
> tag to format other than for a list of results.
> 
> Tips, Suggestions would be appreciated.
> 
> Thanks,
> -Mark
> 
> 
>