KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > contineo > setup > DBCon


1 /*
2  * DBCon.java
3  *
4  * Created on 21. January 2004, 00:45
5  */

6
7 package org.contineo.setup;
8
9 import java.sql.Connection JavaDoc;
10 import java.sql.DriverManager JavaDoc;
11 import java.sql.ResultSet JavaDoc;
12
13 import org.apache.log4j.Level;
14 import org.apache.log4j.Logger;
15 import org.contineo.core.LoggingManager;
16 import org.contineo.core.config.RepositoryConfigurator;
17
18 /**
19  *
20  * @author Michael Scholz
21  * @version 1.0
22  *
23  * This class is for connecting a database using the variables url, username, passsword.
24  */

25 public class DBCon {
26     
27     private java.sql.Statement JavaDoc stmt = null;
28     private Connection JavaDoc dbcon;
29     private Logger logger;
30
31     /**
32      *
33      * @uml.property name="conf"
34      * @uml.associationEnd
35      * @uml.property name="conf" multiplicity="(1 1)"
36      */

37     private RepositoryConfigurator conf;
38
39     
40     /** Creates new DBCon */
41     public DBCon() {
42         logger = LoggingManager.getLogger(this.getClass());
43         conf = new RepositoryConfigurator();
44     }
45
46     protected void doConnection() {
47         try {
48             Class.forName(conf.getAttribute("contineo", "driver"));
49             String JavaDoc url = conf.getAttribute("contineo", "protocol") + ":";
50             url += conf.getAttribute("contineo", "subprotocol") + ":";
51             url += conf.getAttribute("contineo", "dbalias");
52             String JavaDoc username = conf.getAttribute("contineo", "username");
53             String JavaDoc password = conf.getAttribute("contineo", "password");
54             dbcon = DriverManager.getConnection(url, username, password);
55             dbcon.setAutoCommit(true);
56             stmt = dbcon.createStatement();
57         }
58         catch (Exception JavaDoc ex) {
59             if (logger.isEnabledFor(Level.ERROR))
60                 logger.error(ex.getMessage());
61         }
62     }
63     
64     protected void rollback() {
65         try {
66             dbcon.rollback();
67         }
68         catch (Exception JavaDoc ex) {
69             if (logger.isEnabledFor(Level.ERROR))
70                 logger.error("can't rollback");
71         }
72     }
73     
74     /**
75      * This method returns the state of the connection.
76      */

77     public boolean isConnected() {
78         try {
79             return !dbcon.isClosed();
80         }
81         catch (Exception JavaDoc ex) {
82             if (logger.isEnabledFor(Level.DEBUG))
83                 logger.debug("db-connection is open:" + ex.getMessage());
84             return false;
85         }
86     }
87  
88     /**
89      * This method executes select-statements.
90      * @param query Statement to execute.
91      * @return Execution result as ResultSet.
92      */

93     public ResultSet JavaDoc executeQuery(String JavaDoc query) {
94         ResultSet JavaDoc rs = null;
95         try {
96             if (!isConnected())
97                 this.doConnection();
98             rs = stmt.executeQuery(query);
99         }
100         catch (Exception JavaDoc ex) {
101             if (logger.isEnabledFor(Level.ERROR))
102                 logger.error(ex.getMessage());
103         }
104         return rs;
105     }
106  
107     /**
108      * This method executes manipulation-statements.
109      * @param query Statement to execute.
110      * @return Number of succeded executions.
111      */

112     public int executeUpdate(String JavaDoc query) throws UpdateException {
113         int result = -1;
114         try {
115             if (!isConnected())
116                 this.doConnection();
117             result = stmt.executeUpdate(query);
118             //dbcon.commit();
119
}
120         catch (Exception JavaDoc ex) {
121             if (logger.isEnabledFor(Level.ERROR))
122                 logger.error(ex.getMessage());
123             throw new UpdateException(query + " failed.");
124             //rollback();
125
}
126         return result;
127     }
128     
129     /**
130      * This method tests a connection.
131      */

132     public boolean testConnection() {
133         boolean result = true;
134         try {
135             Class.forName(conf.getAttribute("contineo", "driver"));
136             String JavaDoc url = conf.getAttribute("contineo", "protocol") + ":";
137             url += conf.getAttribute("contineo", "subprotocol") + ":";
138             url += conf.getAttribute("contineo", "dbalias");
139             String JavaDoc username = conf.getAttribute("contineo", "username");
140             String JavaDoc password = conf.getAttribute("contineo", "password");
141             dbcon = DriverManager.getConnection(url, username, password);
142             result = true;
143             dbcon.close();
144         }
145         catch (Exception JavaDoc ex) {
146             if (logger.isEnabledFor(Level.ERROR))
147                 logger.error(ex.getMessage());
148             result = false;
149         }
150         return result;
151     }
152 }
153
Popular Tags