跨数据库查询

当前项目配置的数据库与实际需要查询的数据库不一致时

private JdbcTemplate getTemplate (String url, String userName, String password, String databaseType,String driverClassName) {
	        if (url == null || url.isEmpty() || userName == null || userName.isEmpty() || password == null
	                || password.isEmpty() || databaseType == null || databaseType.isEmpty() || driverClassName.isEmpty() || driverClassName==null) {
	            return null;
	        }
	        DataSource dataSource = new DataSource();
	        dataSource.setUrl(url);
	        dataSource.setUsername(userName);
	        dataSource.setPassword(password);
	        dataSource.setTestOnBorrow(true);
	        dataSource.setDriverClassName(driverClassName);
	        switch (databaseType.toUpperCase()) {
	            case "MYSQL" : dataSource.setValidationQuery("select 1"); break;
	            case "ORACLE" : dataSource.setValidationQuery("select 1 from dual");break;
	            case "SQLSERVER" : dataSource.setValidationQuery("select 1");break;
	            default: break;
	        }
	        return new JdbcTemplate(dataSource);
	    }
	@ResponseBody
	@RequestMapping(params = "getTableInfo")
	public List> getTableInfo() throws SQLException {
	    //示例:连接oracle数据库
		JdbcTemplate jt = getTemplate("jdbc:oracle:thin:@localhost:orcl",用户名,密码,"oracle","oracle.jdbc.OracleDriver");
		String sql = "select * from user ";
		List> list = new ArrayList>();		
		list =  jt.queryForList(sql);
		//String json = JSONArray.fromObject(list).toString();
		return list;
	}

你可能感兴趣的:(java)