You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Martin Rusnak <ma...@cyberspace.sk> on 2004/10/23 00:53:28 UTC

Getting database connection in flow script

Dear All,

I am using CForms/flow-script. When the form is submited, I usually call
a methods in my database utility classes from flow-script to perform a
DB queries. For that I need to obtain a database connection from the
pool. I tried the method: Database.getConnection("connection-id"), but
it returns ScriptableConnection class. Is it possible to get reference
to java.sql.Connection in flow-script? What is the preferred way to
handle database calls in flow-script?

thanks,
Martin

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Getting database connection in flow script

Posted by Martin Rusnak <ma...@cyberspace.sk>.
Hello,
I solved the problem getting database connection so that I created
Registrator java class which implements Serviceable interface. Then
the flowscript loads Registrator class by method cocoon.createObject()
in order to allow accessing Avalon services.

Now I need to solve another problem. When the user with the specified
username exists in the database, Registrator class thows
RegistrationException and the form shall be displayed again with an
error message at the corresponding textfield. I have following questions:

1. Is it possible to specify exception class in catch clause? Currently
   I use instanceof operator.

2. How to setup errorMessage for the specific widget?

Here is the script:

registration() {

    var form = new Form("forms/registration.xml")
    var success

    do {
        success = true
        form.showForm("registration-display-pipeline")

        var model = form.getModel()

        var registrator = cocoon.createObject("Registrator")

        try {
            registrator.register(model.username, model.password)
        } catch(e) {
            if(e instanceof RegistrationException) {
                success = false
            } else throw e
        }
    } while(!success)

    cocoon.sendPage("success.html")
}




Antonio Gallardo wrote:
> Servus Martin!
> 
> Martin Rusnak dijo:
> 
>>Dear All,
>>
>>I am using CForms/flow-script. When the form is submited, I usually call
>>a methods in my database utility classes from flow-script to perform a
>>DB queries. For that I need to obtain a database connection from the
>>pool. I tried the method: Database.getConnection("connection-id"), but
>>it returns ScriptableConnection class. Is it possible to get reference
>>to java.sql.Connection in flow-script?
> 
> 
> I never used directly java.sql.* in JSFlow, but I think it should work.
> 
> 
>>What is the preferred way to
>>handle database calls in flow-script?
> 
> 
> Well, I think the "prefered" way depends of how much you will use
> databases. There are diferent approach:
> 
> The olders are:
> 
> 1-ESQL tranformer
> 2-ESQL logicsheet in XSP
> 3-Modular DB Actions - http://wiki.apache.org/cocoon/ReinhardHorn
> 
> The "newer" approach is using a O/R mapping tool - i.e.:Apache OJB,
> Hibernate, etc. This approach is particular powerful because allow you to
> have better SoC + all the advantage that a O/R tool + better abstraction.
> 
> You can find more info in section 7. "Databases" at:
> http://wiki.apache.org/cocoon/HowTos
> 
> Best Regards,
> 
> Antonio Gallardo.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Getting database connection in flow script

Posted by beyaNet <an...@jibeya.com>.
Hi,
you can indeed use java in flowscript. Specify you doc as follows:

importClass(Packages.java.util.Iterator);

then:

var myIt = new Iterator();

var someval = myIt.getMethod();

hope this helps.

Andrew
On 23 Oct 2004, at 14:51, Antonio Gallardo wrote:

> Servus Martin!
>
> Martin Rusnak dijo:
>> Dear All,
>>
>> I am using CForms/flow-script. When the form is submited, I usually 
>> call
>> a methods in my database utility classes from flow-script to perform a
>> DB queries. For that I need to obtain a database connection from the
>> pool. I tried the method: Database.getConnection("connection-id"), but
>> it returns ScriptableConnection class. Is it possible to get reference
>> to java.sql.Connection in flow-script?
>
> I never used directly java.sql.* in JSFlow, but I think it should work.
>
>> What is the preferred way to
>> handle database calls in flow-script?
>
> Well, I think the "prefered" way depends of how much you will use
> databases. There are diferent approach:
>
> The olders are:
>
> 1-ESQL tranformer
> 2-ESQL logicsheet in XSP
> 3-Modular DB Actions - http://wiki.apache.org/cocoon/ReinhardHorn
>
> The "newer" approach is using a O/R mapping tool - i.e.:Apache OJB,
> Hibernate, etc. This approach is particular powerful because allow you 
> to
> have better SoC + all the advantage that a O/R tool + better 
> abstraction.
>
> You can find more info in section 7. "Databases" at:
> http://wiki.apache.org/cocoon/HowTos
>
> Best Regards,
>
> Antonio Gallardo.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
>
>

Re: Getting database connection in flow script

Posted by Antonio Gallardo <ag...@agssa.net>.
Servus Martin!

Martin Rusnak dijo:
> Dear All,
>
> I am using CForms/flow-script. When the form is submited, I usually call
> a methods in my database utility classes from flow-script to perform a
> DB queries. For that I need to obtain a database connection from the
> pool. I tried the method: Database.getConnection("connection-id"), but
> it returns ScriptableConnection class. Is it possible to get reference
> to java.sql.Connection in flow-script?

I never used directly java.sql.* in JSFlow, but I think it should work.

> What is the preferred way to
> handle database calls in flow-script?

Well, I think the "prefered" way depends of how much you will use
databases. There are diferent approach:

The olders are:

1-ESQL tranformer
2-ESQL logicsheet in XSP
3-Modular DB Actions - http://wiki.apache.org/cocoon/ReinhardHorn

The "newer" approach is using a O/R mapping tool - i.e.:Apache OJB,
Hibernate, etc. This approach is particular powerful because allow you to
have better SoC + all the advantage that a O/R tool + better abstraction.

You can find more info in section 7. "Databases" at:
http://wiki.apache.org/cocoon/HowTos

Best Regards,

Antonio Gallardo.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Getting database connection in flow script

Posted by Martin Rusnak <ma...@cyberspace.sk>.
>> I am using CForms/flow-script. When the form is submited, I usually call
>> a methods in my database utility classes from flow-script to perform a
>> DB queries. For that I need to obtain a database connection from the
>> pool. I tried the method: Database.getConnection("connection-id"), but
>> it returns ScriptableConnection class. Is it possible to get reference
>> to java.sql.Connection in flow-script? What is the preferred way to
>> handle database calls in flow-script?
> 
> 
> Martin,
> 
> I don't see why it's not possible to get a Connection the normal way in 
> Flowscript.
> 
> There is no "preferred way" to do it; you're free to do it however you 
> want.  You could probably hide it behind a simple facade written in 
> Java.  You can instantiate normal Java classes in Flowscript.  See: 
> http://cocoon.apache.org/2.1/userdocs/flow/java.html

Tonny,

I'll provide an example code snippets:

In file DBUtil.java:

public static void register(Connection con, String username, String password)
throws SQLException {

    // JDBC calls

}

In file registration.js:

function registration() {

    var form = new Form("forms/registration.xml");

    form.showForm("registration-display-pipeline");

    var model = form.getModel();

    var conn = null;
    try {
        conn = Database.getConnection("ser-connection");
        DBUtil.register(conn, model.username, model.password);
    } catch(ex) {
        cocoon.log.error(ex)
    } finally {
        if(conn != null)conn.close()
    }
    
    cocoon.sendPage("success.html");
}

When running the form, the following exception occurred:

org.mozilla.javascript.EvaluatorException: Cannot convert org.apache.cocoon.components.flow.javascript.ScriptableConnection@1cd3dd7 to java.sql.Connection

So the register() method requires java.sql.Connection as a parameter but the
Database.getConnection() in flow-script returns ScriptableConnection object.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Getting database connection in flow script

Posted by Tony Collen <co...@umn.edu>.
Martin Rusnak wrote:

> Dear All,
> 
> I am using CForms/flow-script. When the form is submited, I usually call
> a methods in my database utility classes from flow-script to perform a
> DB queries. For that I need to obtain a database connection from the
> pool. I tried the method: Database.getConnection("connection-id"), but
> it returns ScriptableConnection class. Is it possible to get reference
> to java.sql.Connection in flow-script? What is the preferred way to
> handle database calls in flow-script?

Martin,

I don't see why it's not possible to get a Connection the normal way in 
Flowscript.

There is no "preferred way" to do it; you're free to do it however you 
want.  You could probably hide it behind a simple facade written in 
Java.  You can instantiate normal Java classes in Flowscript.  See: 
http://cocoon.apache.org/2.1/userdocs/flow/java.html

Tony

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org