當前位置: 首頁>>代碼示例>>Java>>正文


Java JdbcUtils類代碼示例

本文整理匯總了Java中org.springframework.jdbc.support.JdbcUtils的典型用法代碼示例。如果您正苦於以下問題:Java JdbcUtils類的具體用法?Java JdbcUtils怎麽用?Java JdbcUtils使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


JdbcUtils類屬於org.springframework.jdbc.support包,在下文中一共展示了JdbcUtils類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getNextKey

import org.springframework.jdbc.support.JdbcUtils; //導入依賴的package包/類
/**
 * Executes the SQL as specified by {@link #getSequenceQuery()}.
 */
@Override
protected long getNextKey() throws DataAccessException {
	Connection con = DataSourceUtils.getConnection(getDataSource());
	Statement stmt = null;
	ResultSet rs = null;
	try {
		stmt = con.createStatement();
		DataSourceUtils.applyTransactionTimeout(stmt, getDataSource());
		rs = stmt.executeQuery(getSequenceQuery());
		if (rs.next()) {
			return rs.getLong(1);
		}
		else {
			throw new DataAccessResourceFailureException("Sequence query did not return a result");
		}
	}
	catch (SQLException ex) {
		throw new DataAccessResourceFailureException("Could not obtain sequence value", ex);
	}
	finally {
		JdbcUtils.closeResultSet(rs);
		JdbcUtils.closeStatement(stmt);
		DataSourceUtils.releaseConnection(con, getDataSource());
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:29,代碼來源:AbstractSequenceMaxValueIncrementer.java

示例2: processRow

import org.springframework.jdbc.support.JdbcUtils; //導入依賴的package包/類
/**
 * Implementation of ResultSetCallbackHandler.
 * Work out column size if this is the first row, otherwise just count rows.
 * <p>Subclasses can perform custom extraction or processing
 * by overriding the {@code processRow(ResultSet, int)} method.
 * @see #processRow(java.sql.ResultSet, int)
 */
@Override
public final void processRow(ResultSet rs) throws SQLException {
	if (this.rowCount == 0) {
		ResultSetMetaData rsmd = rs.getMetaData();
		this.columnCount = rsmd.getColumnCount();
		this.columnTypes = new int[this.columnCount];
		this.columnNames = new String[this.columnCount];
		for (int i = 0; i < this.columnCount; i++) {
			this.columnTypes[i] = rsmd.getColumnType(i + 1);
			this.columnNames[i] = JdbcUtils.lookupColumnName(rsmd, i + 1);
		}
		// could also get column names
	}
	processRow(rs, this.rowCount++);
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:23,代碼來源:RowCountCallbackHandler.java

示例3: executeSchemaScript

import org.springframework.jdbc.support.JdbcUtils; //導入依賴的package包/類
/**
 * Execute the given schema script on the given JDBC Connection.
 * <p>Note that the default implementation will log unsuccessful statements
 * and continue to execute. Override the {@code executeSchemaStatement}
 * method to treat failures differently.
 * @param con the JDBC Connection to execute the script on
 * @param sql the SQL statements to execute
 * @throws SQLException if thrown by JDBC methods
 * @see #executeSchemaStatement
 */
protected void executeSchemaScript(Connection con, String[] sql) throws SQLException {
	if (sql != null && sql.length > 0) {
		boolean oldAutoCommit = con.getAutoCommit();
		if (!oldAutoCommit) {
			con.setAutoCommit(true);
		}
		try {
			Statement stmt = con.createStatement();
			try {
				for (String sqlStmt : sql) {
					executeSchemaStatement(stmt, sqlStmt);
				}
			}
			finally {
				JdbcUtils.closeStatement(stmt);
			}
		}
		finally {
			if (!oldAutoCommit) {
				con.setAutoCommit(false);
			}
		}
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:35,代碼來源:LocalSessionFactoryBean.java

示例4: readPrimaryKeyNames

import org.springframework.jdbc.support.JdbcUtils; //導入依賴的package包/類
private static Collection<String> readPrimaryKeyNames(DatabaseMetaDataWrapper metaData, String tableName)
                                                                                                         throws SQLException {
    ResultSet pkData = null;

    try {
        List<String> pks = new ArrayList<String>();
        Map<String, Object> values;

        for (pkData = metaData.getPrimaryKeys(tableName); pkData.next(); pks.add(readPrimaryKeyName(metaData,
            values))) {
            values = readColumns(pkData, initColumnsForPK());
        }

        return pks;
    } finally {
        JdbcUtils.closeResultSet(pkData);
    }
}
 
開發者ID:luoyaogui,項目名稱:otter-G,代碼行數:19,代碼來源:DdlUtils.java

示例5: mapRow

import org.springframework.jdbc.support.JdbcUtils; //導入依賴的package包/類
@Override
public T mapRow(ResultSet rs, int rowNum) throws SQLException {
    T mappedObject = this.entityMapper.createInstance();
    ResultSetMetaData rsmd = rs.getMetaData();
    int columnCount = rsmd.getColumnCount();
    Map<String, Class<?>> types = this.entityMapper.types();

    this.entityMapper.startMapping(mappedObject);
    for (int index = 1; index <= columnCount; index++) {
        String column = JdbcUtils.lookupColumnName(rsmd, index);
        Object value = JdbcUtils.getResultSetValue(rs, index, types.get(column));

        this.entityMapper.setValue(mappedObject, column, value);
    }
    this.entityMapper.finishMapping(mappedObject);
    return mappedObject;
}
 
開發者ID:cybozu,項目名稱:spring-data-jdbc-template,代碼行數:18,代碼來源:EntityRowMapper.java

示例6: mapRow

import org.springframework.jdbc.support.JdbcUtils; //導入依賴的package包/類
/**
 * <B>方法名稱:</B>映射行數據<BR>
 * <B>概要說明:</B><BR>
 * 
 * @param rs 結果集
 * @param row 行號
 * @return JSONObject 數據
 * @throws SQLException SQL異常錯誤
 * @see RowMapper#mapRow(ResultSet,
 *      int)
 */
public JSONObject mapRow(ResultSet rs, int row) throws SQLException {
    String key = null;
    Object obj = null;
    JSONObject json = new JSONObject();
    ResultSetMetaData rsmd = rs.getMetaData();
    int count = rsmd.getColumnCount();
    for (int i = 1; i <= count; i++) {
        key = JdbcUtils.lookupColumnName(rsmd, i);
        obj = JdbcUtils.getResultSetValue(rs, i);
        try {
            json.put(key, obj);
        }
        catch (JSONException e) {
        }
    }
    return json;
}
 
開發者ID:craware,項目名稱:webapp-tyust,代碼行數:29,代碼來源:JsonRowMapper.java

示例7: findDefaultColumnType

import org.springframework.jdbc.support.JdbcUtils; //導入依賴的package包/類
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);
	}
}
 
開發者ID:bsteker,項目名稱:bdf2,代碼行數:20,代碼來源:DbCommonServiceImpl.java

示例8: findTablePrimaryKeys

import org.springframework.jdbc.support.JdbcUtils; //導入依賴的package包/類
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);
	}
}
 
開發者ID:bsteker,項目名稱:bdf2,代碼行數:19,代碼來源:DbCommonServiceImpl.java

示例9: initDefaultDbInfo

import org.springframework.jdbc.support.JdbcUtils; //導入依賴的package包/類
/**
 * 初始化默認數據庫配置信息
 * 
 * @return 返回DbInfo對象
 * @throws Exception
 */
public DbInfo initDefaultDbInfo() throws Exception {
	DbInfo dbInfo = new DbInfo();
	dbInfo.setId(DbConstants.DEFAULTDATASOURCE);
	Connection conn = null;
	try {
		conn = this.getJdbcTemplate().getDataSource().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;

}
 
開發者ID:bsteker,項目名稱:bdf2,代碼行數:26,代碼來源:DbService.java

示例10: loadTablePrimaryKeys

import org.springframework.jdbc.support.JdbcUtils; //導入依賴的package包/類
private List<String> loadTablePrimaryKeys(String tableName)throws Exception{
	DataSource ds=this.getJdbcTemplate().getDataSource();
	Connection con = DataSourceUtils.getConnection(ds);
	List<String> primaryKeyList=new ArrayList<String>();
	Statement stmt = null;
	ResultSet rs=null;
	try{
		DatabaseMetaData metaData = con.getMetaData();
		rs = metaData.getPrimaryKeys(null, null, tableName.toUpperCase());
		while (rs.next()) {
			primaryKeyList.add(rs.getString("COLUMN_NAME"));
		}
	}finally{
		JdbcUtils.closeResultSet(rs);
		JdbcUtils.closeStatement(stmt);
		JdbcUtils.closeConnection(con);
	}
	return primaryKeyList;
}
 
開發者ID:bsteker,項目名稱:bdf2,代碼行數:20,代碼來源:EntityPR.java

示例11: loadTablePrimaryKeys

import org.springframework.jdbc.support.JdbcUtils; //導入依賴的package包/類
private String loadTablePrimaryKeys(String tableName,DatabaseMetaData metaData) throws Exception{
	String keys="";
	ResultSet rs = null;
	try {
		rs = metaData.getPrimaryKeys(null, null, tableName.toUpperCase());
		int i=0;
		while (rs.next()) {
			if(i>0){
				keys+=",";
			}
			i++;
			keys+=rs.getString("COLUMN_NAME");
		}
	} finally {
		JdbcUtils.closeResultSet(rs);
	}
	return keys;
}
 
開發者ID:bsteker,項目名稱:bdf2,代碼行數:19,代碼來源:QueryWizardPR.java

示例12: mapRow

import org.springframework.jdbc.support.JdbcUtils; //導入依賴的package包/類
@Override
public Map<String, Object>  mapRow(ResultSet rs, int rowNum) throws SQLException {
	ResultSetMetaData rsmd = rs.getMetaData();
	int columnCount = rsmd.getColumnCount();
	Map mapOfColValues = createColumnMap(columnCount);
	for (int i = 1; i <= columnCount; i++) {
		String key = getColumnKey(JdbcUtils.lookupColumnName(rsmd, i));
		key = key.toLowerCase();
		Object obj = null;
		String typename= rsmd.getColumnTypeName(i).toUpperCase();
		if("DECIMAL".equals(typename)){
			obj = rs.getDouble(i);
		}else{
			obj = getColumnValue(rs, i);
		}
		 
		mapOfColValues.put(key, obj);
	}
	return mapOfColValues;
}
 
開發者ID:yulele166,項目名稱:pub-service,代碼行數:21,代碼來源:MySqlColumnMapRowMapper.java

示例13: getSingleMlogRecord

import org.springframework.jdbc.support.JdbcUtils; //導入依賴的package包/類
/**
 * 根據mlog物化日誌表的SEQUENCE$$字段單獨查詢
 *
 * @param sequence
 * @param context
 * @param columns
 * @return
 */
private List<ColumnValue> getSingleMlogRecord(final String singleMlogExtractSql, final int sequence, final YuGongContext context, final List<ColumnMeta> mlogCols, final List<ColumnMeta> columns, final List<ColumnValue> rowIds) {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(context.getSourceDs());
    final List<ColumnValue> columnValues = new ArrayList<ColumnValue>();
    return (List<ColumnValue>) jdbcTemplate.execute(singleMlogExtractSql, new PreparedStatementCallback() {
        @Override
        public List<ColumnValue> doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
            ps.setInt(1, sequence);
            ResultSet rs = ps.executeQuery();
            try {
                while (rs.next()) {
                    rowIds.add(new ColumnValue(rowidColumn, rs.getObject("rowid")));
                    columnValues.addAll(buildColumnValue(rs, context.getSourceEncoding(), mlogCols, columns));
                }
            } finally {
                JdbcUtils.closeResultSet(rs);
            }
            return columnValues;
        }
    });
}
 
開發者ID:kevinKaiF,項目名稱:cango,代碼行數:29,代碼來源:KafkaIncRecordExtractor.java

示例14: getShardKeyByDRDS

import org.springframework.jdbc.support.JdbcUtils; //導入依賴的package包/類
/**
 * 獲取DRDS下表的拆分字段, 返回格式為 id,name
 *
 * @param dataSource
 * @param schemaName
 * @param tableName
 * @return
 */
public static String getShardKeyByDRDS(final DataSource dataSource, final String schemaName, final String tableName) {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    return (String) jdbcTemplate.execute(queryShardKey, new PreparedStatementCallback() {

        public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
            DatabaseMetaData metaData = ps.getConnection().getMetaData();
            // String sName = getIdentifierName(schemaName, metaData);
            String tName = getIdentifierName(tableName, metaData);

            ps.setString(1, tName);
            ResultSet rs = null;
            try {
                rs = ps.executeQuery();
                String log = null;
                if (rs.next()) {
                    log = rs.getString("KEYS");
                }

                return log;
            } finally {
                JdbcUtils.closeResultSet(rs);
            }
        }
    });
}
 
開發者ID:kevinKaiF,項目名稱:cango,代碼行數:34,代碼來源:TableMetaGenerator.java

示例15: getDatabaseURLAndUserName

import org.springframework.jdbc.support.JdbcUtils; //導入依賴的package包/類
/**
 * @return the current database url and user name (for a given Datasource)
 * @throws RuntimeException when the database metadata cannot be retrieved
 */
public static String getDatabaseURLAndUserName(DataSource dataSource) {
    DatabaseMetaDataCallback callback = new DatabaseMetaDataCallback() {
        public Object processMetaData(DatabaseMetaData dbmd) throws SQLException, MetaDataAccessException {
            String url = dbmd.getURL();
            String userName = dbmd.getUserName();

            StringBuilder s = new StringBuilder(url);
            s.append(" (userName='");
            s.append(userName);
            s.append("')");
            return s.toString();
        }
    };
    try {
        return (String) JdbcUtils.extractDatabaseMetaData(dataSource, callback);
    } catch (Exception e) {
        throw new RuntimeException("unable to read database metadata", e);
    }
}
 
開發者ID:major2015,項目名稱:easyrec_major,代碼行數:24,代碼來源:DaoUtils.java


注:本文中的org.springframework.jdbc.support.JdbcUtils類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。