A general rule of thumb in object oriented programming is that you should try to avoid writing code that does the same thing in more than one class. If you find yourself implementing similar functionality in two different classes, try to find a way to generalize the process and encapsulate the functionality in a single class.
> Hey. > I have made some servlets that is connected to a database. these > servlets are quite similar in funtion. so i thaught: > Why don't i write a class thats connected to the database and then > lett my servlets use that class to fetch data. that way i'm only > using 1 connection to the database and i dont have to write versions > of the same method in the each and every one of the servlets. A couple of points here. First, you should have more than one Connection. In fact, it would be to your benefit to create a pool of Connection objects that your servlets will use when they need to connect to the database. It's best not to have more than one thread using a Connection object at the same time. Connection pools are very common and you should search the archives of this list to find out more about them - http://archives.java.sun.com/archives/servlet-interest.html. You may also benefit by creating a class that abstracts the use of the Connection object. Such ha class could have a very simple interface to the user (your servlets) taking a query string as an argument and returning a result set, it's up to you. > > > In the database-connected class the connection to the database is > static > > Question one: > is this not a good idea? and in that case, why? > > Question two: > When i try to instantiate my database-connected class i get a "cannot > resolve symbol" error at combile. > The class that i'm trying to instantiate are located in the same > directory, and the servlet is working fine without it. > Where shall i put it? > I've tried the lib dir, no luck. > > Thankful for any help! > > Thanks in advance > Bj�rn Alexandersson > --------------------------------------------------------------------- > Message written: 2002-04-26 kl. 21:24:24 > Email: [EMAIL PROTECTED] > Telephone: +46(0)611-550 771 > Cell: +46(0)70-659 93 17 > Facs: +46(0)611-550 774 > > ___________________________________________________________________________ > To unsubscribe, send email to [EMAIL PROTECTED] and include in the body > of the message "signoff SERVLET-INTEREST". > > Archives: http://archives.java.sun.com/archives/servlet-interest.html > Resources: http://java.sun.com/products/servlet/external-resources.html > LISTSERV Help: http://www.lsoft.com/manuals/user/user.html ___________________________________________________________________________ To unsubscribe, send email to [EMAIL PROTECTED] and include in the body of the message "signoff SERVLET-INTEREST". Archives: http://archives.java.sun.com/archives/servlet-interest.html Resources: http://java.sun.com/products/servlet/external-resources.html LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
