本文整理汇总了Java中java.sql.ResultSet.getMetaData方法的典型用法代码示例。如果您正苦于以下问题:Java ResultSet.getMetaData方法的具体用法?Java ResultSet.getMetaData怎么用?Java ResultSet.getMetaData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.sql.ResultSet
的用法示例。
在下文中一共展示了ResultSet.getMetaData方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testUnicodeColumnNames
import java.sql.ResultSet; //导入方法依赖的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();
}
}
示例2: createAttributes
import java.sql.ResultSet; //导入方法依赖的package包/类
public static List<Attribute> createAttributes(ResultSet rs) throws SQLException {
LinkedList attributes = new LinkedList();
if(rs == null) {
throw new IllegalArgumentException("Cannot create attributes: ResultSet must not be null!");
} else {
ResultSetMetaData metadata;
try {
metadata = rs.getMetaData();
} catch (NullPointerException var7) {
throw new RuntimeException("Could not create attribute list: ResultSet object seems closed.");
}
int numberOfColumns = metadata.getColumnCount();
for(int column = 1; column <= numberOfColumns; ++column) {
String name = metadata.getColumnLabel(column);
Attribute attribute = AttributeFactory.createAttribute(name, getRapidMinerTypeIndex(metadata.getColumnType(column)));
attributes.add(attribute);
}
return attributes;
}
}
示例3: ensureEqualContent
import java.sql.ResultSet; //导入方法依赖的package包/类
/**
* ensures two tables (or views, that is) have the same content
*/
private void ensureEqualContent(String tableNameLHS,
String tableNameRHS) throws SQLException {
ResultSet lhs = m_statement.executeQuery("SELECT * FROM \""
+ tableNameLHS + "\"");
ResultSet rhs = m_statement.executeQuery("SELECT * FROM \""
+ tableNameRHS + "\"");
ResultSetMetaData meta = lhs.getMetaData();
while (lhs.next() && rhs.next()) {
for (int col = 1; col <= meta.getColumnCount(); ++col) {
assertEquals("table content does not match: cp. "
+ tableNameLHS + "-" + tableNameRHS + ", row "
+ lhs.getRow() + ", col "
+ col, lhs.getObject(col), rhs.getObject(col));
}
}
// lhs should be after last, rhs still on last
assertTrue("row count does not match: " + tableNameLHS + "-"
+ tableNameRHS, lhs.isAfterLast() && rhs.isLast());
}
示例4: getTotal
import java.sql.ResultSet; //导入方法依赖的package包/类
/**
* 得到结果集总数
* @param ps
* @return
* @throws SQLException
*/
private long getTotal(PreparedStatement ps) throws SQLException {
long total=-1;
ResultSet rs=null;
ResultSetMetaData rsmd=null;
String columnName=null;
while(ps.getMoreResults()){
rs= ps.getResultSet();
rsmd = rs.getMetaData();
for (int i = 0, n = rsmd.getColumnCount(); i < n; i++) {
columnName=rsmd.getColumnLabel(i + 1);
if(columnName.equalsIgnoreCase(COLUMN_TOTAL_NAME)){
while (rs.next()) {
total=rs.getLong(columnName);
}
break;
}
}
}
return total;
}
示例5: testInfoSchemaTablesZeroRowsBy_TABLE_NAME_works
import java.sql.ResultSet; //导入方法依赖的package包/类
/** (Failed before (because TABLE_NAME test is pushed down).) */
@Test
public void testInfoSchemaTablesZeroRowsBy_TABLE_NAME_works()
throws Exception {
Statement stmt = connection.createStatement();
ResultSet results =
stmt.executeQuery(
"SELECT * FROM INFORMATION_SCHEMA.`TABLES` WHERE TABLE_NAME = ''" );
// Result set should still have columns even though there are no rows:
ResultSetMetaData metadata = results.getMetaData();
assertThat( "ResultSetMetaData.getColumnCount() should have been > 0",
metadata.getColumnCount(), not( equalTo( 0 ) ) );
assertThat( "Unexpected non-empty results. Test rot?",
false, equalTo( results.next() ) );
}
示例6: executeQuery
import java.sql.ResultSet; //导入方法依赖的package包/类
public ResultList executeQuery(String sqlStatement) throws SQLExecutionException
{
ResultSet resultSet;
ResultList resultList;
try {
Statement stmt = connection.createStatement();
resultSet = stmt.executeQuery(sqlStatement);
ResultSetMetaData rsMetadata = resultSet.getMetaData();
resultList = convert(resultSet, rsMetadata.getColumnCount());
resultSet.close();
stmt.close();
}
catch (SQLException e) {
throw new SQLExecutionException(sqlStatement);
}
return resultList;
}
示例7: shouldExecuteParametrisedQuery
import java.sql.ResultSet; //导入方法依赖的package包/类
/**
* Checks if parametrised query execution is working correctly.
*/
@Test
public void shouldExecuteParametrisedQuery() {
SqlScriptExecutor executor = sqlScriptExecutorProvider.get(new LoggingSqlScriptVisitor());
SelectStatement testSelect = select(field("alfaDate1"), field("alfaDate2"), literal(123))
.from(tableRef("DateTable")).where(eq(field("alfaDate1"), parameter("firstDateParam").type(DataType.BIG_INTEGER)));;
Iterable<SqlParameter> parameterMetadata = ImmutableList.of(parameter(column("firstDateParam", DataType.STRING)));
RecordBuilder parameterData = DataSetUtils.record().setString("firstDateParam", "20040609");
ResultSetProcessor<List<List<String>>> resultSetProcessor = new ResultSetProcessor<List<List<String>>>() {
/**
* Takes all rows and puts into two-dimension String array.
*/
@Override
public List<List<String>> process(ResultSet resultSet) throws SQLException {
Builder<List<String>> builder = ImmutableList.<List<String>>builder();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
while (resultSet.next()) {
List<String> rowBuilder = new LinkedList<>();
for (int columnNumber = 1; columnNumber < columnCount + 1; columnNumber++) {
String stringifiezedCell = resultSet.getString(columnNumber);
rowBuilder.add(stringifiezedCell);
}
builder.add(rowBuilder);
}
return builder.build();
}
};
List<List<String>> result = executor.executeQuery(testSelect, parameterMetadata, parameterData, connection, resultSetProcessor);
assertEquals(ImmutableList.of(ImmutableList.of("20040609","20040813", "123"), ImmutableList.of("20040609","20040609", "123") , ImmutableList.of("20040609","20040610", "123")), result);
}
示例8: dumpTable
import java.sql.ResultSet; //导入方法依赖的package包/类
private static void dumpTable(String tn) throws SQLException {
Connection conn = getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from " + tn);
ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
out.println();
out.println("****************************************");
out.println("DUMP FOR TABLE: " + tn);
out.println("****************************************");
out.flush();
while (rs.next()) {
out.print("[");
for (int i = 1; i <= count; i++) {
out.print(rs.getString(i));
if (i < count) {
out.print(" : ");
}
}
out.println("]");
}
out.println();
out.flush();
rs.close();
stmt.close();
conn.close();
}
示例9: checkReturnedColumnsForBug44451
import java.sql.ResultSet; //导入方法依赖的package包/类
private void checkReturnedColumnsForBug44451(String stepDescription, String methodName, List<String> expectedFields, ResultSet resultSetToCheck)
throws Exception {
ResultSetMetaData rsMetaData = resultSetToCheck.getMetaData();
int numberOfColumns = rsMetaData.getColumnCount();
assertEquals(stepDescription + ", wrong column count in method '" + methodName + "'.", expectedFields.size(), numberOfColumns);
for (int i = 0; i < numberOfColumns; i++) {
int position = i + 1;
assertEquals(stepDescription + ", wrong column at position '" + position + "' in method '" + methodName + "'.", expectedFields.get(i),
rsMetaData.getColumnName(position));
}
rs.close();
}
示例10: checkTypes
import java.sql.ResultSet; //导入方法依赖的package包/类
private void checkTypes(ResultSet rsToCheck, int[] types) throws Exception {
ResultSetMetaData rsmd = rsToCheck.getMetaData();
assertEquals(types.length, rsmd.getColumnCount());
for (int i = 0; i < types.length; i++) {
String expectedType = TYPES_MAP.get(new Integer(types[i]));
String actualType = TYPES_MAP.get(new Integer(rsmd.getColumnType(i + 1)));
assertNotNull(expectedType);
assertNotNull(actualType);
assertEquals("Unexpected type in column " + (i + 1), expectedType, actualType);
}
}
示例11: testTables
import java.sql.ResultSet; //导入方法依赖的package包/类
@Test public void testTables() throws Exception {
final Connection connection = getMockConnection();
final ResultSet resultSet =
connection.getMetaData().getTables(null, null, null, new String[0]);
assertFalse(resultSet.next());
final ResultSetMetaData metaData = resultSet.getMetaData();
assertTrue(metaData.getColumnCount() >= 3);
assertEquals("TABLE_CAT", metaData.getColumnName(1));
assertEquals("TABLE_SCHEM", metaData.getColumnName(2));
assertEquals("TABLE_NAME", metaData.getColumnName(3));
resultSet.close();
connection.close();
}
示例12: queryForList
import java.sql.ResultSet; //导入方法依赖的package包/类
/**
* Query a single record
*
* @param sql
* @param args
* @return List<Map<String,Object>>
*/
public static List<Map<String, Object>> queryForList(String sql, Object... args) {
List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
Connection con = null;
ResultSet rs = null;
PreparedStatement ps = null;
try {
con = getconnnection();
ps = con.prepareStatement(sql);
if (args != null) {
for (int i = 0; i < args.length; i++) {
ps.setObject((i + 1), args[i]);
}
}
rs = ps.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
while (rs.next()) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 1; i <= columnCount; i++) {
map.put(rsmd.getColumnLabel(i), rs.getObject(i));
}
result.add(map);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(rs, ps, con);
}
return result;
}
示例13: parse
import java.sql.ResultSet; //导入方法依赖的package包/类
public static DataTable parse(ResultSet resultSet) throws SQLException {
ResultSetMetaData metaData = resultSet.getMetaData();
DataTable dataTable = new DataTable();
dataTable.columns = new ColumnCollection();
int colCount = metaData.getColumnCount();
for (int i = 1; i <= colCount; i++) {
Column column = new Column();
column.setCatalogName(metaData.getCatalogName(i));
column.setColumnClassName(metaData.getColumnClassName(i));
column.setDisplaySize(metaData.getColumnDisplaySize(i));
column.setLabel(metaData.getColumnLabel(i));
column.setName(metaData.getColumnName(i));
column.setPrecision(metaData.getPrecision(i));
column.setScale(metaData.getScale(i));
column.setSchemaName(metaData.getSchemaName(i));
column.setTableName(metaData.getTableName(i));
column.setType(metaData.getColumnType(i));
column.setTypeName(metaData.getColumnTypeName(i));
dataTable.columns.add(column);
}
while (resultSet.next()) {
Object[] data = new Object[colCount];
for (int i = 1; i <= data.length; i++) {
data[i - 1] = resultSet.getObject(i);
}
dataTable.add(new Row(dataTable, data));
}
resultSet.close();
return dataTable;
}
示例14: convert
import java.sql.ResultSet; //导入方法依赖的package包/类
public <T> T convert(ResultSet rs, Class<T> cls) throws SQLException {
T obj = null;
boolean isNull = true;
try {
ResultSetMetaData md = rs.getMetaData();
List<String> list = new ArrayList<String>();
for (int j = 1; j <= md.getColumnCount(); j++) {
list.add(md.getColumnName(j));
}
obj = cls.newInstance();
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
String fieldName = field.getName();
String columnName = convertColumnName(fieldName);
if (list.contains(columnName)) {
isNull = false;
@SuppressWarnings("rawtypes")
Class fieldType = field.getType();
field.setAccessible(true);
field.set(obj, convertColumnValue(fieldType.getName(), columnName, rs));
}
}
}
catch (Exception e) {
// logger.error("Convert DB Data FAIL", e);
}
return isNull ? null : obj;
}
示例15: checkStatementExecute
import java.sql.ResultSet; //导入方法依赖的package包/类
private void checkStatementExecute(Connection connection,
boolean prepare, int maxRowCount) throws SQLException {
final String sql = "select * from (\n"
+ " values (1, 'a'), (null, 'b'), (3, 'c')) as t (c1, c2)";
final Statement statement;
final ResultSet resultSet;
final ParameterMetaData parameterMetaData;
if (prepare) {
final PreparedStatement ps = connection.prepareStatement(sql);
statement = ps;
ps.setMaxRows(maxRowCount);
parameterMetaData = ps.getParameterMetaData();
assertTrue(ps.execute());
resultSet = ps.getResultSet();
} else {
statement = connection.createStatement();
statement.setMaxRows(maxRowCount);
parameterMetaData = null;
assertTrue(statement.execute(sql));
resultSet = statement.getResultSet();
}
if (parameterMetaData != null) {
assertThat(parameterMetaData.getParameterCount(), equalTo(0));
}
final ResultSetMetaData metaData = resultSet.getMetaData();
assertEquals(2, metaData.getColumnCount());
assertEquals("C1", metaData.getColumnName(1));
assertEquals("C2", metaData.getColumnName(2));
for (int i = 0; i < maxRowCount || (maxRowCount == 0 && i < 3); i++) {
assertTrue(resultSet.next());
}
assertFalse(resultSet.next());
resultSet.close();
statement.close();
connection.close();
}