Revision 1091
Added by Jing Tao over 22 years ago
src/edu/ucsb/nceas/metacat/DBConnection.java | ||
---|---|---|
36 | 36 |
* index, status, age, createtime, connection time, usageCount, warning message |
37 | 37 |
*/ |
38 | 38 |
|
39 |
public class DBConnection
|
|
39 |
public class DBConnection
|
|
40 | 40 |
{ |
41 | 41 |
private Connection conn; |
42 | 42 |
private String tag;//to idenify this object |
... | ... | |
50 | 50 |
private SQLWarning warningMessage; |
51 | 51 |
|
52 | 52 |
|
53 |
private String DBDriver=MetaCatUtil.getOption("dbDriver"); |
|
54 |
private String DBConnectedJDBC=MetaCatUtil.getOption("defaultDB"); |
|
55 |
private String userName=MetaCatUtil.getOption("user"); |
|
56 |
private String passWord=MetaCatUtil.getOption("password"); |
|
53 |
private static String DBDriver=MetaCatUtil.getOption("dbDriver");
|
|
54 |
private static String DBConnectedJDBC=MetaCatUtil.getOption("defaultDB");
|
|
55 |
private static String userName=MetaCatUtil.getOption("user");
|
|
56 |
private static String passWord=MetaCatUtil.getOption("password");
|
|
57 | 57 |
|
58 | 58 |
/** |
59 | 59 |
* Default constructor of the DBConnection class |
60 |
* It will create a new DBConnection with a new Connection |
|
61 | 60 |
* |
62 | 61 |
*/ |
63 |
public DBConnection() throws SQLException |
|
62 |
public DBConnection() throws SQLException
|
|
64 | 63 |
{ |
65 | 64 |
conn = openConnection(); |
66 | 65 |
tag = conn.toString(); |
... | ... | |
74 | 73 |
|
75 | 74 |
} |
76 | 75 |
|
77 |
/** |
|
78 |
* construct an instance of the DBQConnection class |
|
79 |
* |
|
80 |
* @param conn, the JDBC connection itself. |
|
81 |
* @param status, the status of this connection, free or using |
|
82 |
* @param age,the age of the connection |
|
83 |
* @param usageCount of the connection |
|
84 |
*/ |
|
85 |
public DBConnection (Connection conn, int status, long age, |
|
86 |
long createTime, long connectionTime, int usageCount) |
|
87 |
throws SQLException |
|
88 |
{ |
|
89 |
this.conn = conn; |
|
90 |
this.tag = conn.toString(); |
|
91 |
this.status = status; |
|
92 |
this.age = age; |
|
93 |
this.createTime = createTime; |
|
94 |
this.connectionTime = connectionTime; |
|
95 |
this.usageCount = usageCount; |
|
96 |
try |
|
97 |
{ |
|
98 |
this.warningMessage = conn.getWarnings(); |
|
99 |
} |
|
100 |
catch (SQLException e) |
|
101 |
{ |
|
102 |
throw e; |
|
103 |
} |
|
104 |
} |
|
76 |
|
|
105 | 77 |
|
106 | 78 |
/** |
107 | 79 |
* get the connetion from the object |
108 | 80 |
*/ |
109 |
public Connection getConnection() |
|
81 |
public Connection getConnections()
|
|
110 | 82 |
{ |
111 | 83 |
return conn; |
112 | 84 |
} |
... | ... | |
115 | 87 |
* Set a connection to this object |
116 | 88 |
* @param myDBConnection, the connection which will be assign to this object |
117 | 89 |
*/ |
118 |
public void setConnection( Connection myConnection) |
|
90 |
public void setConnections( Connection myConnection)
|
|
119 | 91 |
{ |
120 | 92 |
this.conn = myConnection; |
121 | 93 |
} |
... | ... | |
261 | 233 |
/** |
262 | 234 |
* get the db connetion waring message from the object |
263 | 235 |
*/ |
264 |
public SQLWarning getWarningMessage() |
|
236 |
public SQLWarning getWarningMessage() throws SQLException
|
|
265 | 237 |
{ |
266 |
return warningMessage;
|
|
238 |
return conn.getWarnings();
|
|
267 | 239 |
} |
268 | 240 |
|
269 | 241 |
/** |
... | ... | |
281 | 253 |
public void close() throws SQLException |
282 | 254 |
{ |
283 | 255 |
conn.close(); |
284 |
conn=null; |
|
285 | 256 |
tag = null; |
286 | 257 |
status = 0; |
287 | 258 |
age = 0; |
... | ... | |
293 | 264 |
} |
294 | 265 |
|
295 | 266 |
/** |
296 |
* Method to establish a JDBC database connection
|
|
267 |
* Method to establish DBConnection
|
|
297 | 268 |
*/ |
298 |
private Connection openConnection()
|
|
269 |
public static Connection openConnection()
|
|
299 | 270 |
throws SQLException |
300 | 271 |
{ |
301 | 272 |
return openConnection(DBDriver, DBConnectedJDBC, userName, passWord); |
... | ... | |
309 | 280 |
* @param user name of the user to use for database connection |
310 | 281 |
* @param password password for the user to use for database connection |
311 | 282 |
*/ |
312 |
private Connection openConnection(String dbDriver, String connection, |
|
283 |
private static Connection openConnection(String dbDriver, String connection,
|
|
313 | 284 |
String user, String password) |
314 | 285 |
throws SQLException |
315 | 286 |
{ |
316 |
|
|
317 | 287 |
// Load the Oracle JDBC driver |
318 | 288 |
try |
319 | 289 |
{ |
... | ... | |
325 | 295 |
return null; |
326 | 296 |
} |
327 | 297 |
// Connect to the database |
328 |
Connection conn = DriverManager.getConnection( connection, user, password); |
|
329 |
return conn; |
|
298 |
Connection connLocal = null; |
|
299 |
connLocal = DriverManager.getConnection( connection, user, password); |
|
300 |
return connLocal; |
|
330 | 301 |
}//OpenDBConnection |
302 |
|
|
303 |
/** |
|
304 |
* Method to create a PreparedStatement by sending a sql statement |
|
305 |
* @Param sql, the sql statement which will be sent to db |
|
306 |
*/ |
|
307 |
public PreparedStatement prepareStatement( String sql ) throws SQLException |
|
308 |
{ |
|
309 |
return conn.prepareStatement(sql); |
|
310 |
}//prepareStatement |
|
311 |
|
|
331 | 312 |
|
313 |
|
|
332 | 314 |
|
333 | 315 |
}//DBConnection class |
Also available in: Unified diff
Add some new methods in this class.