1 **
2 *数据库连接类,连接数据库3 *@author Damon4 */
5 public class DBConn implementsConnection6 {7
8 //获取JdbcUrl信息
9 privateJdbcUrl JUrl;10
11 //数据库连接
12 private Connection con = null;13
14 //连接是否已使用
15 private booleanbNotInUse;16
17 private CharArrayWriter m_buf = newCharArrayWriter();18
19 private PrintWriter m_pw = new PrintWriter(m_buf, true);20
21 //默认连接
22 publicDBConn()23 {24 //TODO Auto-generated constructor stub
25 this.JUrl = newJdbcUrl();26 }27
28 //指定数据库连接
29 publicDBConn(String urlType)30 {31 this.JUrl = newJdbcUrl(urlType);32 }33
34 //创建连接
35 public booleancreateConnection()36 {37
38 //根据数据库类型加载驱动及连接
39 try
40 {41 //连接MySQL数据库
42 if(SysCon.DATABASE_TYPE_MYSQL.equals(JUrl.getDBType()))43 {44 //加载数据库驱动
45 Class.forName("com.mysql.jdbc.Driver");46
47 //尝试连接数据库
48 con =DriverManager.getConnection(JUrl.getJdbcUrl(), JUrl.getUserName(), JUrl.getPassWord());49 }50 //其他数据库类型判断及处理51 //SQLSERVER
52 else if(SysCon.DATABASE_TYPE_SQLSERVER.equals(JUrl.getDBType()))53 {54 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");55 con =DriverManager.getConnection(JUrl.getJdbcUrl(), JUrl.getUserName(), JUrl.getPassWord());56 }57 //DB2
58 else if(SysCon.DATABASE_TYPE_DB2.equals(JUrl.getDBType()))59 {60 Class.forName("com.ibm.db2.jcc.DB2Driver");61 con =DriverManager.getConnection(JUrl.getJdbcUrl(), JUrl.getUserName(), JUrl.getPassWord());62 }63 //ORACLE
64 else if(SysCon.DATABASE_TYPE_ORACLE.equals(JUrl.getDBType()))65 {66 Class.forName("oracle.jdbc.driver.OracleDriver");67 //一个是缓存取到的记录数,一个是设置默认的批量提交数
68 Properties props = newProperties();69 props.setProperty("user", JUrl.getUserName());70 props.setProperty("password", JUrl.getPassWord());71 props.setProperty("defaultRowPrefetch", "50");72 props.setProperty("defaultExecuteBatch", "50");73 con =DriverManager.getConnection(JUrl.getJdbcUrl(), props);74 }75 else
76 {77 System.out.println("未匹配到数据库类型!");78 return false;79 }80
81 }82 catch(ClassNotFoundException e)83 {84 //TODO Auto-generated catch block
85 System.out.println("加载驱动失败!");86 e.printStackTrace();87 return false;88 }89 catch(SQLException e)90 {91 //TODO Auto-generated catch block
92 System.out.println("创建连接失败..." +e.getMessage());93 e.printStackTrace();94 return false;95 }96 return true;97 }98
99 protected voidsetInUse()100 {101 /**
102 * Record stack information when each connection is get We reassian103 * System.err, so Thread.currentThread().dumpStack() can dump stack info104 * into our class FilterPrintStream.105 */
106 newThrowable().printStackTrace(m_pw);107
108 bNotInUse = false;109
110 /**
111 * record lastest access time112 */
113 }114
115 /*下面都是 实现Connection的方法,返回conn的实现*/
116 public T unwrap(Class iface) throwsSQLException117 {118 //TODO Auto-generated method stub
119 return con.unwrap(null);120 }121
122 public boolean isWrapperFor(Class> iface) throwsSQLException123 {124 //TODO Auto-generated method stub
125 return false;126 }127
128 public Statement createStatement() throwsSQLException129 {130 //TODO Auto-generated method stub
131 returncon.createStatement();132 }133
134 public PreparedStatement prepareStatement(String sql) throwsSQLException135 {136 //TODO Auto-generated method stub
137 returncon.prepareStatement(sql);138 }139
140 public CallableStatement prepareCall(String sql) throwsSQLException141 {142 //TODO Auto-generated method stub
143 returncon.prepareCall(sql);144 }145
146 public String nativeSQL(String sql) throwsSQLException147 {148 //TODO Auto-generated method stub
149 returncon.nativeSQL(sql);150 }151
152 public void setAutoCommit(boolean autoCommit) throwsSQLException153 {154 //TODO Auto-generated method stub
155 con.setAutoCommit(autoCommit);156 }157
158 public boolean getAutoCommit() throwsSQLException159 {160 //TODO Auto-generated method stub
161 returncon.getAutoCommit();162 }163
164 public void commit() throwsSQLException165 {166 //TODO Auto-generated method stub
167 con.commit();168 }169
170 public void rollback() throwsSQLException171 {172 //TODO Auto-generated method stub
173 con.rollback();174 }175
176 public void close() throwsSQLException177 {178 //TODO Auto-generated method stub
179 con.close();180 }181
182 public boolean isClosed() throwsSQLException183 {184 //TODO Auto-generated method stub
185
186 returncon.isClosed();187 }188
189 public DatabaseMetaData getMetaData() throwsSQLException190 {191 //TODO Auto-generated method stub
192 returncon.getMetaData();193 }194
195 public void setReadOnly(boolean readOnly) throwsSQLException196 {197 //TODO Auto-generated method stub
198 con.setReadOnly(readOnly);199 }200
201 public boolean isReadOnly() throwsSQLException202 {203 //TODO Auto-generated method stub
204 returncon.isReadOnly();205 }206
207 public void setCatalog(String catalog) throwsSQLException208 {209 //TODO Auto-generated method stub
210 con.setCatalog(catalog);211 }212
213 public String getCatalog() throwsSQLException214 {215 //TODO Auto-generated method stub
216 returncon.getCatalog();217 }218
219 public void setTransactionIsolation(int level) throwsSQLException220 {221 //TODO Auto-generated method stub
222 con.setTransactionIsolation(level);223 }224
225 public int getTransactionIsolation() throwsSQLException226 {227 //TODO Auto-generated method stub
228 returncon.getTransactionIsolation();229 }230
231 public SQLWarning getWarnings() throwsSQLException232 {233 //TODO Auto-generated method stub
234 returncon.getWarnings();235 }236
237 public void clearWarnings() throwsSQLException238 {239 //TODO Auto-generated method stub
240 con.clearWarnings();241 }242
243 public Statement createStatement(int resultSetType, int resultSetConcurrency) throwsSQLException244 {245 //TODO Auto-generated method stub
246 returncon.createStatement(resultSetType, resultSetConcurrency);247 }248
249 public PreparedStatement prepareStatement(String sql, int resultSetType, intresultSetConcurrency)250 throwsSQLException251 {252 //TODO Auto-generated method stub
253 returncon.prepareStatement(sql, resultSetType, resultSetConcurrency);254 }255
256 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throwsSQLException257 {258 //TODO Auto-generated method stub
259 returncon.prepareCall(sql, resultSetType, resultSetConcurrency);260 }261
262 public Map> getTypeMap() throwsSQLException263 {264 //TODO Auto-generated method stub
265 returncon.getTypeMap();266 }267
268 public void setTypeMap(Map> map) throwsSQLException269 {270 //TODO Auto-generated method stub
271 con.setTypeMap(map);272 }273
274 public void setHoldability(int holdability) throwsSQLException275 {276 //TODO Auto-generated method stub
277 con.setHoldability(holdability);278 }279
280 public int getHoldability() throwsSQLException281 {282 //TODO Auto-generated method stub
283 returncon.getHoldability();284 }285
286 public Savepoint setSavepoint() throwsSQLException287 {288 //TODO Auto-generated method stub
289 returncon.setSavepoint();290 }291
292 public Savepoint setSavepoint(String name) throwsSQLException293 {294 //TODO Auto-generated method stub
295 returncon.setSavepoint(name);296 }297
298 public void rollback(Savepoint savepoint) throwsSQLException299 {300 //TODO Auto-generated method stub
301 con.rollback(savepoint);302 }303
304 public void releaseSavepoint(Savepoint savepoint) throwsSQLException305 {306 //TODO Auto-generated method stub
307 con.releaseSavepoint(savepoint);308 }309
310 public Statement createStatement(int resultSetType, int resultSetConcurrency, intresultSetHoldability)311 throwsSQLException312 {313 //TODO Auto-generated method stub
314 returncon.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);315 }316
317 public PreparedStatement prepareStatement(String sql, int resultSetType, intresultSetConcurrency,318 int resultSetHoldability) throwsSQLException319 {320 //TODO Auto-generated method stub
321 returncon.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);322 }323
324 public CallableStatement prepareCall(String sql, int resultSetType, intresultSetConcurrency,325 int resultSetHoldability) throwsSQLException326 {327 //TODO Auto-generated method stub
328 return null;329 }330
331 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throwsSQLException332 {333 //TODO Auto-generated method stub
334 returncon.prepareStatement(sql, autoGeneratedKeys);335 }336
337 public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throwsSQLException338 {339 //TODO Auto-generated method stub
340 returncon.prepareStatement(sql, columnIndexes);341 }342
343 public PreparedStatement prepareStatement(String sql, String[] columnNames) throwsSQLException344 {345 //TODO Auto-generated method stub
346 returncon.prepareStatement(sql, columnNames);347 }348
349 public Clob createClob() throwsSQLException350 {351 //TODO Auto-generated method stub
352 returncon.createClob();353 }354
355 public Blob createBlob() throwsSQLException356 {357 //TODO Auto-generated method stub
358 returncon.createBlob();359 }360
361 public NClob createNClob() throwsSQLException362 {363 //TODO Auto-generated method stub
364 returncon.createNClob();365 }366
367 public SQLXML createSQLXML() throwsSQLException368 {369 //TODO Auto-generated method stub
370 returncon.createSQLXML();371 }372
373 public boolean isValid(int timeout) throwsSQLException374 {375 //TODO Auto-generated method stub
376 returncon.isValid(timeout);377 }378
379 public void setClientInfo(String name, String value) throwsSQLClientInfoException380 {381 //TODO Auto-generated method stub
382 con.setClientInfo(name, value);383 }384
385 public void setClientInfo(Properties properties) throwsSQLClientInfoException386 {387 //TODO Auto-generated method stub
388 con.setClientInfo(properties);389 }390
391 public String getClientInfo(String name) throwsSQLException392 {393 //TODO Auto-generated method stub
394 returncon.getClientInfo(name);395 }396
397 public Properties getClientInfo() throwsSQLException398 {399 //TODO Auto-generated method stub
400 returncon.getClientInfo();401 }402
403 public Array createArrayOf(String typeName, Object[] elements) throwsSQLException404 {405 //TODO Auto-generated method stub
406 returncon.createArrayOf(typeName, elements);407 }408
409 public Struct createStruct(String typeName, Object[] attributes) throwsSQLException410 {411 //TODO Auto-generated method stub
412 returncon.createStruct(typeName, attributes);413 }414
415 }