davinci数据源定义
连接数据库
1,获取数据源----配置用相应的数据源类生成数据源---大部分的数据源支持jdbc,所以可以包装成jdbc的数据源,然后连接(获取到了数据源就可以进行一切数据库操作)
2,根据数据源获取连接,从而可以连接数据库
SourceController
SourceUtils
getDataSource 这里拓展数据源
dataSource = getDataSource(jdbcUrl, username, password, database, version, isExt);
connection = dataSource.getConnection();
DataSource getDataSource(String jdbcUrl, String userename, String password, String database, String version, boolean isExt) throws SourceException {
if (jdbcUrl.toLowerCase().contains(DataTypeEnum.ELASTICSEARCH.getDesc().toLowerCase())) {
return ESDataSource.getDataSource(jdbcUrl, userename, password, jdbcDataSource);
} else {
return jdbcDataSource.getDataSource(jdbcUrl, userename, password, database, version, isExt);
}
}
connection.setAutoCommit(false);
pstmt = connection.prepareStatement(sql);
public synchronized void removeDatasource(String jdbcUrl, String username, String password, String version, boolean isExt) {
String key = SourceUtils.getKey(jdbcUrl, username, password, version, isExt);
if (dataSourceMap.containsKey(key)) {
DruidDataSource druidDataSource = dataSourceMap.get(key);
druidDataSource.close();
dataSourceMap.remove(key);
}
}
获取到了数据源就可以进行一切数据库操作
/**
* 判断表是否存在
*
* @param tableName
* @return
* @throws SourceException
*/
public boolean tableIsExist(String tableName) throws SourceException {
boolean result = false;
Connection connection = null;
try {
connection = sourceUtils.getConnection(this.jdbcUrl, this.username, this.password, this.database, this.dbVersion, this.isExt);
if (null != connection) {
ResultSet tables = connection.getMetaData().getTables(null, null, tableName, null);
if (null != tables && tables.next()) {
result = true;
} else {
result = false;
}
tables.close();
}
} catch (Exception e) {
throw new SourceException("Get connection meta data error, jdbcUrl=" + this.jdbcUrl);
} finally {
sourceUtils.releaseConnection(connection);
}
return result;
}