Howto Run SQL Queries From Your Browser

- TARGET-AUDIENCE: *beginner* advanced expert
- COCOON-RELEASES: 2.0.3, 2.0.4
- DOCUMENT-STATUS: *draft* reviewed released

What you will get from this page

Simple instructions describing how to connect your browser to your SQL database. You will then be able to execute SQL commands in your browser's location bar, like this:

http://localhost:8080/cocoon/~pete/sql?stmt=select * from user


http://localhost:8080/cocoon/~pete/sql?stmt=insert into user values(NULL,"pete","")

or, more dangerously:

http://localhost:8080/cocoon/~pete/sql?stmt=delete from user where uid="pete"

or even:(!)

http://localhost:8080/cocoon/~pete/sql?stmt=drop table user

...which is why I need to warn you right now:

Warning! do not use this on a production system, or a system exposed to the public Internet, etc.

This howto example severly compromises system security. Kids, don't try this at home, don't run with scissors, don't do drugs, wear a rubber, etc.

So why might you want to do this?

Well, if you're a developer, or a Cocoon user, you might want to use this as a handy SQL client. If you're neither, then you probably shouldn't be reading this. This technique might amuse your audience during software demo presentations, or provide a very minor amount of help with development, but it will not win you a Nobel Prize, and it is unlikely to enhance your allure to exotic women. File this under "Stupid Cocoon Tricks" or "Worst Practices" and go outside and play instead!

Your basic skills

Modest familiarity with trivial Cocoon applications.

Technical prerequisites

Database connected to Cocoon.



Here are the steps we will follow:

Step 1 - Add the ServerPagesGenerator to your sitemap.xmap

Add this to your sitemap.xmap:


Step 2: Add a matcher to your sitemap.xmap

Add this to your <pipeline>:

   <map:match pattern="sql">
    <map:generate type="xsp" src="sql.xsp">
     <map:parameter name="use-request-parameters" value="true"/>
    <map:serialize type="xml"/>

Step 3: Create sql.xsp

Copy and save as sql.xsp: (be sure to substitute the name of your pool, as configured in cocoon.xconf in the <esql:pool> tag below)

<?xml version="1.0" encoding="iso-8859-1"?>


  <esql:pool>mydevpool</esql:pool> <!-- your pool here -->
    <xsp-request:get-parameter name="stmt"/>
     <xsp:element name="result">
    <xsp:element name="result">
    <xsp:element name="result">


Now you should be able to execute an SQL statement on your database by typing the statement in the location bar of your browser like this:

http://path_to_your_db_host:your_port/path_to_your_cocoon_work_dir/sql?stmt=select * from user

Of course, whether this works or not depends on whether you have a table called 'user' in the db specified by your <datasource> (defined in cocoon.xconf) If it works, you should see something like:

  <name>Pete Farmer</name>
  <name>Robert Vesco</name>
  <name>Sonny Barger</name>

If it doesn't work, you may want to make sure that your database is configured properly to sing and dance merrily with Cocoon. See SpecificDatabaseConnection

Step 4: (Optional) Post-process the results

You may want to flatten your <resultset>, and collapse the XML elements returned by the XSP generator into attributes of the <result> element. This may be convienient if your are using other products in your software stack, or whatever. Otherwise, you're done. Go outside and ride your skateboard!

If you've decided you're not done, then add this line to the matcher <map:match pattern="sql"> in your sitemap.xmap:

    <map:transform src="sql.xsl"/>

so that it now looks like this:

   <map:match pattern="sql">
    <map:generate type="xsp" src="sql.xsp">
     <map:parameter name="use-request-parameters" value="true"/>
    <map:transform src="sql.xsl"/>
    <map:serialize type="xml"/>

And create the file sql.xsl:

<?xml version="1.0" encoding="iso-8859-1"?>
    xmlns:xsl="" version="1.0">
<xsl:strip-space elements="*"/>
 <xsl:template match="*">
   <xsl:for-each select="@*|*[not(* or @*)]">
    <xsl:attribute name="{name(.)}"><xsl:value-of select="."/>
   <xsl:apply-templates select="*[* or @*]|text()"/>

Now when you run your query your results will look like this:

 <result recnum="1" uid="pete" name="Pete Farmer"/>
 <result recnum="2" uid="bob" name="Robert Vesco"/>
 <result recnum="3" uid="sonny" name="Sonny Barger"/>

which, depending on your application, may be more useful.

The End

With a few drops of the magical Cocoon krazy glue, you have now "glued" your browser to your db. Enjoy. That's all. Finito. What do you want for nothing?

page metadata


HowtoQueryFromWebBrowser (last edited 2009-09-20 23:42:55 by localhost)