本文整理匯總了Java中java.sql.ResultSetMetaData.getColumnName方法的典型用法代碼示例。如果您正苦於以下問題:Java ResultSetMetaData.getColumnName方法的具體用法?Java ResultSetMetaData.getColumnName怎麽用?Java ResultSetMetaData.getColumnName使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.sql.ResultSetMetaData
的用法示例。
在下文中一共展示了ResultSetMetaData.getColumnName方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: mapColumnsToProperties
import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
protected int[] mapColumnsToProperties(ResultSetMetaData rsmd, PropertyDescriptor[] props)
throws SQLException
{
int cols = rsmd.getColumnCount();
int[] columnToProperty = new int[cols + 1];
Arrays.fill(columnToProperty, -1);
for (int col = 1; col <= cols; col++)
{
String columnName = rsmd.getColumnLabel(col);
if ((null == columnName) || (0 == columnName.length())) {
columnName = rsmd.getColumnName(col);
}
String propertyName = (String)this.columnToPropertyOverrides.get(columnName);
if (propertyName == null) {
propertyName = columnName;
}
for (int i = 0; i < props.length; i++) {
if (propertyName.equalsIgnoreCase(props[i].getName()))
{
columnToProperty[col] = i;
break;
}
}
}
return columnToProperty;
}
示例2: testUnicodeColumnNames
import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
@Test public void testUnicodeColumnNames() throws Exception {
final String tableName = "unicodeColumn";
final String columnName = "\u041d\u043e\u043c\u0435\u0440\u0422\u0435\u043b"
+ "\u0435\u0444\u043e\u043d\u0430"; // PhoneNumber in Russian
ConnectionSpec.getDatabaseLock().lock();
try (Connection conn = getLocalConnection();
Statement stmt = conn.createStatement()) {
assertFalse(stmt.execute("DROP TABLE IF EXISTS " + tableName));
final String sql = "CREATE TABLE " + tableName + "(" + columnName + " integer)";
assertFalse(stmt.execute(sql));
final ResultSet results = stmt.executeQuery("SELECT * FROM " + tableName);
assertNotNull(results);
ResultSetMetaData metadata = results.getMetaData();
assertNotNull(metadata);
String actualColumnName = metadata.getColumnName(1);
// HSQLDB is going to upper-case the column name
assertEquals(columnName.toUpperCase(Locale.ROOT), actualColumnName);
} finally {
ConnectionSpec.getDatabaseLock().unlock();
}
}
示例3: readResultSet
import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
private List<Object[]> readResultSet(ResultSet rs) throws SQLException {
if (rs == null) {
return null;
}
List<Object[]> array = new ArrayList<Object[]>();
ResultSetMetaData metadata = rs.getMetaData();
int nColumn = metadata.getColumnCount();
//��ӱ�ͷ
Object[] columnNames = new Object[nColumn];
for (int i = 1; i <= nColumn; i++) {
columnNames[i - 1] = metadata.getColumnName(i);
}
array.add(columnNames);
//��ȡ����
while (rs.next()) {
Object[] row = new Object[nColumn];
for (int i = 0; i < nColumn; i++) {
row[i] = rs.getObject(i + 1);
}
array.add(row);
}
return array;
}
示例4: fill
import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
boolean fill(ResultSetMetaData mt, int nColId) // Fill from the meta data of a result set
{
try
{
m_csColName = mt.getColumnName(nColId);
m_nTypeId = mt.getColumnType(nColId);
m_nPrecision = mt.getPrecision(nColId);
m_nScale = mt.getScale(nColId);
return true;
}
catch (SQLException e)
{
Log.logCritical("Exception catched While filling DB table's Column Description:" + e.toString());
}
return false;
}
示例5: select
import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
@RequestMapping("/commit")
public List<Map<String, String>> select() {
List<Map<String, String>> results = new ArrayList<>();
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM INFORMATION_SCHEMA.COLUMNS")) {
ResultSetMetaData metaData = resultSet.getMetaData();
while (resultSet.next()) {
Map<String, String> result = new HashMap<>();
for (int i = 0; i < metaData.getColumnCount(); i++) {
String columnName = metaData.getColumnName(i + 1);
result.put(columnName, resultSet.getString(columnName));
}
results.add(result);
}
connection.commit();
}
catch (Exception e) {
throw new IllegalStateException(e);
}
return results;
}
示例6: rollback
import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
@RequestMapping("/rollback")
public List<Map<String, String>> rollback() {
List<Map<String, String>> results = new ArrayList<>();
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM INFORMATION_SCHEMA.COLUMNS")) {
ResultSetMetaData metaData = resultSet.getMetaData();
while (resultSet.next()) {
Map<String, String> result = new HashMap<>();
for (int i = 0; i < metaData.getColumnCount(); i++) {
String columnName = metaData.getColumnName(i + 1);
result.put(columnName, resultSet.getString(columnName));
}
results.add(result);
}
connection.rollback();
}
catch (Exception e) {
throw new IllegalStateException(e);
}
return results;
}
示例7: getColumnInfo
import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
/**
* Returns the array of column information from the result-set meta-data
* @param metaData the result set meta data
* @param platform the database platform
* @param request the request descriptor
* @return the array of column information
* @throws SQLException if there is a database access error
*/
private List<ColumnInfo> getColumnInfo(ResultSetMetaData metaData, SQLPlatform platform, DbSourceOptions<R> request) throws SQLException {
final int rowCapacity = request.getRowCapacity();
final int columnCount = metaData.getColumnCount();
final List<ColumnInfo> columnInfoList = new ArrayList<>(columnCount);
final SQLType.TypeResolver typeResolver = SQLType.getTypeResolver(platform);
for (int i=0; i<columnCount; ++i) {
final int colIndex = i+1;
final String colName = metaData.getColumnName(colIndex);
if (!request.getExcludeColumnSet().contains(colName)) {
final int typeCode = metaData.getColumnType(colIndex);
final String typeName = metaData.getColumnTypeName(colIndex);
final SQLType sqlType = typeResolver.getType(typeCode, typeName);
final SQLExtractor extractor = request.getExtractors().getOrDefault(colName, SQLExtractor.with(sqlType.typeClass(), platform));
columnInfoList.add(new ColumnInfo(i, colIndex, colName, rowCapacity, extractor));
}
}
return columnInfoList;
}
示例8: toString
import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
private static String toString(ResultSetMetaData metadata, int colNum) throws SQLException {
return "ResultSetMetaData(" + colNum + ")[" +
"columnName='" + metadata.getColumnName(colNum) + '\'' +
", type='" + metadata.getColumnType(colNum) + '\'' +
", nullable=" + metadata.isNullable(colNum) +
", displaySize=" + metadata.getColumnDisplaySize(colNum) +
", precision=" + metadata.getPrecision(colNum) +
", scale=" + metadata.getScale(colNum) +
", signed=" + metadata.isSigned(colNum) +
", className='" + metadata.getColumnClassName(colNum) + '\'' +
']';
}
示例9: getColumnAdapters
import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
/**
* Returns a apply of column type info for the target table
* @param frame the DataFrame reference
* @return the apply of column type info
*/
@SuppressWarnings("unchecked")
private List<ColumnAdapter> getColumnAdapters(DataFrame<R,C> frame, DbSinkOptions<R,C> options) {
final Connection conn = options.getConnection();
final String tableName = options.getTableName();
final SQLPlatform platform = options.getPlatform().orElseThrow(() -> new IllegalStateException("No SQL platform specified in options"));
final Map<C,String> columnMap1 = frame.cols().keys().collect(Collectors.toMap(c -> c, c -> options.getColumnNames().apply(c)));
final Map<String,C> columnMap2 = Collect.reverse(columnMap1);
try (Statement stmt = conn.createStatement()) {
final String sql = "select * from \"" + tableName + "\" where 1=2";
final List<ColumnAdapter> columnList = new ArrayList<>();
final ResultSetMetaData metaData = stmt.executeQuery(sql).getMetaData();
final SQLType.TypeResolver typeResolver = SQLType.getTypeResolver(platform);
for (int i=0; i<metaData.getColumnCount(); ++i) {
final String sqlColName = metaData.getColumnName(i+1);
final int sqlTypeCode = metaData.getColumnType(i+1);
final String sqlTypeName = metaData.getColumnTypeName(i+1);
final SQLType sqlType = typeResolver.getType(sqlTypeCode, sqlTypeName);
if (options.getRowKeyColumn().map(name -> name.equals(sqlColName)).orElse(false)) {
columnList.add(new RowKeyAdapter(sqlColName, sqlType, options));
} else if (options.getAutoIncrementColumnName().map(name -> !name.equalsIgnoreCase(sqlColName)).orElse(true)) {
final C colKey = columnMap2.get(sqlColName);
final Class<?> dataType = frame.cols().type(colKey);
final DataFrameCursor<R,C> cursor = frame.cursor().atColKey(colKey);
final Function1<DataFrameValue<R,C>,?> mapper = options.getColumnMappings().getMapper(dataType);
columnList.add(new ValueAdapter(sqlColName, sqlType, cursor, mapper));
}
}
return columnList;
} catch (Exception ex) {
throw new DataFrameException("Failed to resolve SQL column types for table " + tableName, ex);
}
}
示例10: printAllItems
import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
/**FOR DEBUG PURPOSES
* Lists all items in the database
* @throws SQLException */
public static void printAllItems() throws SQLException
{
//Create a query to fetch all items
DatabaseConnection connection = new DatabaseConnection();
String query = "SELECT * FROM ITEMS";
try
{
//Execute our query
ResultSet rs = connection.executeQuery(query);
ResultSetMetaData metaData = rs.getMetaData();
//Display all results from our query
while (rs.next())
{
String row = "";
for(int i = 1; i <= metaData.getColumnCount(); i++)
{
row += metaData.getColumnName(i) + ": ";
row += rs.getString(i);
if (i < metaData.getColumnCount())
row += ", ";
}
System.out.println(row);
}
}
catch (SQLException e)
{
//Throw a SQL exception if we run into one
throw e;
}
finally
{
//Close the connection regardless of whether we encountered an exception
connection.close();
}
}
示例11: queryMap
import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
/**
* 查詢返回一個map
* @param Connection 數據庫鏈接
* @param sql
* @param entity
* @return
*/
public static Map<String, Object> queryMap(Connection connection, String sql) {
Statement statement = null;
Map<String, Object> result = new HashMap<>();
try{
statement = connection.createStatement();
ResultSet rs = statement.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
while (rs.next()) {
int cols = rsmd.getColumnCount();
for (int i = 1; i <= cols; i++)
{
String columnName = rsmd.getColumnLabel(i);
if ((null == columnName) || (0 == columnName.length())) {
columnName = rsmd.getColumnName(i);
}
result.put(columnName, rs.getObject(i));
}
break;
}
}catch(Exception e) {
logger.error("DbUtils queryMap failed", e);
}finally {
if (connection != null) {
try{
connection.close();
}catch(Exception e2) {
logger.error("DbUtils queryMap failed", e2);
}
}
}
return result;
}
示例12: returnSimpleResult
import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
/**
* 數據庫查詢操作,返回單條記錄
* @param sql語句
* @param 傳入的占位符
* @return 返回Map集合類型,包含查詢的結果
* @throws SQLException
*/
public Map<String,Object> returnSimpleResult(String sql,List<Object>params)
{
Map<String, Object> map = new HashMap<String, Object>();
int index = 1;//從1開始設置占位符
try {
pStatement = connection.prepareStatement(sql);
if(params != null && !params.isEmpty()) /*判斷參數是否為空*/
{
for(int i = 0;i<params.size();i++) /*循環填充占位符*/
{
pStatement.setObject(index++, params.get(i));
}
}
// System.out.println(pStatement.toString());
resultset = pStatement.executeQuery(sql);
/* 將查詢結果封裝到map集合*/
ResultSetMetaData metaDate = resultset.getMetaData();//獲取resultSet列的信息
int columnLength = metaDate.getColumnCount();//獲得列的長度
while(resultset.next())
{
for(int i = 0;i<columnLength;i++)
{
String metaDateKey = metaDate.getColumnName(i+1);//獲得列名
Object resultsetValue = resultset.getObject(metaDateKey);//通過列名獲得值
if(resultsetValue == null)
{
resultsetValue = "";//轉成String類型
}
map.put(metaDateKey, resultsetValue);//添加到map集合(以上代碼是為了將從數據庫返回的值轉換成map的key和value)
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return map;
}
示例13: printAllUsers
import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
/**FOR DEBUG PURPOSES
* Lists all users in the database
* @throws SQLException */
public static void printAllUsers() throws SQLException
{
//Create query to fetch all users
DatabaseConnection connection = new DatabaseConnection();
String query = "SELECT * FROM USERS";
try
{
//Execute our query
ResultSet rs = connection.executeQuery(query);
ResultSetMetaData metaData = rs.getMetaData();
//Display all results from our query
while (rs.next())
{
String row = "";
for(int i = 1; i <= metaData.getColumnCount(); i++)
{
row += metaData.getColumnName(i) + ": ";
row += rs.getString(i);
if (i < metaData.getColumnCount())
row += ", ";
}
System.out.println(row);
}
}
catch (SQLException e)
{
//Throw a SQL exception if we run into one
throw e;
}
finally
{
//Close the connection regardless of whether we encountered an exception
connection.close();
}
}
示例14: getColName
import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
private String getColName(int nColSourceIndex)
{
try
{
ResultSetMetaData rsMetaData = m_r.getMetaData();
String csColName = rsMetaData.getColumnName(nColSourceIndex);
return csColName;
}
catch (SQLException e)
{
LogSQLException.log(e);
}
return "";
}
示例15: QueryResult
import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
public QueryResult(ResultSet resultSet) {
if (resultSet == null) {
return;
}
try {
ResultSetMetaData md = resultSet.getMetaData();
for (int i = 0; i < md.getColumnCount(); i++) {
int index = i + 1;
VariableDescriptor desc = new VariableDescriptor(
md.getColumnName(index),
md.getColumnLabel(index),
md.getTableName(index)
);
variableDescriptors.add(desc);
}
while (resultSet.next()) {
List<Object> row = new ArrayList<>();
for (int i = 0; i < md.getColumnCount(); i++) {
Object value = resultSet.getObject(i + 1);
row.add(value);
}
rows.add(new DataRow(variableDescriptors, row));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}