svn-gvsig-desktop / tags / J2ME_compat_v1_2_Build_1209 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / db / utils / ConnectionWithParams.java @ 19509
History | View | Annotate | Download (4.33 KB)
1 |
package com.iver.cit.gvsig.fmap.drivers.db.utils; |
---|---|
2 |
|
3 |
import java.sql.SQLException; |
4 |
|
5 |
import org.apache.log4j.Logger; |
6 |
|
7 |
import com.iver.cit.gvsig.fmap.drivers.ConnectionFactory; |
8 |
import com.iver.cit.gvsig.fmap.drivers.DBException; |
9 |
import com.iver.cit.gvsig.fmap.drivers.IConnection; |
10 |
|
11 |
/**
|
12 |
* Utility class to keep the connection parameters. It is used as a item in the
|
13 |
* single connections manager tree and in the available connections combo box
|
14 |
* (wizard db)
|
15 |
*
|
16 |
* @author jldominguez
|
17 |
*
|
18 |
*/
|
19 |
|
20 |
public class ConnectionWithParams { |
21 |
|
22 |
private static Logger logger = Logger.getLogger(ConnectionWithParams.class.getName()); |
23 |
|
24 |
private IConnection conn = null; |
25 |
private String connectionStr = ""; |
26 |
private String drvName = ""; |
27 |
private String user = ""; |
28 |
private String pw = ""; |
29 |
private String name = ""; |
30 |
private boolean connected = false; |
31 |
|
32 |
private String host; |
33 |
private String port; |
34 |
private String db; |
35 |
|
36 |
private String schema; |
37 |
|
38 |
private boolean isNull = false; |
39 |
|
40 |
|
41 |
/**
|
42 |
* Utility constructor to indicate an empty item.
|
43 |
* It is used as the first item in the wizard's combo box
|
44 |
* so that when the wizard is loaded, no query is done to any database.
|
45 |
*
|
46 |
*/
|
47 |
public ConnectionWithParams() {
|
48 |
isNull = true;
|
49 |
} |
50 |
|
51 |
/**
|
52 |
* Class Constructor.
|
53 |
*
|
54 |
* @param _conn_str connection string
|
55 |
* @param _c connection object
|
56 |
* @param _drvName driver name
|
57 |
* @param _user user name
|
58 |
* @param _pw password
|
59 |
* @param _name connection name (freely chosen by user)
|
60 |
* @param _host host's url
|
61 |
* @param _port port number as a string
|
62 |
* @param _db database name
|
63 |
* @param _isConn whether the connection is open or not
|
64 |
*/
|
65 |
public ConnectionWithParams(
|
66 |
String _conn_str,
|
67 |
IConnection _c, |
68 |
String _drvName,
|
69 |
String _user,
|
70 |
String _pw,
|
71 |
String _name,
|
72 |
String _host,
|
73 |
String _port,
|
74 |
String _db,
|
75 |
boolean _isConn) {
|
76 |
|
77 |
connectionStr = _conn_str; |
78 |
connected = _isConn; |
79 |
conn = _c; |
80 |
drvName = _drvName; |
81 |
user = _user; |
82 |
pw = _pw; |
83 |
name = _name; |
84 |
|
85 |
host = _host; |
86 |
port = _port; |
87 |
db = _db; |
88 |
|
89 |
if (!connected) {
|
90 |
pw = null;
|
91 |
conn = null;
|
92 |
} |
93 |
} |
94 |
|
95 |
public IConnection getConnection() {
|
96 |
return conn;
|
97 |
} |
98 |
|
99 |
public String getDrvName() { |
100 |
return drvName;
|
101 |
} |
102 |
|
103 |
public String getPw() { |
104 |
return pw;
|
105 |
} |
106 |
|
107 |
public String getUser() { |
108 |
return user;
|
109 |
} |
110 |
|
111 |
|
112 |
/**
|
113 |
* Used to paint the object in lists and trees
|
114 |
*/
|
115 |
public String toString() { |
116 |
|
117 |
if (isNull) {
|
118 |
return ""; |
119 |
} |
120 |
|
121 |
if (connected) {
|
122 |
return "[C] " + name + " (" + drvName + ")"; |
123 |
} else {
|
124 |
return name + " (" + drvName + ")"; |
125 |
} |
126 |
} |
127 |
|
128 |
public boolean isConnected() { |
129 |
return connected;
|
130 |
} |
131 |
|
132 |
public void setConnected(boolean c) { |
133 |
connected = c; |
134 |
} |
135 |
|
136 |
/**
|
137 |
* Tries to connects the connection object with the given password.
|
138 |
* @param _pw password
|
139 |
* @throws SQLException
|
140 |
*/
|
141 |
public void connect(String _pw) throws DBException { |
142 |
|
143 |
try {
|
144 |
conn = ConnectionFactory.createConnection(connectionStr, user, _pw);//DriverManager.getConnection(connectionStr, user, _pw);
|
145 |
} catch (DBException e) {
|
146 |
|
147 |
pw = null;
|
148 |
conn = null;
|
149 |
connected = false;
|
150 |
|
151 |
throw new DBException(e); |
152 |
} |
153 |
|
154 |
pw = _pw; |
155 |
connected = true;
|
156 |
} |
157 |
|
158 |
/**
|
159 |
* Disconnects the connection
|
160 |
*
|
161 |
*/
|
162 |
public void disconnect() { |
163 |
|
164 |
try {
|
165 |
conn.close(); |
166 |
} catch (DBException e) {
|
167 |
logger.error("While closing connection: " + e.getMessage(), e);
|
168 |
} |
169 |
pw = null;
|
170 |
conn = null;
|
171 |
connected = false;
|
172 |
} |
173 |
|
174 |
|
175 |
public String getConnectionStr() { |
176 |
return connectionStr;
|
177 |
} |
178 |
|
179 |
public void setConnectionStr(String connectionStr) { |
180 |
this.connectionStr = connectionStr;
|
181 |
} |
182 |
|
183 |
public String getName() { |
184 |
return name;
|
185 |
} |
186 |
|
187 |
public void setName(String name) { |
188 |
this.name = name;
|
189 |
} |
190 |
|
191 |
public String getDb() { |
192 |
return db;
|
193 |
} |
194 |
|
195 |
public void setDb(String db) { |
196 |
this.db = db;
|
197 |
} |
198 |
|
199 |
public String getHost() { |
200 |
return host;
|
201 |
} |
202 |
|
203 |
public void setHost(String host) { |
204 |
this.host = host;
|
205 |
} |
206 |
|
207 |
public String getPort() { |
208 |
return port;
|
209 |
} |
210 |
|
211 |
public void setPort(String port) { |
212 |
this.port = port;
|
213 |
} |
214 |
|
215 |
/**
|
216 |
*
|
217 |
* @return whether or not this is the first item in the combo box.
|
218 |
*/
|
219 |
public boolean isNull() { |
220 |
return isNull;
|
221 |
} |
222 |
|
223 |
public String getSchema() { |
224 |
return schema;
|
225 |
} |
226 |
|
227 |
public void setSchema(String schema) { |
228 |
this.schema = schema;
|
229 |
} |
230 |
|
231 |
} |