Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeder2.ecngs.de!ecngs!feeder.ecngs.de!Xl.tags.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!local2.nntp.ams.giganews.com!nntp.bt.com!news.bt.com.POSTED!not-for-mail NNTP-Posting-Date: Tue, 19 Feb 2013 03:18:55 -0600 Date: Tue, 19 Feb 2013 09:18:53 +0000 From: lipska the kat <"nospam at neversurrender dot co dot uk"> Organization: Trollbusters 3 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:11.0) Gecko/20120410 Thunderbird/11.0.1 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: how to access connection object in another class? References: <9345656c-d843-4e20-a49a-df3b3fd7dfe5@googlegroups.com> In-Reply-To: <9345656c-d843-4e20-a49a-df3b3fd7dfe5@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit Message-ID: Lines: 55 X-Usenet-Provider: http://www.giganews.com X-AuthenticatedUsername: NoAuthUser X-Trace: sv3-sNCnZlolnOsb4XO3jgRBPSP3gdR853bA53SWwkAlpyfixcjFL21I8KY/kzUHCCL65lmQWFDBlGdREvM!9NSXBQqXBGq5jhQ5/bolla4y1yPhmbkxTHGrFOwAIeL4to9JKOpFNBOhGYPmplY99X7JejDA0lY= X-Complaints-To: abuse@btinternet.com X-DMCA-Complaints-To: abuse@btinternet.com X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2445 Xref: csiph.com comp.lang.java.programmer:22358 On 19/02/13 08:18, xodepp shrestha wrote: > Here is the source code. > This is the code to connect the database. > > package stundentrecord; > > import java.sql.Connection; > import java.sql.DriverManager; > I think you probably need something like this public class ConnectionManager{ public Connection getNewConnection() throws ...{ Connection conn = null; //get your connection then return it ... return conn; } public void closeConnection(connection conn) throws ...{ //manage the close connection process ... } } now you can do as you wish e.g class SomeClass{ public SomeType someMethod(){ //should really be a singleton but what the heck ConnectionManager connections = new Connectionmanager(); Connection conn = connections.getNewConnection(); ... //use the connection ... //close it ... connections.closeConnection(conn); ... // etc } } HTH lipska -- Lipska the KatŠ: Troll hunter, sandbox destroyer and farscape dreamer of Aeryn Sun