nutz中实现多数据库切换

 Java代码nutz很好的实现了表切换
根据它的思路写了个数据库切换,测试可行!
Java代码 复制代码 收藏代码
  1. public class MutiDataSource implements DataSource {
  2. private static ConcurrentHashMap<String, DruidDataSource> dataSourceMap = new ConcurrentHashMap<String, DruidDataSource>();
  3. protected HashMap<String, String> dsdbMap;
  4. protected String driverClassName;
  5. protected String url;
  6. protected String username;
  7. protected String password;
  8. protected int maxActive;
  9. public MutiDataSource() {
  10. }
  11. public void init() throws SQLException {
  12. Iterator<String> it = dsdbMap.keySet().iterator();
  13. String dsname, dbname;
  14. while (it.hasNext()) {
  15. dsname = it.next();
  16. dbname = dsdbMap.get(dsname);
  17. DruidDataSource ds = createDataSource(dbname);
  18. dataSourceMap.put(dsname, ds);
  19. }
  20. }
  21. public void close() {
  22. Iterator<DruidDataSource> it = dataSourceMap.values().iterator();
  23. while (it.hasNext()) {
  24. it.next().close();
  25. }
  26. }
  27. protected DruidDataSource createDataSource(String dbname) {
  28. DruidDataSource ds = new DruidDataSource();
  29. ds.setDriverClassName(driverClassName);
  30. ds.setUsername(username);
  31. ds.setPassword(password);
  32. ds.setUrl(url + dbname);
  33. return ds;
  34. }
  35. public DataSource getDs() {
  36. return dataSourceMap.get(DataSourceNames.get());
  37. }
  38. public void setDriverClassName(String driverClassName) {
  39. this.driverClassName = driverClassName;
  40. }
  41. public void setUrl(String url) {
  42. this.url = url;
  43. }
  44. public void setUsername(String username) {
  45. this.username = username;
  46. }
  47. public void setPassword(String password) {
  48. this.password = password;
  49. }
  50. public void setMaxActive(int maxActive) {
  51. this.maxActive = maxActive;
  52. }
  53. @Override
  54. public PrintWriter getLogWriter() throws SQLException {
  55. return getDs().getLogWriter();
  56. }
  57. @Override
  58. public int getLoginTimeout() throws SQLException {
  59. return getDs().getLoginTimeout();
  60. }
  61. @Override
  62. public void setLogWriter(PrintWriter out) throws SQLException {
  63. getDs().setLogWriter(out);
  64. }
  65. @Override
  66. public void setLoginTimeout(int seconds) throws SQLException {
  67. getDs().setLoginTimeout(seconds);
  68. }
  69. @Override
  70. public boolean isWrapperFor(Class<?> iface) throws SQLException {
  71. return getDs().isWrapperFor(iface);
  72. }
  73. @Override
  74. public <T> T unwrap(Class<T> iface) throws SQLException {
  75. return getDs().unwrap(iface);
  76. }
  77. @Override
  78. public Connection getConnection() throws SQLException {
  79. return getDs().getConnection();
  80. }
  81. @Override
  82. public Connection getConnection(String username, String password)
  83. throws SQLException {
  84. return getDs().getConnection(username, password);
  85. }
  86. }
public class MutiDataSource implements DataSource {
	private static ConcurrentHashMap<String, DruidDataSource> dataSourceMap = new ConcurrentHashMap<String, DruidDataSource>();
	protected HashMap<String, String> dsdbMap;
	protected String driverClassName;
	protected String url;
	protected String username;
	protected String password;
	protected int maxActive;
	public MutiDataSource() {
	}
	public void init() throws SQLException {
		Iterator<String> it = dsdbMap.keySet().iterator();
		String dsname, dbname;
		while (it.hasNext()) {
			dsname = it.next();
			dbname = dsdbMap.get(dsname);
			DruidDataSource ds = createDataSource(dbname);
			dataSourceMap.put(dsname, ds);
		}
	}
	public void close() {
		Iterator<DruidDataSource> it = dataSourceMap.values().iterator();
		while (it.hasNext()) {
			it.next().close();
		}
	}
	protected DruidDataSource createDataSource(String dbname) {
		DruidDataSource ds = new DruidDataSource();
		ds.setDriverClassName(driverClassName);
		ds.setUsername(username);
		ds.setPassword(password);
		ds.setUrl(url + dbname);
		return ds;
	}
	public DataSource getDs() {
		return dataSourceMap.get(DataSourceNames.get());
	}
	public void setDriverClassName(String driverClassName) {
		this.driverClassName = driverClassName;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public void setMaxActive(int maxActive) {
		this.maxActive = maxActive;
	}
	@Override
	public PrintWriter getLogWriter() throws SQLException {
		return getDs().getLogWriter();
	}
	@Override
	public int getLoginTimeout() throws SQLException {
		return getDs().getLoginTimeout();
	}
	@Override
	public void setLogWriter(PrintWriter out) throws SQLException {
		getDs().setLogWriter(out);
	}
	@Override
	public void setLoginTimeout(int seconds) throws SQLException {
		getDs().setLoginTimeout(seconds);
	}
	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		return getDs().isWrapperFor(iface);
	}
	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		return getDs().unwrap(iface);
	}
	@Override
	public Connection getConnection() throws SQLException {
		return getDs().getConnection();
	}
	@Override
	public Connection getConnection(String username, String password)
			throws SQLException {
		return getDs().getConnection(username, password);
	}
}

Java代码 复制代码 收藏代码
  1. public class DataSourceNames {
  2. private static final ThreadLocal<String> ds = new ThreadLocal<String>();
  3. public static String get() {
  4. return ds.get();
  5. }
  6. public static String set(String obj) {
  7. String re = get();
  8. ds.set(obj);
  9. return re;
  10. }
  11. public static void clear() {
  12. set(null);
  13. }
  14. }  

你可能感兴趣的:(Excel)