Java创建连接池连接不同数据库
Posted Damon
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java创建连接池连接不同数据库相关的知识,希望对你有一定的参考价值。
在一个应用里面,可能涉及到连接多个不同数据库进行操作,而每次连接写不同的实现会很麻烦。前面已经会了用JDBC连接数据库,那么利用反射和工厂模式,可以实现连接不同的数据库,这样处理起来将会很方便。同时建造数据库连接池,处理多个业务数据处理。
那么具体怎么实现呢,下面一起来看一下:
整体结构如下:
第一步,先处理连接不同数据库
1、首先,将数据库配置信息创建一个公用类:JdbcUrl.java
主数据库可以用默认的构造方法,如果是连接其他库,则通过传递参数的方式来处理。
数据库参数有如下几个:
1 /** 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 }
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 { 以上是关于Java创建连接池连接不同数据库的主要内容,如果未能解决你的问题,请参考以下文章