当前位置:Gxlcms > 数据库问题 > Java创建连接池连接不同数据库

Java创建连接池连接不同数据库

时间:2021-07-01 10:21:17 帮助过:7人阅读

/** 2 * 数据库连接配置信息类 3 * @author Damon 4 */ 5 public class JdbcUrl 6 { 7 8 /** 定义数据库参数 */ 9 10 // 数据库类型 11 private String DBType; 12 // 数据库服务器IP 13 private String IP; 14 // 数据库服务器端口 15 private String Port; 16 // 数据库名称 17 private String DBName; 18 // 用户名 19 private String UserName; 20 // 密码 21 private String PassWord; 22 23 24 /** 25 * 默认构造方法,连接默认数据库 26 */ 27 public JdbcUrl() 28 { 29 // TODO Auto-generated constructor stub 30 DBType = SysCon.DATABASE_TYPE_MYSQL; 31 IP = "127.0.0.1"; 32 DBName = "mysql"; 33 Port = "3306"; 34 UserName = "damon"; 35 PassWord = "damon"; 36 } 37 38 /** 39 * 连接指定数据库 40 * @param urlType 传入连接类型标识 41 */ 42 public JdbcUrl(String urlType) 43 { 44 if ("mysql".equals(urlType)) 45 { 46 DBType = SysCon.DATABASE_TYPE_MYSQL; 47 IP = "127.0.0.1"; 48 DBName = "mysql"; 49 Port = "3306"; 50 UserName = "damon"; 51 PassWord = "damon"; 52 } 53 } 54 55 /** 56 * 获取连接句柄 57 * @return String 58 */ 59 public String getJdbcUrl() 60 { 61 String sUrl = ""; 62 63 if (DBType.trim().toUpperCase().equals("MYSQL")) 64 { 65 sUrl = "jdbc:mysql://" + IP + ":" + Port + "/" + DBName; 66 } 67 else if (DBType.trim().toUpperCase().equals("DB2")) 68 { 69 sUrl = "jdbc:db2://" + IP + ":" + Port + "/" + DBName; 70 } 71 72 else if (DBType.trim().toUpperCase().equals("ORACLE")) 73 { 74 sUrl = "jdbc:oracle:thin:@" + IP + ":" + Port + ":" + DBName; 75 } 76 77 else if (DBType.trim().toUpperCase().equals("SQLSERVER")) 78 { 79 sUrl = "jdbc:microsoft:sqlserver://" + IP + ":" + Port + ";databaseName=" + DBName + ";selectMethod=cursor"; 80 } 81 else if (DBType.trim().toUpperCase().equals("WEBLOGICPOOL")) 82 { 83 sUrl = "jdbc:weblogic:pool:" + DBName; 84 } 85 else 86 { 87 System.out.println("暂无对应数据库驱动"); 88 } 89 return sUrl; 90 } 91 92 // getters and setters 93 94 public String getDBType() 95 { 96 return DBType; 97 } 98 99 public void setDBType(String dBType) 100 { 101 DBType = dBType; 102 } 103 104 public String getIP() 105 { 106 return IP; 107 } 108 109 public void setIP(String iP) 110 { 111 IP = iP; 112 } 113 114 public String getPort() 115 { 116 return Port; 117 } 118 119 public void setPort(String port) 120 { 121 Port = port; 122 } 123 124 public String getDBName() 125 { 126 return DBName; 127 } 128 129 public void setDBName(String dBName) 130 { 131 DBName = dBName; 132 } 133 134 public String getUserName() 135 { 136 return UserName; 137 } 138 139 public void setUserName(String userName) 140 { 141 UserName = userName; 142 } 143 144 public String getPassWord() 145 { 146 return PassWord; 147 } 148 149 public void setPassWord(String passWord) 150 { 151 PassWord = passWord; 152 } 153 154 } View Code

 

2、重写一个Connection类,实现Connection接口的方法,同时连接数据库。

参数有已实现的JdbrUrl类,主要新增方法为:createConnection() 根据DBType来对不同数据库进行处理:加载对应的数据库,然后获取数据库连接。 技术分享 技术分享
  1 **
  2  * 数据库连接类,连接数据库
  3  * @author Damon
  4  */
  5 public class DBConn implements Connection
  6 {
  7 
  8     // 获取JdbcUrl信息
  9     private JdbcUrl JUrl;
 10 
 11     // 数据库连接
 12     private Connection con = null;
 13 
 14     // 连接是否已使用
 15     private boolean bNotInUse;
 16 
 17     private CharArrayWriter m_buf = new CharArrayWriter();
 18 
 19     private PrintWriter m_pw = new PrintWriter(m_buf, true);
 20 
 21     // 默认连接
 22     public DBConn()
 23     {
 24         // TODO Auto-generated constructor stub
 25         this.JUrl = new JdbcUrl();
 26     }
 27 
 28     // 指定数据库连接
 29     public DBConn(String urlType)
 30     {
 31         this.JUrl = new JdbcUrl(urlType);
 32     }
 33 
 34     // 创建连接
 35     public boolean createConnection()
 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 = new Properties();
 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 void setInUse()
100     {
101         /**
102          * Record stack information when each connection is get We reassian
103          * System.err, so Thread.currentThread().dumpStack() can dump stack info
104          * into our class FilterPrintStream.
105          */
106         new Throwable().printStackTrace(m_pw);
107 
108         bNotInUse = false;
109 
110         /**
111          * record lastest access time
112          */
113     }
114 
115     /* 下面都是 实现Connection的方法,返回conn的实现 */
116     public <T> T unwrap(Class<T> iface) throws SQLException
117     {
118         // TODO Auto-generated method stub
119         return con.unwrap(null);
120     }
121 
122     public boolean isWrapperFor(Class<?> iface) throws SQLException
123     {
124         // TODO Auto-generated method stub
125         return false;
126     }
127 
128     public Statement createStatement() throws SQLException
129     {
130         // TODO Auto-generated method stub
131         return con.createStatement();
132     }
133 
134     public PreparedStatement prepareStatement(String sql) throws SQLException
135     {
136         // TODO Auto-generated method stub
137         return con.prepareStatement(sql);
138     }
139 
140     public CallableStatement prepareCall(String sql) throws SQLException
141     {
142         // TODO Auto-generated method stub
143         return con.prepareCall(sql);
144     }
145 
146     public String nativeSQL(String sql) throws SQLException
147     {
148         // TODO Auto-generated method stub
149         return con.nativeSQL(sql);
150     }
151 
152     public void setAutoCommit(boolean autoCommit) throws SQLException
153     {
154         // TODO Auto-generated method stub
155         con.setAutoCommit(autoCommit);
156     }
157 
158     public boolean getAutoCommit() throws SQLException
159     {
160         // TODO Auto-generated method stub
161         return con.getAutoCommit();
162     }
163 
164     public void commit() throws SQLException
165     {
166         // TODO Auto-generated method stub
167         con.commit();
168     }
169 
170     public void rollback() throws SQLException
171     {
172         // TODO Auto-generated method stub
173         con.rollback();
174     }
175 
176     public void close() throws SQLException
177     {
178         // TODO Auto-generated method stub
179         con.close();
180     }
181 
182     public boolean isClosed() throws SQLException
183     {
184         // TODO Auto-generated method stub
185 
186         return con.isClosed();
187     }
188 
189     public DatabaseMetaData getMetaData() throws SQLException
190     {
191         // TODO Auto-generated method stub
192         return con.getMetaData();
193     }
194 
195     public void setReadOnly(boolean readOnly) throws SQLException
196     {
197         // TODO Auto-generated method stub
198         con.setReadOnly(readOnly);
199     }
200 
201     public boolean isReadOnly() throws SQLException
202     {
203         // TODO Auto-generated method stub
204         return con.isReadOnly();
205     }
206 
207     public void setCatalog(String catalog) throws SQLException
208     {
209         // TODO Auto-generated method stub
210         con.setCatalog(catalog);
211     }
212 
213     public String getCatalog() throws SQLException
214     {
215         // TODO Auto-generated method stub
216         return con.getCatalog();
217     }
218 
219     public void setTransactionIsolation(int level) throws SQLException
220     {
221         // TODO Auto-generated method stub
222         con.setTransactionIsolation(level);
223     }
224 
225     public int getTransactionIsolation() throws SQLException
226     {
227         // TODO Auto-generated method stub
228         return con.getTransactionIsolation();
229     }
230 
231     public SQLWarning getWarnings() throws SQLException
232     {
233         // TODO Auto-generated method stub
234         return con.getWarnings();
235     }
236 
237     public void clearWarnings() throws SQLException
238     {
239         // TODO Auto-generated method stub
240         con.clearWarnings();
241     }
242 
243     public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException
244     {
245         // TODO Auto-generated method stub
246         return con.createStatement(resultSetType, resultSetConcurrency);
247     }
248 
249     public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
250             throws SQLException
251     {
252         // TODO Auto-generated method stub
253         return con.prepareStatement(sql, resultSetType, resultSetConcurrency);
254     }
255 
256     public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException
257     {
258         // TODO Auto-generated method stub
259         return con.prepareCall(sql, resultSetType, resultSetConcurrency);
260     }
261 
262     public Map<String, Class<?>> getTypeMap() throws SQLException
263     {
264         // TODO Auto-generated method stub
265         return con.getTypeMap();
266     }
267 
268     public void setTypeMap(Map<String, Class<?>> map) throws SQLException
269     {
270         // TODO Auto-generated method stub
271         con.setTypeMap(map);
272     }
273 
274     public void setHoldability(int holdability) throws SQLException
275     {
276         // TODO Auto-generated method stub
277         con.setHoldability(holdability);
278     }
279 
280     public int getHoldability() throws SQLException
281     {
282         // TODO Auto-generated method stub
283         return con.getHoldability();
284     }
285 
286     public Savepoint setSavepoint() throws SQLException
287     {
288         // TODO Auto-generated method stub
289         return con.setSavepoint();
290     }
291 
292     public Savepoint setSavepoint(String name) throws SQLException
293     {
294         // TODO Auto-generated method stub
295         return con.setSavepoint(name);
296     }
297 
298     public void rollback(Savepoint savepoint) throws SQLException
299     {
300         // TODO Auto-generated method stub
301         con.rollback(savepoint);
302     }
303 
304     public void releaseSavepoint(Savepoint savepoint) throws SQLException
305     {
306         // TODO Auto-generated method stub
307         con.releaseSavepoint(savepoint);
308     }
309 
310     public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
311             throws SQLException
312     {
313         // TODO Auto-generated method stub
314         return con.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
315     }
316 
317     public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency,
318             int resultSetHoldability) throws SQLException
319     {
320         // TODO Auto-generated method stub
321         return con.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
322     }
323 
324     public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
325             int resultSetHoldability) throws SQLException
326     {
327         // TODO Auto-generated method stub
328         return null;
329     }
330 
331     public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException
332     {
333         // TODO Auto-generated method stub
334         return con.prepareStatement(sql, autoGeneratedKeys);
335     }
336 
337     public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException
338     {
339         // TODO Auto-generated method stub
340         return con.prepareStatement(sql, columnIndexes);
341     }
342 
343     public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException
344     {
345         // TODO Auto-generated method stub
346         return con.prepareStatement(sql, columnNames);
347     }
348 
349     public Clob createClob() throws SQLException
350     {
351         // TODO Auto-generated method stub
352         return con.createClob();
353     }
354 
355     public Blob createBlob() throws SQLException
356     {
357         // TODO Auto-generated method stub
358         return con.createBlob();
359     }
360 
361     public NClob createNClob() throws SQLException
362     {
363         // TODO Auto-generated method stub
364         return con.createNClob();
365     }
366 
367     public SQLXML createSQLXML() throws SQLException
368     {
369         // TODO Auto-generated method stub
370         return con.createSQLXML();
371     }
372 
373     public boolean isValid(int timeout) throws SQLException
374     {
375         // TODO Auto-generated method stub
376         return con.isValid(timeout);
377     }
378 
379     public void setClientInfo(String name, String value) throws SQLClientInfoException
380     {
381         // TODO Auto-generated method stub
382         con.setClientInfo(name, value);
383     }
384 
385     public void setClientInfo(Properties properties) throws SQLClientInfoException
386     {
387         // TODO Auto-generated method stub
388         con.setClientInfo(properties);
389     }


                  

	 	
                    
                    
                    
                    
                    
                

人气教程排行