You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Cesar <ce...@uol.com.br> on 2004/07/02 13:41:14 UTC

PreparedStatement and ResultSet

Hi People

I ´m using the Connection Object of OJB in some parts of my program...and I
´m having problems with my connections.....I know that i can´t close the
connection object because it is work of  PersistenceBroker, but about
PreparedStatement and ResultSet ? Can I close the PreparedStatement and
ResultSet?


                PersistenceBroker broker                = null;
                PreparedStatement pstmt                 = null;
                ResultSet rs                            = null;
                Connection conn                         = null;
                ConnectionManagerIF connectionManager   = null;
                String blog;

                try
                {
                    broker =
getPersistenceBroker().createPersistenceBroker(getPersistenceBroker().
getDefaultKey());
                    connectionManager =  broker.serviceConnectionManager();
                    conn = connectionManager.getConnection();
                    pstmt = conn.prepareStatement(Queries.SELECT_TOP_VOTE);
                    pstmt.setInt(1,
adminUserConfiguration.getVoteTotalPoints());
                    pstmt.setInt(2,
adminUserConfiguration.getQtdTopLeafs());
                    rs = pstmt.executeQuery();
                    while (rs.next())
                    {
                        topVoteBlogs.add(rs.getString(2));
                        topVoteUrls.add(rs.getString(3));
                    }
                     this.setTopVote(topVoteBlogs,topVoteUrls);
                }
                catch (Exception e)
                {
                    logger.error(e);
                }
                finally
                {
                    try
                    {
                        if (rs!=null)
                        {
                            rs.close();
                            rs = null;
                        }

                        if (pstmt!=null)
                        {
                            pstmt.close();
                            pstmt = null;
                        }
                        broker.close();
                    }
                    catch (Exception e)
                    {
                              logger.error(e);
                    }
                }


Thanks.


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


Re: PreparedStatement and ResultSet

Posted by Armin Waibel <ar...@apache.org>.
Hi Cesar,

Cesar wrote:

> Hi People
> 
> I ´m using the Connection Object of OJB in some parts of my program...and I
> ´m having problems with my connections.....I know that i can´t close the
> connection object because it is work of  PersistenceBroker, but about
> PreparedStatement and ResultSet ? Can I close the PreparedStatement and
> ResultSet?
>

yes, you have to close created pstmt and rs by your own. Sorry this 
isn't clear in documentation. I will fix this in docs.
http://db.apache.org/ojb/docu/guides/connection.html#obtain-connection

regards,
Armin

> 
>                 PersistenceBroker broker                = null;
>                 PreparedStatement pstmt                 = null;
>                 ResultSet rs                            = null;
>                 Connection conn                         = null;
>                 ConnectionManagerIF connectionManager   = null;
>                 String blog;
> 
>                 try
>                 {
>                     broker =
> getPersistenceBroker().createPersistenceBroker(getPersistenceBroker().
> getDefaultKey());
>                     connectionManager =  broker.serviceConnectionManager();
>                     conn = connectionManager.getConnection();
>                     pstmt = conn.prepareStatement(Queries.SELECT_TOP_VOTE);
>                     pstmt.setInt(1,
> adminUserConfiguration.getVoteTotalPoints());
>                     pstmt.setInt(2,
> adminUserConfiguration.getQtdTopLeafs());
>                     rs = pstmt.executeQuery();
>                     while (rs.next())
>                     {
>                         topVoteBlogs.add(rs.getString(2));
>                         topVoteUrls.add(rs.getString(3));
>                     }
>                      this.setTopVote(topVoteBlogs,topVoteUrls);
>                 }
>                 catch (Exception e)
>                 {
>                     logger.error(e);
>                 }
>                 finally
>                 {
>                     try
>                     {
>                         if (rs!=null)
>                         {
>                             rs.close();
>                             rs = null;
>                         }
> 
>                         if (pstmt!=null)
>                         {
>                             pstmt.close();
>                             pstmt = null;
>                         }
>                         broker.close();
>                     }
>                     catch (Exception e)
>                     {
>                               logger.error(e);
>                     }
>                 }
> 
> 
> Thanks.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
> 
> 
> 

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