You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Deepak Suldhal <dt...@yahoo.com> on 2005/03/09 01:28:00 UTC

Simple database access (mysql)

Hi
  If someone could please help us with a simple step by step procedure of displaying data from
a database query (for ex MySQL).
  In Tapestry where do we create the connection class. 
 
I tried to incorporate the standard driver class and connection class in the form.listners method
this works once, If I want to access again in a new browser (IE) page it gives Connection already closed error.
 
It would be great to understand tapestry if we are able to access data from tables.
 
If someone would take a little pain and put togather this document it will really benifit.
 
Thanks
Deepak

		
---------------------------------
Celebrate Yahoo!'s 10th Birthday! 
 Yahoo! Netrospective: 100 Moments of the Web 

Re: Simple database access (mysql)

Posted by Bruno Sanguinetti <ke...@sussex.ac.uk>.
I have not really had time to test your code, but from a quick look, it  
seems that you instantiate your database manager in the Page class.  
Tapestry uses a pool of pages, which means that tapestry will instantiate  
your Home class more than once, and keep all the instances in memory to  
serve different clients.
This means that it will try to make many database managers, and here you  
might come across problems.
To solve these you want to make just one database manager instance that  
you will use. This is what I have done in the example that I posted to you  
earlier.

Things that you might want to use are:
- static methods/variables
- the Global or Visit object
- overriding the Application servlet's init and destroy methods

You might get away with declaring your connection as a static property of  
the page, but I don't think it's a great idea ...

All the best! Hope this helps ...


Bruno

> Hi
> I have a form which has a datepicker and a radiogroup
> consisting of 3 choices. user enters date and selects
> on of the option and submits ok. based on which radion
> button is on I present the next page. All this works
> good.
>
> I now just want to add one small function to this
> before proceeding to the next page I just want to see
> the count of records in the table. And this is where I
> have problems,
>
> I do not want to display anything on the form from
> database. I just want to query and see if there are
> records.
>
> Please help me contruct this code. As it has to be in
> production very soon.
>
> Here is what I have tried.
>
> package com.test.fin46;
> import org.apache.tapestry.IRequestCycle;
> import org.apache.tapestry.html.BasePage;
>
> import java.sql.Connection;
> import java.sql.PreparedStatement;
> import java.sql.ResultSet;
> import java.sql.ResultSetMetaData;
> import java.sql.SQLException;
> import java.util.Date;
>
> public class Home extends BasePage {
>         private Date date;
>         public static final int _Mortgage = 1;
>         public static final int _LC       = 2;
>         public static final int _BOTH     = 3;
>
>         private int misses = 3;
>         private String error = "";
>         private String errorMsg;
>         private int _count;
>
>         public void detach() {
>                 misses = 3;
>                 error = null;
>                 super.detach();
>         }
>
>         public int getMisses() {
>                 return misses;
>         }
>
>         public void setMisses(int value) {
>                 misses = value;
>         }
>
>         /**
>          * @return Returns the date.
>          */
>         public Date getDate() {
>                 return date;
>         }
>         /**
>          * @param date The date to set.
>          */
>         public void setDate(Date date) {
>                 this.date = date;
>         }
>         public String getError() {
>                 return error;
>         }
>
>         public boolean getHasError() {
>                 if ((error == null) || (error.length()
> == 0)) {
>                         return (false);
>                 }
>                 return (true);
>         }
>
>         public void formSubmit(IRequestCycle cycle) {
>                 if (misses == 0) {
>                         error = "Please select a game
> difficulty.";
>                         return;
>                 }
>                if (getDate()==null) {
>                         errorMsg = "Please Enter a
> Valid Date";
>                         return;
>                         }
>
> //Here I want to query the database and see if there
> are records for processing only then I would like to
> proceed to next page.
>                 try
>                 {
>                         DatabaseManager dm = new
> DatabaseManager();
>                         Connection con =
> dm.getConnection();
>                        String myquery  = "select
> count(*) as current_count from
> pslwrkdb..fin46_instmt";
>                         PreparedStatement pstmt =
> null;
>                         ResultSet rs = null;
>                         try {
>                                 pstmt =
> con.prepareStatement(myquery);
>                                 rs =
> pstmt.executeQuery();
>                                 while(rs.next())
>                                 {
>                                         _count =
>  rs.getInt("current_count");
>                                }
>                         }
>                         catch (SQLException se) {
> //System.out.println(se.getMessage());
>                         }
>                         finally {
>                                 if (pstmt != null) {
>                                         try {
> pstmt.close();
>                                         }
>                                         catch
> (SQLException ex1) {
> ex1.printStackTrace();
>                                         }
>                                 }
>                                 if (rs != null) {
>                                         try {
> rs.close();
>                                         }
>                                         catch
> (SQLException ex1) {
> ex1.printStackTrace();
>                                         }
>                                 }
>                                 if (con != null) {
>                                         try {
> //oppslogger.info("Releasing Connection ");
> con.close();
>                                         }
>                                         catch
> (SQLException ex2) {
> ex2.printStackTrace();
>                                         }
>                                 }
>                         }
>                 }
>                 catch (Exception e)
>                 {
>                         errorMsg = "Error occured
> while reading the database";
>                 }
>                 if ( _count == 0) {
>                         errorMsg = "Data is not yet
> ready Please try later";
>                         return;
>                         }
>                //Visit visit = (Visit)getVisit();
>                 //visit.startGame(misses);
>                 if (misses == 1){
>                 cycle.activate("Mortgage");
>                 }
>                 if (misses == 2){
>                         cycle.activate("LC");
>                         }
>                 if (misses == 3){
>                         cycle.activate("BOTH");
>                         }
>        }
>         /**
>          * @return Returns the errorMsg.
>          */
>        public String getErrorMsg() {
>                 return errorMsg;
>         }
> }
>
>
> and my driver manager is
>
> package com.test.fin46;
>
> import java.sql.Connection;
> import java.sql.Driver;
> import java.sql.SQLException;
> import java.util.Properties;
> import java.sql.DriverManager;
>
> public class DatabaseManager {
>          private static Connection con = null;
>
>          //Constructor
>          public DatabaseManager() {
>             // create connection object con here
>             if (con == null) {
>               try {
>                 Driver d = (Driver)
> Class.forName("com.sybase.jdbc2.jdbc.SybDriver").newInstance();
>
>                 Properties props = new Properties();
>                 props.put("user", "deepak");
>                 props.put("password", "password");
>                 props.put("server",
> "jdbc:sybase:Tds:re2188sr1/pslwrkdb");
>                 con =
> DriverManager.getConnection("jdbc:sybase:Tds:re2unx188:2025",props);
>
> //System.out.println(d.getMajorVersion());
> //System.out.println(d.getMinorVersion());
>              }
>               catch (Exception e) {
>                 System.out.println("class not
> found1");
>                 System.out.println(e.getMessage());
>                 System.out.println(e.toString());
>               }
>             }
>           }
>         //method get the connection
>          public static Connection getConnection() {
>             return con;
>           }
>
>          //method close the the connection
>          public void closeConnection() {
>             try {
>               con.close();
>             } catch (SQLException sqle) {
>               System.out.println("Exception in closing
> connection");
>             }
>           }
> }
>
>
>
>
>
>
>
>
> --- Bruno Sanguinetti <ke...@sussex.ac.uk> wrote:
>
>> On Wed, 09 Mar 2005 00:28:00 -0000, Deepak Suldhal
>> <dt...@yahoo.com> wrote:
>>
>> > Hi
>> >   If someone could please help us with a simple
>> step by step procedure
>> > of displaying data from
>> > a database query (for ex MySQL).
>> >   In Tapestry where do we create the connection
>> class.
>> > I tried to incorporate the standard driver class
>> and connection class in
>> > the form.listners method
>> > this works once, If I want to access again in a
>> new browser (IE) page it
>> > gives Connection already closed error.
>> > It would be great to understand tapestry if we are
>> able to access data
>> > from tables.
>> > If someone would take a little pain and put
>> togather this document it
>> > will really benifit.
>> > Thanks
>> > Deepak
>> >
>> > 		
>> > ---------------------------------
>> > Celebrate Yahoo!'s 10th Birthday!
>> >  Yahoo! Netrospective: 100 Moments of the Web
>>
>> You can find a good tutorial on using a database
>> with Tapestry here:
>>
>>
> http://www.sandcastsoftware.com/articlesandtutorials/brownbag/index.html#tapestry
>>
>> They start by implementing a simple DAO pattern,
>> which makes it easier to
>> change your Object-Relational mapping. In the second
>> part they use
>> hibernate.
>>
>> However, if your question is: "where can I start and
>> stop a  global object
>> that manages my connections"
>> You can do so by extending
>> org.apache.tapestry.ApplicationServlet and
>> overriding its init() and destroy() methods.
>>
>> Usually I go vith Hibernate and Tomcat's connection
>> pool, but I can give
>> you an example that does not use Hibernate (uses
>> Berkeley DB). If you use
>> the DAO pattern you can easily swap between your
>> persistence engines at a
>> later stage anyway.
>>
>> Regards,
>>
>> Bruno
>>
>> public class ConjunctiServlet extends
>> org.apache.tapestry.ApplicationServlet{
>>      public void init() throws ServletException {
>>          super.init();
>>          System.out.println("Opening databases
>> ...");
>>          try {
>>              BDB.open();
>>          } catch (DatabaseException e) {
>>              System.out.println("Could not open
>> databases.");
>>              e.printStackTrace();
>>          }
>>      }
>>
>>
>>      public void destroy() {
>>          System.out.println("Closing databases
>> ...");
>>          BDB.close();
>>          super.destroy();
>>      }
>> }
>>
>> where BDB is my Berkeley DB database manager class
>>
>> public class BDB {
>>
>>      public static Environment env;
>>      private static Database _personDB;
>>
>>      public static final void open() throws
>> DatabaseException {
>>          if (env==null){
>>              EnvironmentConfig envConfig = new
>> EnvironmentConfig();
>>              envConfig.setReadOnly(false);
>>              envConfig.setAllowCreate(true);
>>              envConfig.setTransactional(true);
>>
>>              env = new Environment(new
>> File("C:\\TMP\\data"), envConfig);
>>
>>          }
>>          else throw (new DatabaseException("Could
>> not open environment"));
>>      }
>>
>>      public static void close() {
>>          if(_personDB  != null) {
>>              try {
>>                  _personDB.close();
>>              } catch (DatabaseException e) {
>>                  System.out.println("Could not Close
>> database PersonDB" );
>>                  e.printStackTrace();
>>              }
>>          }
>>
>>          //close the environment, if it was open
>>          if (env!=null){
>>              try{
>>                  env.close();
>>              } catch(DatabaseException e) {
>>                  System.err.println("Error closing
>> database environment "
>> + e.toString());
>>              }
>>          }
>>      }
>>
>>      public static final Database getPersonDB()
>> throws DatabaseException {
>>          if(env == null)
>>              throw new DatabaseException("Attempt to
>> open database PersonDB
>> while no environment is configured" );
>>
>>          if(_personDB==null){
>>              DatabaseConfig dbConfig = new
>> DatabaseConfig();
>>              dbConfig.setAllowCreate(true);
>>              dbConfig.setTransactional(true);
>>              _personDB =
>> env.openDatabase(null,"PersonDB",dbConfig);
>>          }
>>          return _personDB;
>>      }
>>
>>      public static Environment getEnv() {
>>          return env;
>>      }
>> }
>>
>>
>> then, In the DAO implementation I just have a static
>> ref to it:
>>
>> public class PersonDAOBDB implements PersonDAO{
>>
>>      static Database personDB = null;
>>      TupleBinding _personBinding = new
>> PersonBinding();
>>      static {
>>          try {
>>              personDB = BDB.getPersonDB();
>>
>>          } catch (DatabaseException e) {
>>              e.printStackTrace();
>>          }
>>      }     // get the personDB
>>
>> ...
>>
>>
> ---------------------------------------------------------------------
>> To unsubscribe, e-mail:
>> tapestry-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail:
>> tapestry-user-help@jakarta.apache.org
>>
>>
>
>
>
> 	
> 		
> __________________________________
> Celebrate Yahoo!'s 10th Birthday!
> Yahoo! Netrospective: 100 Moments of the Web
> http://birthday.yahoo.com/netrospective/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>



-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Simple database access (mysql)

Posted by Deepak Suldhal <dt...@yahoo.com>.
Hi
I have a form which has a datepicker and a radiogroup
consisting of 3 choices. user enters date and selects
on of the option and submits ok. based on which radion
button is on I present the next page. All this works
good.

I now just want to add one small function to this
before proceeding to the next page I just want to see
the count of records in the table. And this is where I
have problems,

I do not want to display anything on the form from
database. I just want to query and see if there are
records.

Please help me contruct this code. As it has to be in
production very soon.

Here is what I have tried. 

package com.test.fin46; 
import org.apache.tapestry.IRequestCycle; 
import org.apache.tapestry.html.BasePage; 

import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.ResultSetMetaData; 
import java.sql.SQLException; 
import java.util.Date; 

public class Home extends BasePage { 
        private Date date; 
        public static final int _Mortgage = 1; 
        public static final int _LC       = 2; 
        public static final int _BOTH     = 3; 
        

        private int misses = 3; 
        private String error = ""; 
        private String errorMsg; 
        private int _count; 

        public void detach() { 
                misses = 3; 
                error = null; 
                super.detach(); 
        } 

        public int getMisses() { 
                return misses; 
        } 

        public void setMisses(int value) { 
                misses = value; 
        } 

        /** 
         * @return Returns the date. 
         */ 
        public Date getDate() { 
                return date; 
        } 
        /** 
         * @param date The date to set. 
         */ 
        public void setDate(Date date) { 
                this.date = date; 
        } 
        public String getError() { 
                return error; 
        } 

        public boolean getHasError() { 
                if ((error == null) || (error.length()
== 0)) { 
                        return (false); 
                } 
                return (true); 
        } 

        public void formSubmit(IRequestCycle cycle) { 
                if (misses == 0) { 
                        error = "Please select a game
difficulty."; 
                        return; 
                } 
                
                if (getDate()==null) { 
                        errorMsg = "Please Enter a
Valid Date"; 
                        return; 
                        } 
                

//Here I want to query the database and see if there
are records for processing only then I would like to
proceed to next page. 
                try 
                { 
                        DatabaseManager dm = new
DatabaseManager(); 
                        Connection con =
dm.getConnection(); 
                        
                        String myquery  = "select
count(*) as current_count from 
pslwrkdb..fin46_instmt"; 
                        PreparedStatement pstmt =
null; 
                        ResultSet rs = null; 
                        try { 
                                pstmt =
con.prepareStatement(myquery); 
                                rs =
pstmt.executeQuery(); 
                                while(rs.next()) 
                                { 
                                        _count =      
 rs.getInt("current_count"); 
                                        
                                } 
                        } 
                        catch (SQLException se) { 
                               
//System.out.println(se.getMessage()); 
                        } 
                        finally { 
                                if (pstmt != null) { 
                                        try { 
                                               
pstmt.close(); 
                                        } 
                                        catch
(SQLException ex1) { 
                                               
ex1.printStackTrace(); 
                                        } 
                                } 
                                if (rs != null) { 
                                        try { 
                                               
rs.close(); 
                                        } 
                                        catch
(SQLException ex1) { 
                                               
ex1.printStackTrace(); 
                                        } 
                                } 
                                if (con != null) { 
                                        try { 
                                               
//oppslogger.info("Releasing Connection "); 
                                               
con.close(); 
                                        } 
                                        catch
(SQLException ex2) { 
                                               
ex2.printStackTrace(); 
                                        } 
                                } 
                        } 
                } 
                catch (Exception e) 
                { 
                        errorMsg = "Error occured
while reading the database"; 
                } 
                if ( _count == 0) { 
                        errorMsg = "Data is not yet
ready Please try later"; 
                        return; 
                        } 
                
                
                //Visit visit = (Visit)getVisit(); 
                //visit.startGame(misses); 
                if (misses == 1){ 
                cycle.activate("Mortgage"); 
                } 
                if (misses == 2){ 
                        cycle.activate("LC"); 
                        } 
                if (misses == 3){ 
                        cycle.activate("BOTH"); 
                        } 
                
        } 
        /** 
         * @return Returns the errorMsg. 
         */ 
        
        public String getErrorMsg() { 
                return errorMsg; 
        } 
        
} 


and my driver manager is

package com.test.fin46; 

import java.sql.Connection; 
import java.sql.Driver; 
import java.sql.SQLException; 
import java.util.Properties; 
import java.sql.DriverManager; 

public class DatabaseManager { 
         private static Connection con = null; 

         //Constructor 
         public DatabaseManager() { 
            // create connection object con here 
            if (con == null) { 
              try { 
                Driver d = (Driver)
Class.forName("com.sybase.jdbc2.jdbc.SybDriver").newInstance();

                Properties props = new Properties(); 
                props.put("user", "deepak"); 
                props.put("password", "password"); 
                props.put("server",
"jdbc:sybase:Tds:re2188sr1/pslwrkdb"); 
                con =
DriverManager.getConnection("jdbc:sybase:Tds:re2unx188:2025",props);

               
//System.out.println(d.getMajorVersion()); 
               
//System.out.println(d.getMinorVersion()); 
                
              } 
              catch (Exception e) { 
                System.out.println("class not
found1"); 
                System.out.println(e.getMessage()); 
                System.out.println(e.toString()); 
              } 
            } 
          } 
          
         //method get the connection 
         public static Connection getConnection() { 
            return con; 
          } 

         //method close the the connection 
         public void closeConnection() { 
            try { 
              con.close(); 
            } catch (SQLException sqle) { 
              System.out.println("Exception in closing
connection"); 
            } 
          } 
} 








--- Bruno Sanguinetti <ke...@sussex.ac.uk> wrote:

> On Wed, 09 Mar 2005 00:28:00 -0000, Deepak Suldhal  
> <dt...@yahoo.com> wrote:
> 
> > Hi
> >   If someone could please help us with a simple
> step by step procedure  
> > of displaying data from
> > a database query (for ex MySQL).
> >   In Tapestry where do we create the connection
> class.
> > I tried to incorporate the standard driver class
> and connection class in  
> > the form.listners method
> > this works once, If I want to access again in a
> new browser (IE) page it  
> > gives Connection already closed error.
> > It would be great to understand tapestry if we are
> able to access data  
> > from tables.
> > If someone would take a little pain and put
> togather this document it  
> > will really benifit.
> > Thanks
> > Deepak
> >
> > 		
> > ---------------------------------
> > Celebrate Yahoo!'s 10th Birthday!
> >  Yahoo! Netrospective: 100 Moments of the Web
> 
> You can find a good tutorial on using a database
> with Tapestry here:
> 
>
http://www.sandcastsoftware.com/articlesandtutorials/brownbag/index.html#tapestry
> 
> They start by implementing a simple DAO pattern,
> which makes it easier to  
> change your Object-Relational mapping. In the second
> part they use  
> hibernate.
> 
> However, if your question is: "where can I start and
> stop a  global object  
> that manages my connections"
> You can do so by extending
> org.apache.tapestry.ApplicationServlet and  
> overriding its init() and destroy() methods.
> 
> Usually I go vith Hibernate and Tomcat's connection
> pool, but I can give  
> you an example that does not use Hibernate (uses
> Berkeley DB). If you use  
> the DAO pattern you can easily swap between your
> persistence engines at a  
> later stage anyway.
> 
> Regards,
> 
> Bruno
> 
> public class ConjunctiServlet extends  
> org.apache.tapestry.ApplicationServlet{
>      public void init() throws ServletException {
>          super.init();
>          System.out.println("Opening databases
> ...");
>          try {
>              BDB.open();
>          } catch (DatabaseException e) {
>              System.out.println("Could not open
> databases.");
>              e.printStackTrace();
>          }
>      }
> 
> 
>      public void destroy() {
>          System.out.println("Closing databases
> ...");
>          BDB.close();
>          super.destroy();
>      }
> }
> 
> where BDB is my Berkeley DB database manager class
> 
> public class BDB {
> 
>      public static Environment env;
>      private static Database _personDB;
> 
>      public static final void open() throws
> DatabaseException {
>          if (env==null){
>              EnvironmentConfig envConfig = new
> EnvironmentConfig();
>              envConfig.setReadOnly(false);
>              envConfig.setAllowCreate(true);
>              envConfig.setTransactional(true);
> 
>              env = new Environment(new
> File("C:\\TMP\\data"), envConfig);
> 
>          }
>          else throw (new DatabaseException("Could
> not open environment"));
>      }
> 
>      public static void close() {
>          if(_personDB  != null) {
>              try {
>                  _personDB.close();
>              } catch (DatabaseException e) {
>                  System.out.println("Could not Close
> database PersonDB" );
>                  e.printStackTrace();
>              }
>          }
> 
>          //close the environment, if it was open
>          if (env!=null){
>              try{
>                  env.close();
>              } catch(DatabaseException e) {
>                  System.err.println("Error closing
> database environment "  
> + e.toString());
>              }
>          }
>      }
> 
>      public static final Database getPersonDB()
> throws DatabaseException {
>          if(env == null)
>              throw new DatabaseException("Attempt to
> open database PersonDB  
> while no environment is configured" );
> 
>          if(_personDB==null){
>              DatabaseConfig dbConfig = new
> DatabaseConfig();
>              dbConfig.setAllowCreate(true);
>              dbConfig.setTransactional(true);
>              _personDB =
> env.openDatabase(null,"PersonDB",dbConfig);
>          }
>          return _personDB;
>      }
> 
>      public static Environment getEnv() {
>          return env;
>      }
> }
> 
> 
> then, In the DAO implementation I just have a static
> ref to it:
> 
> public class PersonDAOBDB implements PersonDAO{
> 
>      static Database personDB = null;
>      TupleBinding _personBinding = new
> PersonBinding();
>      static {
>          try {
>              personDB = BDB.getPersonDB();
> 
>          } catch (DatabaseException e) {
>              e.printStackTrace();
>          }
>      }     // get the personDB
> 
> ...
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> tapestry-user-help@jakarta.apache.org
> 
> 



	
		
__________________________________ 
Celebrate Yahoo!'s 10th Birthday! 
Yahoo! Netrospective: 100 Moments of the Web 
http://birthday.yahoo.com/netrospective/

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Simple database access (mysql)

Posted by Bruno Sanguinetti <ke...@sussex.ac.uk>.
On Wed, 09 Mar 2005 00:28:00 -0000, Deepak Suldhal  
<dt...@yahoo.com> wrote:

> Hi
>   If someone could please help us with a simple step by step procedure  
> of displaying data from
> a database query (for ex MySQL).
>   In Tapestry where do we create the connection class.
> I tried to incorporate the standard driver class and connection class in  
> the form.listners method
> this works once, If I want to access again in a new browser (IE) page it  
> gives Connection already closed error.
> It would be great to understand tapestry if we are able to access data  
> from tables.
> If someone would take a little pain and put togather this document it  
> will really benifit.
> Thanks
> Deepak
>
> 		
> ---------------------------------
> Celebrate Yahoo!'s 10th Birthday!
>  Yahoo! Netrospective: 100 Moments of the Web

You can find a good tutorial on using a database with Tapestry here:

http://www.sandcastsoftware.com/articlesandtutorials/brownbag/index.html#tapestry

They start by implementing a simple DAO pattern, which makes it easier to  
change your Object-Relational mapping. In the second part they use  
hibernate.

However, if your question is: "where can I start and stop a  global object  
that manages my connections"
You can do so by extending org.apache.tapestry.ApplicationServlet and  
overriding its init() and destroy() methods.

Usually I go vith Hibernate and Tomcat's connection pool, but I can give  
you an example that does not use Hibernate (uses Berkeley DB). If you use  
the DAO pattern you can easily swap between your persistence engines at a  
later stage anyway.

Regards,

Bruno

public class ConjunctiServlet extends  
org.apache.tapestry.ApplicationServlet{
     public void init() throws ServletException {
         super.init();
         System.out.println("Opening databases ...");
         try {
             BDB.open();
         } catch (DatabaseException e) {
             System.out.println("Could not open databases.");
             e.printStackTrace();
         }
     }


     public void destroy() {
         System.out.println("Closing databases ...");
         BDB.close();
         super.destroy();
     }
}

where BDB is my Berkeley DB database manager class

public class BDB {

     public static Environment env;
     private static Database _personDB;

     public static final void open() throws DatabaseException {
         if (env==null){
             EnvironmentConfig envConfig = new EnvironmentConfig();
             envConfig.setReadOnly(false);
             envConfig.setAllowCreate(true);
             envConfig.setTransactional(true);

             env = new Environment(new File("C:\\TMP\\data"), envConfig);

         }
         else throw (new DatabaseException("Could not open environment"));
     }

     public static void close() {
         if(_personDB  != null) {
             try {
                 _personDB.close();
             } catch (DatabaseException e) {
                 System.out.println("Could not Close database PersonDB" );
                 e.printStackTrace();
             }
         }

         //close the environment, if it was open
         if (env!=null){
             try{
                 env.close();
             } catch(DatabaseException e) {
                 System.err.println("Error closing database environment "  
+ e.toString());
             }
         }
     }

     public static final Database getPersonDB() throws DatabaseException {
         if(env == null)
             throw new DatabaseException("Attempt to open database PersonDB  
while no environment is configured" );

         if(_personDB==null){
             DatabaseConfig dbConfig = new DatabaseConfig();
             dbConfig.setAllowCreate(true);
             dbConfig.setTransactional(true);
             _personDB = env.openDatabase(null,"PersonDB",dbConfig);
         }
         return _personDB;
     }

     public static Environment getEnv() {
         return env;
     }
}


then, In the DAO implementation I just have a static ref to it:

public class PersonDAOBDB implements PersonDAO{

     static Database personDB = null;
     TupleBinding _personBinding = new PersonBinding();
     static {
         try {
             personDB = BDB.getPersonDB();

         } catch (DatabaseException e) {
             e.printStackTrace();
         }
     }     // get the personDB

...

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


RE: Simple database access (mysql)

Posted by Shawn Church <sh...@boxity.com>.
I agree with Kevin in principle, but I would be hesitant to recommend adding
an ORM into the mix at this point unless you are already comfortable with
one of them.  In answer to your question, if you have a singleton connection
manager (i.e. - a connection pool) or even a global connection object, you
can initialize it in various places, but one example would be in
Engine.setupForRequest().  I would not recommend this sort of design in a
production application, but this will work if you are just trying to get
into Tapestry.

There are a number of examples on the web, but one place you might start is
the Tapestry Wiki at http://wiki.apache.org/jakarta-tapestry/.  Check out
the tutorials and the sample applications.

Shawn

-----Original Message-----
From: Kevin Menard [mailto:nirvdrum@negativetwenty.net]
Sent: Tuesday, March 08, 2005 8:06 PM
To: Tapestry users
Subject: Re: Simple database access (mysql)


On 3/8/05 7:28 PM, "Deepak Suldhal" <dt...@yahoo.com> wrote:

> Hi
>   If someone could please help us with a simple step by step procedure of
> displaying data from
> a database query (for ex MySQL).
>   In Tapestry where do we create the connection class.

At the risk of starting a whole new ORM framework battle, I'd seriously
consider looking at Cayenne or Hibernate or something else.  I know this
isn't a direct answer to your question, but these ORM frameworks make DB
access with Tapestry much nicer.  An ORM framework will provide wrapper
classes for your tables.  Since Tapestry works with properties of objects,
these wrapper classes will make the interaction between Tapestry and your DB
easier (and more intuitive IMHO).

--
Kevin



---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Simple database access (mysql)

Posted by Kevin Menard <ni...@negativetwenty.net>.
On 3/8/05 7:28 PM, "Deepak Suldhal" <dt...@yahoo.com> wrote:

> Hi
>   If someone could please help us with a simple step by step procedure of
> displaying data from
> a database query (for ex MySQL).
>   In Tapestry where do we create the connection class.

At the risk of starting a whole new ORM framework battle, I'd seriously
consider looking at Cayenne or Hibernate or something else.  I know this
isn't a direct answer to your question, but these ORM frameworks make DB
access with Tapestry much nicer.  An ORM framework will provide wrapper
classes for your tables.  Since Tapestry works with properties of objects,
these wrapper classes will make the interaction between Tapestry and your DB
easier (and more intuitive IMHO).

-- 
Kevin



---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org