Solved

JSP db Connection design question

Posted on 2004-10-06
7
300 Views
Last Modified: 2010-08-05
Hi,

I am planning on using just JSP and Struts for my application that has an Oracle backend. I don't want to use JavaBean orDAOs because of the short timeframe. I want to know the best way to implement the database connections logic. Should I have  a database utilitly class that has getConnection and close connection methods and call this class directly in JSP when ever there is need to query or update? The appliction will run on Weblogic so I have to configure the datasource in weblogic.

Thanks in advance
Dasari
0
Comment
Question by:DASARI_R
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 12236352
> Should I have  a database utilitly class that has getConnection and close connection methods and call this class directly in JSP when ever there is need to query or update?

Yes :-)
0
 

Author Comment

by:DASARI_R
ID: 12236503
Can you please give me some design ideas or an example ie where to use static method or a singleton class

class DBMgr()
{
Connection conn;    
public static Connection getConnection()
{
 // do init
return conn;
}

public static void close()
{
if (null != conn)
  conn.close();
}

}
DBMgr.getConnection()
DBMgr.close()

If I use this in JSPs will there be a synchonization issue or will every client get a new connection ?

Thanks
Vinay
0
 
LVL 3

Expert Comment

by:applekanna
ID: 12243621
Hi DASARI_R,
http://java.sun.com/developer/onlineTraining/Programming/JDCBook/conpool.html

Just do "connection pooling java example" in google and you will find all that you want

Cheers!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Accepted Solution

by:
kokchoon78 earned 125 total points
ID: 12257056
I think struts provides a simple Database Pooling..Here is how i implement :

In my struts action class, i get the datasource object with :

DataSource dataSource = getDataSource(request);

and then pass the datasource object to my Database Manager class.

For example :

PageDA pageDA = new PageDA();
pageDA.deletePage( dataSource, pageId );

in th PageDA class :

public boolean deletePage( DataSource dataSource , int pageId)
{
   Connection myConnection = null;
   PreparedStatement pstmt = null;
   boolean returnB = false;

   try
   {
     myConnection = dataSource.getConnection();

     pstmt = myConnection.prepareStatement("delete from PAGE where PAGE_ID=?");
     pstmt.setInt( 1, pageId );
    if( pstmt.executeUpdate() == 1 )
    {
       returnB = true;
     }

     if( !myConnection.getAutoCommit() )
     {
       myConnection.commit();
     }
   }
   catch (SQLException sqle)
   {
     try
       {
         myConnection.rollback();
        }catch(Exception e)
       {
          e.printStackTrace();
       }
       sqle.printStackTrace();
    }
    finally
    {
       try
       {
           if( pstmt != null ) pstmt.close();
           if( myConnection != null ) myConnection.close();
        }
       catch (SQLException e)
       {
           e.printStackTrace();
       }
    }
return ( returnB );
}

hth,
Kok Choon.

0
 
LVL 35

Expert Comment

by:TimYates
ID: 12502483
I think it should be:

Split:  applekanna {http:#12243621} & kokchoon78 {http:#12257056}

:-)
0
 

Author Comment

by:DASARI_R
ID: 12503997
Kok Choon,

Thank you for the answer.

Dasari
 
 
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Primefaces: How to display a variable that can take only 'Y' or 'N' or a boolean value. 2 128
Lamba in java 8 9 261
main method of string class 3 124
junit example issue 2 58
This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
We asked our MSP customer base what their favorite tools were and how they help them serve clients. We focused our questions on favorite tools in the following categories: >PSA tools >RMM tools >Alert management tools >Communication tools and Mo…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question