You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Ling Kok Choon <ko...@brel.com> on 2001/08/07 09:42:44 UTC

How to getConnection in Action ?

Hi All,

   I try to use Action in the Sitemap to dinamicaly select stylesheet file  base on the request 
*.xml file. so in the Action, i need to get a connection from the connection pool to connect to 
the database. But How to get the connection ? I have read the Cocoon2 Documentation ( section 
Using Databases) but the example code is wrong, i can't find the import classes (e.g. 
org.apache.avalon.ComponentManager, org.apache.avalon.ComponentSelector ) from the Avalon's 
package !

   Do you have any idea ? 


Thank you.

Kok Choon.



--------------------------------------------------------
Name: Ling Kok Choon
E-mail: Ling Kok Choon <ko...@brel.com>
Date: 08/07/01
Time: 15:42:44

This message was sent by Z-Mail Pro - from NetManage
NetManage - delivers Standards Based IntraNet Solutions
--------------------------------------------------------

---------------------------------------------------------------------
Please check that your question has not already been answered in the
FAQ before posting. <http://xml.apache.org/cocoon/faqs.html>

To unsubscribe, e-mail: <co...@xml.apache.org>
For additional commands, e-mail: <co...@xml.apache.org>


Re: How to getConnection in Action ?

Posted by Christian Haul <ha...@dvs1.informatik.tu-darmstadt.de>.
On 07.Aug.2001 -- 03:42 PM, Ling Kok Choon wrote:
> Hi All,
> 
>    I try to use Action in the Sitemap to dinamicaly select stylesheet file  base on the request 
> *.xml file. so in the Action, i need to get a connection from the connection pool to connect to 
> the database. But How to get the connection ? I have read the Cocoon2 Documentation ( section 
> Using Databases) but the example code is wrong, i can't find the import classes (e.g. 
> org.apache.avalon.ComponentManager, org.apache.avalon.ComponentSelector ) from the Avalon's 
> package !
> 
>    Do you have any idea ? 

Look at the Database*Actions.

	Chris.

-- 
C h r i s t i a n       H a u l
haul@informatik.tu-darmstadt.de
    fingerprint: 99B0 1D9D 7919 644A 4837  7D73 FEF9 6856 335A 9E08

---------------------------------------------------------------------
Please check that your question has not already been answered in the
FAQ before posting. <http://xml.apache.org/cocoon/faqs.html>

To unsubscribe, e-mail: <co...@xml.apache.org>
For additional commands, e-mail: <co...@xml.apache.org>


AW: How to getConnection in Action ?

Posted by Max Larsson <ma...@gmservice.de>.
Hi,

i am doing it that way:

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import org.apache.avalon.excalibur.datasource.DataSourceComponent;
import org.apache.avalon.framework.component.ComponentException;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.framework.component.ComponentSelector;
import org.apache.avalon.framework.component.Composable;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.cocoon.Constants;
import org.apache.cocoon.Roles;
import org.apache.cocoon.acting.AbstractComplementaryConfigurableAction;
import org.apache.cocoon.environment.Redirector;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.environment.Session;
import org.apache.cocoon.environment.SourceResolver;

public class CategoryAction extends AbstractComplementaryConfigurableAction
{
	private DataSourceComponent datasource;

	public void compose(ComponentManager manager) throws ComponentException {
		getLogger().debug( "HelpDesk : application.CategoryAction/compose :
start" );

		ComponentSelector selector = (ComponentSelector)manager.lookup(
Roles.DB_CONNECTION );
		datasource = (DataSourceComponent)selector.select( "helpdesk" );

		getLogger().debug( "HelpDesk : application.CategoryAction/compose :
datasource = " + datasource );
		getLogger().debug( "HelpDesk : application.CategoryAction/compose :
end" );
	}

	public Map act( Redirector redirector,SourceResolver resolver,Map
objectModel,String source,Parameters params ) throws java.lang.Exception {
		getLogger().debug( "HelpDesk : application.CategoryAction/act : start" );

		getLogger().debug( "HelpDesk : application.CategoryAction/act : redirector
= " + redirector );
		getLogger().debug( "HelpDesk : application.CategoryAction/act : resolver
= " + resolver );
		getLogger().debug( "HelpDesk : application.CategoryAction/act :
objectModel = " + objectModel );
		getLogger().debug( "HelpDesk : application.CategoryAction/act : source
= " + source );
		getLogger().debug( "HelpDesk : application.CategoryAction/act : params
= " + params );

		Map     result  = new HashMap();
		Request request = (Request)objectModel.get( Constants.REQUEST_OBJECT );
		Session session = request.getSession( true );

		String action       = request.getParameter( "action" );
		String category_xpl = request.getParameter( "category_xpl" );
		String category_id  = request.getParameter( "category_id" );

		getLogger().debug( "HelpDesk : application.CategoryAction/act : request
= " + request );
		getLogger().debug( "HelpDesk : application.CategoryAction/act : session
= " + session );
		getLogger().debug( "HelpDesk : application.CategoryAction/act : action
= " + action );
		getLogger().debug( "HelpDesk : application.CategoryAction/act : category
id = " + category_id );
		getLogger().debug( "HelpDesk : application.CategoryAction/act : category
xpl= " + category_xpl );

		if( action.startsWith( "Save" ) ) {
			try {
				Connection con  = datasource.getConnection();
				Statement  sta  = con.createStatement();

				getLogger().debug( "HelpDesk : application.UploadAction/act : insert
category problem relation = " + sta.execute( "INSERT INTO menu_problem_rel
( menu_ref,problem_ref ) VALUES ( " + category_id + "," +
request.getParameter( "id" ) + ")" ) );

				sta.close();
				con.close();
			}
			catch( SQLException exc ) {
				getLogger().debug( "HelpDesk : application.UploadAction/act :
SQLException!",exc );
			}
		}
		else if( action.startsWith( "Delete" ) ) {
				Connection con  = datasource.getConnection();
				Statement  sta  = con.createStatement();

				getLogger().debug( "HelpDesk : application.UploadAction/act : insert
category problem relation = " + sta.execute( "DELETE FROM menu_problem_rel
WHERE problem_ref = " + request.getParameter( "id" ) + " AND menu_ref = " +
category_id ) );

				sta.close();
				con.close();
		}

		getLogger().debug( "HelpDesk : application.CategoryAction/act : result = "
+ result );
		getLogger().debug( "HelpDesk : application.CategoryAction/act : end" );
		return Collections.unmodifiableMap( result );
	}
}

> -----Ursprüngliche Nachricht-----
> Von: Ling Kok Choon [mailto:kokchoon@brel.com]
> Gesendet: Dienstag, 7. August 2001 09:43
> An: cocoon-users@xml.apache.org
> Betreff: How to getConnection in Action ?
>
>
> Hi All,
>
>    I try to use Action in the Sitemap to dinamicaly select
> stylesheet file  base on the request
> *.xml file. so in the Action, i need to get a connection from
> the connection pool to connect to
> the database. But How to get the connection ? I have read the
> Cocoon2 Documentation ( section
> Using Databases) but the example code is wrong, i can't find
> the import classes (e.g.
> org.apache.avalon.ComponentManager,
> org.apache.avalon.ComponentSelector ) from the Avalon's
> package !
>
>    Do you have any idea ?
>
>
> Thank you.
>
> Kok Choon.
>
>
>
> --------------------------------------------------------
> Name: Ling Kok Choon
> E-mail: Ling Kok Choon <ko...@brel.com>
> Date: 08/07/01
> Time: 15:42:44
>
> This message was sent by Z-Mail Pro - from NetManage
> NetManage - delivers Standards Based IntraNet Solutions
> --------------------------------------------------------
>
> ---------------------------------------------------------------------
> Please check that your question has not already been answered in the
> FAQ before posting. <http://xml.apache.org/cocoon/faqs.html>
>
> To unsubscribe, e-mail: <co...@xml.apache.org>
> For additional commands, e-mail: <co...@xml.apache.org>
>
>


---------------------------------------------------------------------
Please check that your question has not already been answered in the
FAQ before posting. <http://xml.apache.org/cocoon/faqs.html>

To unsubscribe, e-mail: <co...@xml.apache.org>
For additional commands, e-mail: <co...@xml.apache.org>