1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package jdbcadmin.core.tools;
21
22 import java.sql.Connection;
23 import java.sql.SQLException;
24
25 import jdbcadmin.core.data.ConnectionInfo;
26 import jdbcadmin.core.exceptions.TechnicalException;
27
28 /***
29 * Base class for classes which use a database connection<br>
30 * The public methods of the overriding classes <b>must</b> call the {@link #connect} and {@link #diconnect} methods
31 */
32 public abstract class AbstractCnxUser {
33
34
35 /*** Informations de connexion à la base de donnée */
36 protected ConnectionInfo conInfos;
37 /*** Connexion à la base, apres fermeture, elle doit être mise à null */
38 protected Connection cnx;
39
40
41
42 /***
43 * Constructor
44 * @param aCnxInfos Connection informations
45 */
46 public AbstractCnxUser (ConnectionInfo aCnxInfos) {
47 conInfos = aCnxInfos;
48 }
49
50
51
52
53 /***
54 * If not initialized, creates or retrieves the connection
55 * @return <code>true</code> if the connection had to be created/retrieved
56 * (in this case, the calling method <b>must</b> call {@link #disconnect}), <code>false</code> otherwise
57 * @throws TechnicalException error connecting to db.
58 **/
59 protected boolean connect() throws TechnicalException {
60 if (cnx != null) {
61 return false;
62 } else {
63 cnx = ConnectionFactory.getCnx(conInfos);
64 return true;
65 }
66 }
67
68 /***
69 * Closes the db connection
70 **/
71 protected void disconnect() {
72 if (cnx != null) {
73 try {
74 cnx.close();
75 cnx = null;
76 } catch (SQLException sqle) {
77
78 }
79 }
80 }
81
82
83 }