Java Code Examples for org.springframework.jdbc.support.JdbcUtils#closeConnection()

The following examples show how to use org.springframework.jdbc.support.JdbcUtils#closeConnection() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DbCommonServiceImpl.java    From bdf3 with Apache License 2.0 6 votes vote down vote up
@Override
public List<String> findDefaultColumnType(String dbInfoId) throws Exception {
	DataSource ds = this.getDataSourceByDbInfoId(dbInfoId);
	Connection conn = null;
	ResultSet resultSet = null;
	try {
		conn = DataSourceUtils.getConnection(ds);
		DatabaseMetaData metaData = conn.getMetaData();
		resultSet = metaData.getTypeInfo();
		List<String> list = new ArrayList<String>();
		while (resultSet.next()) {
			String typeName = resultSet.getString("TYPE_NAME").toUpperCase();
			list.add(typeName);
		}
		return list;
	} finally {
		JdbcUtils.closeResultSet(resultSet);
		JdbcUtils.closeConnection(conn);
	}
}
 
Example 2
Source File: DbCommonServiceImpl.java    From bdf3 with Apache License 2.0 6 votes vote down vote up
@Override
public List<String> findTablePrimaryKeys(String dbInfoId, String tableName) throws Exception {
	List<String> primaryKeys = new ArrayList<String>();
	Connection con = null;
	ResultSet rs = null;
	DataSource ds = this.getDataSourceByDbInfoId(dbInfoId);
	try {
		con = ds.getConnection();
		DatabaseMetaData metaData = con.getMetaData();
		rs = metaData.getPrimaryKeys(null, null, tableName.toUpperCase());
		while (rs.next()) {
			primaryKeys.add(rs.getString("COLUMN_NAME").toUpperCase());
		}
		return primaryKeys;
	} finally {
		JdbcUtils.closeResultSet(rs);
		JdbcUtils.closeConnection(con);
	}
}
 
Example 3
Source File: DbService.java    From bdf3 with Apache License 2.0 6 votes vote down vote up
/**
 * 初始化默认数据库配置信息
 * 
 * @return 返回DbInfo对象
 * @throws Exception
 */
public DbInfo initDefaultDbInfo() throws Exception {
	DbInfo dbInfo = new DbInfo();
	dbInfo.setId(DbConstants.DEFAULTDATASOURCE);
	Connection conn = null;
	try {
		conn = dataSource.getConnection();
		DatabaseMetaData metaData = conn.getMetaData();
		dbInfo.setDbType(metaData.getDatabaseProductName());
		dbInfo.setName("默认连接" + dbInfo.getDbType());
		dbInfo.setUrl(metaData.getURL());
		dbInfo.setUsername(metaData.getUserName());
		dbInfo.setProductName(metaData.getDatabaseProductName());
		dbInfo.setProductVersion(metaData.getDatabaseProductVersion());
	} finally {
		JdbcUtils.closeConnection(conn);
	}
	return dbInfo;

}
 
Example 4
Source File: HibernateJpaDialect.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void releaseConnection(Connection con) {
	if (sessionConnectionMethod != null) {
		// Need to explicitly call close() with Hibernate 3.x in order to allow
		// for eager release of the underlying physical Connection if necessary.
		// However, do not do this on Hibernate 4.2+ since it would return the
		// physical Connection to the pool right away, making it unusable for
		// further operations within the current transaction!
		JdbcUtils.closeConnection(con);
	}
}
 
Example 5
Source File: DatasourceServletAction.java    From ureport with Apache License 2.0 5 votes vote down vote up
public void buildDatabaseTables(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	Connection conn=null;
	ResultSet rs = null;
	try{
		conn=buildConnection(req);
		DatabaseMetaData metaData = conn.getMetaData();
		String url = metaData.getURL();
		String schema = null;
		if (url.toLowerCase().contains("oracle")) {
			schema = metaData.getUserName();
		}
		List<Map<String,String>> tables = new ArrayList<Map<String,String>>();
		rs = metaData.getTables(null, schema, "%", new String[] { "TABLE","VIEW" });
		while (rs.next()) {
			Map<String,String> table = new HashMap<String,String>();
			table.put("name",rs.getString("TABLE_NAME"));
			table.put("type",rs.getString("TABLE_TYPE"));
			tables.add(table);
		}
		writeObjectToJson(resp, tables);
	}catch(Exception ex){
		throw new ServletException(ex);
	}finally{
		JdbcUtils.closeResultSet(rs);
		JdbcUtils.closeConnection(conn);
	}
}
 
Example 6
Source File: DbCommonServiceImpl.java    From bdf3 with Apache License 2.0 5 votes vote down vote up
@Override
public List<TableInfo> findTableInfos(String dbInfoId) throws Exception {
	List<TableInfo> tablesList = new ArrayList<TableInfo>();
	DataSource ds = this.getDataSourceByDbInfoId(dbInfoId);
	Connection conn = null;
	ResultSet rs = null;
	try {
		conn = DataSourceUtils.getConnection(ds);
		DatabaseMetaData metaData = conn.getMetaData();
		String url = metaData.getURL();
		String schema = null;
		if (url.toLowerCase().contains("oracle")) {
			String value = Configure.getString("bdf2.default.schema");
			if (StringUtils.hasText(value)) {
				schema = value;
			} else {
				schema = metaData.getUserName();
			}
		}
		rs = metaData.getTables(null, schema, "%", new String[] { "TABLE" });
		TableInfo tableInfo = null;
		while (rs.next()) {
			tableInfo = new TableInfo();
			tableInfo.setTableName(rs.getString("TABLE_NAME"));
			tableInfo.setDbInfoId(dbInfoId);
			tablesList.add(tableInfo);
		}
		return tablesList;
	} finally {
		JdbcUtils.closeResultSet(rs);
		JdbcUtils.closeConnection(conn);
	}
}
 
Example 7
Source File: HibernateJpaDialect.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void releaseConnection(Connection con) {
	if (sessionConnectionMethod != null) {
		// Need to explicitly call close() with Hibernate 3.x in order to allow
		// for eager release of the underlying physical Connection if necessary.
		// However, do not do this on Hibernate 4.2+ since it would return the
		// physical Connection to the pool right away, making it unusable for
		// further operations within the current transaction!
		JdbcUtils.closeConnection(con);
	}
}
 
Example 8
Source File: OpenJpaDialect.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void releaseConnection(Connection con) {
	JdbcUtils.closeConnection(con);
}
 
Example 9
Source File: DefaultJdoDialect.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void releaseConnection(Connection con) {
	JdbcUtils.closeConnection(con);
}
 
Example 10
Source File: DatasourceServletAction.java    From ureport with Apache License 2.0 4 votes vote down vote up
public void buildFields(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	String sql=req.getParameter("sql");
	String parameters=req.getParameter("parameters");
	Connection conn=null;
	final List<Field> fields=new ArrayList<Field>();
	try{
		conn=buildConnection(req);
		Map<String, Object> map = buildParameters(parameters);
		sql=parseSql(sql, map);
		if(ProcedureUtils.isProcedure(sql)){
			List<Field> fieldsList = ProcedureUtils.procedureColumnsQuery(sql, map, conn);
			fields.addAll(fieldsList);
		}else{
			DataSource dataSource=new SingleConnectionDataSource(conn,false);
			NamedParameterJdbcTemplate jdbc=new NamedParameterJdbcTemplate(dataSource);
			PreparedStatementCreator statementCreator=getPreparedStatementCreator(sql,new MapSqlParameterSource(map));
			jdbc.getJdbcOperations().execute(statementCreator, new PreparedStatementCallback<Object>() {
				@Override
				public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
					ResultSet rs = null;
					try {
						rs = ps.executeQuery();
						ResultSetMetaData metadata=rs.getMetaData();
						int columnCount=metadata.getColumnCount();
						for(int i=0;i<columnCount;i++){
							String columnName=metadata.getColumnLabel(i+1);
							fields.add(new Field(columnName));
						}
						return null;
					}finally {
						JdbcUtils.closeResultSet(rs);
					}
				}
			});
		}
		writeObjectToJson(resp, fields);
	}catch(Exception ex){
		throw new ReportDesignException(ex);
	}finally{
		JdbcUtils.closeConnection(conn);
	}
}
 
Example 11
Source File: DbCommonServiceImpl.java    From bdf3 with Apache License 2.0 4 votes vote down vote up
@Override
public List<ColumnInfo> findMultiColumnInfos(String dbInfoId, String sql) throws Exception {
	if (!StringUtils.hasText(sql)) {
		return null;
	}
	DataSource ds = this.getDataSourceByDbInfoId(dbInfoId);
	Connection conn = null;
	Statement st = null;
	ResultSet rs = null;
	try {
		conn = DataSourceUtils.getConnection(ds);
		st = conn.createStatement();
		rs = st.executeQuery(sql);
		ResultSetMetaData rsmd = rs.getMetaData();
		int count = rsmd.getColumnCount();
		List<ColumnInfo> columnNames = new ArrayList<ColumnInfo>();
		ColumnInfo columnInfo = null;
		for (int i = 1; i <= count; i++) {
			columnInfo = new ColumnInfo();
			columnInfo.setColumnName(rsmd.getColumnLabel(i));
			columnInfo.setColumnType(rsmd.getColumnTypeName(i));
			columnInfo.setTableName(rsmd.getTableName(i));
			if (rsmd.getPrecision(i) > 0 && !columnInfo.getColumnType().equals("DATETIME") && !columnInfo.getColumnType().equals("TIMESTAMP") && !columnInfo.getColumnType().equals("DATE")) {
				columnInfo.setColumnSize(String.valueOf(rsmd.getPrecision(i)));
			}
			if (rsmd.getScale(i) > 0 && !columnInfo.getColumnType().equals("DATETIME") && !columnInfo.getColumnType().equals("TIMESTAMP") && !columnInfo.getColumnType().equals("DATE")) {
				columnInfo.setColumnSize(columnInfo.getColumnSize() + "," + rsmd.getScale(i));
			}
			int flagI = rsmd.isNullable(i);
			if (flagI == 0) {
				columnInfo.setIsnullAble(false);
			} else {
				columnInfo.setIsnullAble(true);
			}
			columnNames.add(columnInfo);
		}
		return columnNames;
	} finally {
		JdbcUtils.closeResultSet(rs);
		JdbcUtils.closeStatement(st);
		JdbcUtils.closeConnection(conn);
	}
}
 
Example 12
Source File: OpenJpaDialect.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void releaseConnection(Connection con) {
	JdbcUtils.closeConnection(con);
}
 
Example 13
Source File: DefaultJdoDialect.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void releaseConnection(Connection con) {
	JdbcUtils.closeConnection(con);
}
 
Example 14
Source File: OpenJpaDialect.java    From syncope with Apache License 2.0 4 votes vote down vote up
@Override
public void releaseConnection(final Connection con) {
    JdbcUtils.closeConnection(con);
}
 
Example 15
Source File: RowIterator.java    From ecs-sync with Apache License 2.0 4 votes vote down vote up
@Override
public void close() {
    JdbcUtils.closeResultSet(rs);
    JdbcUtils.closeStatement(st);
    JdbcUtils.closeConnection(con);
}