I’m just getting my head around java (and OOP for that matter), the only thing I am familiar with is MySQL. I need to keep the DB connection open throughout the duration of the application, as well as a server socket.
I’m not even sure if they both need separate classes, but here’s what I have so far:
http://pastebin.com/qzMFFTrY
(it wouldn’t all go in a code tag)
The variable I need is con for line 86.
Why not instantiate
DoCommswith the connection you’ve got earlier ?e.g. line 44 would be:
and
DoCommswould hold a reference to that connection and then use it at line 86.Note that you get the connection, and then close it in the
finallyblock before you instantiate yourDoCommsobjects (line 28). So you should close your connection once you’ve finished processing everything. Briefly:If your application is long-lived, then I would use connection-pooling (e.g. C3P0 or Apache DBCP) and open/close connections as required. However your approach may well be suitable for your requirements, and I wouldn’t worry about that for the moment.