本文整理汇总了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);
}
}