本文整理匯總了Java中java.sql.ResultSetMetaData.getColumnTypeName方法的典型用法代碼示例。如果您正苦於以下問題:Java ResultSetMetaData.getColumnTypeName方法的具體用法?Java ResultSetMetaData.getColumnTypeName怎麽用?Java ResultSetMetaData.getColumnTypeName使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.sql.ResultSetMetaData
的用法示例。
在下文中一共展示了ResultSetMetaData.getColumnTypeName方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testTypes
import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
/**
* Tests for types being returned correctly
*
* @throws Exception
* if an error occurs.
*/
public void testTypes() throws Exception {
try {
this.stmt.execute("DROP TABLE IF EXISTS typesRegressTest");
this.stmt.execute("CREATE TABLE typesRegressTest (varcharField VARCHAR(32), charField CHAR(2), enumField ENUM('1','2'),"
+ "setField SET('1','2','3'), tinyblobField TINYBLOB, mediumBlobField MEDIUMBLOB, longblobField LONGBLOB, blobField BLOB)");
this.rs = this.stmt.executeQuery("SELECT * from typesRegressTest");
ResultSetMetaData rsmd = this.rs.getMetaData();
int numCols = rsmd.getColumnCount();
for (int i = 0; i < numCols; i++) {
String columnName = rsmd.getColumnName(i + 1);
String columnTypeName = rsmd.getColumnTypeName(i + 1);
System.out.println(columnName + " -> " + columnTypeName);
}
} finally {
this.stmt.execute("DROP TABLE IF EXISTS typesRegressTest");
}
}
示例2: mapRow
import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
/**
* Extract a value for the single column in the current row.
* <p>Validates that there is only one column selected,
* then delegates to {@code getColumnValue()} and also
* {@code convertValueToRequiredType}, if necessary.
* @see java.sql.ResultSetMetaData#getColumnCount()
* @see #getColumnValue(java.sql.ResultSet, int, Class)
* @see #convertValueToRequiredType(Object, Class)
*/
@Override
@SuppressWarnings("unchecked")
public T mapRow(ResultSet rs, int rowNum) throws SQLException {
// Validate column count.
ResultSetMetaData rsmd = rs.getMetaData();
int nrOfColumns = rsmd.getColumnCount();
if (nrOfColumns != 1) {
throw new IncorrectResultSetColumnCountException(1, nrOfColumns);
}
// Extract column value from JDBC ResultSet.
Object result = getColumnValue(rs, 1, this.requiredType);
if (result != null && this.requiredType != null && !this.requiredType.isInstance(result)) {
// Extracted value does not match already: try to convert it.
try {
return (T) convertValueToRequiredType(result, this.requiredType);
}
catch (IllegalArgumentException ex) {
throw new TypeMismatchDataAccessException(
"Type mismatch affecting row number " + rowNum + " and column type '" +
rsmd.getColumnTypeName(1) + "': " + ex.getMessage());
}
}
return (T) result;
}
示例3: 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;
}
示例4: queryColumns
import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
/**
* 描述: 查詢數據表字段名(key:字段名,value:字段類型名)
* 時間: 2017年11月15日 上午11:29:32
* @author yi.zhang
* @param table 表名
* @return
*/
public Map<String,String> queryColumns(String table){
try {
if(connect==null){
init(driverName, url, username, password, isDruid, max_pool_size, init_pool_size);
}
String sql = "select * from "+table;
PreparedStatement ps = connect.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
Map<String,String> reflect = new HashMap<String,String>();
for(int i=1;i<=count;i++){
String column = rsmd.getColumnName(i);
String type = rsmd.getColumnTypeName(i);
reflect.put(column, type);
}
rs.close();
ps.close();
return reflect;
} catch (Exception e) {
logger.error("-----Columns excute query Error-----", e);
}
return null;
}
示例5: logDebugInfoForDBValue
import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
/**
* Trace with Debug severity info about retrieved value such as the DB and JDBC type
* @param value value already got from the database
* @param index the column index (starting from 1) of the cell
* @param resultSet needed for extra
* @throws SQLException
*/
protected void logDebugInfoForDBValue( Object value, int index,
ResultSet resultSet ) throws SQLException {
if (log.isDebugEnabled()) {
// trace column type too
ResultSetMetaData metaData = resultSet.getMetaData();
String dbType = metaData.getColumnTypeName(index);
int javaType = metaData.getColumnType(index);
log.debug("DB value is '" + value + "' (retrieved as " + value.getClass().getSimpleName()
+ "), JDBC type " + javaType + ", DB type " + dbType);
}
}
示例6: 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);
}
}
示例7: mapRow
import java.sql.ResultSetMetaData; //導入方法依賴的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);
if("NUMBER".equals(typename)){
int scale = rsmd.getScale(i);
int precision = rsmd.getPrecision(i);
if(scale == 0){
if(precision<10)
obj = rs.getInt(i);
else
obj = rs.getLong(i);
}else if(scale>0){
obj = rs.getDouble(i);
}else
obj = rs.getLong(i);
}else{
obj = getColumnValue(rs, i);
}
mapOfColValues.put(key, obj);
}
return mapOfColValues;
}
示例8: testAllFieldsForNull
import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
@SuppressWarnings("deprecation")
private void testAllFieldsForNull(ResultSet rsToTest) throws Exception {
ResultSetMetaData rsmd = this.rs.getMetaData();
int numCols = rsmd.getColumnCount();
while (rsToTest.next()) {
for (int i = 0; i < numCols - 1; i++) {
String typeName = rsmd.getColumnTypeName(i + 1);
if ("VARBINARY".equalsIgnoreCase(typeName)) {
System.out.println();
}
if (!"BIT".equalsIgnoreCase(typeName)) {
assertEquals(false, rsToTest.getBoolean(i + 1));
assertTrue("for type " + typeName, rsToTest.wasNull());
assertEquals(0, rsToTest.getDouble(i + 1), 0 /* delta */);
assertTrue("for type " + typeName, rsToTest.wasNull());
assertEquals(0, rsToTest.getFloat(i + 1), 0 /* delta */);
assertTrue("for type " + typeName, rsToTest.wasNull());
assertEquals(0, rsToTest.getInt(i + 1));
assertTrue("for type " + typeName, rsToTest.wasNull());
assertEquals(0, rsToTest.getLong(i + 1));
assertTrue("for type " + typeName, rsToTest.wasNull());
assertEquals(null, rsToTest.getObject(i + 1));
assertTrue("for type " + typeName, rsToTest.wasNull());
assertEquals(null, rsToTest.getString(i + 1));
assertTrue("for type " + typeName, rsToTest.wasNull());
assertEquals(null, rsToTest.getAsciiStream(i + 1));
assertTrue("for type " + typeName, rsToTest.wasNull());
assertEquals(null, rsToTest.getBigDecimal(i + 1));
assertTrue("for type " + typeName, rsToTest.wasNull());
assertEquals(null, rsToTest.getBinaryStream(i + 1));
assertTrue("for type " + typeName, rsToTest.wasNull());
assertEquals(null, rsToTest.getBlob(i + 1));
assertTrue("for type " + typeName, rsToTest.wasNull());
assertEquals(0, rsToTest.getByte(i + 1));
assertTrue("for type " + typeName, rsToTest.wasNull());
assertEquals(null, rsToTest.getBytes(i + 1));
assertTrue("for type " + typeName, rsToTest.wasNull());
assertEquals(null, rsToTest.getCharacterStream(i + 1));
assertTrue("for type " + typeName, rsToTest.wasNull());
assertEquals(null, rsToTest.getClob(i + 1));
assertTrue("for type " + typeName, rsToTest.wasNull());
assertEquals(null, rsToTest.getDate(i + 1));
assertTrue("for type " + typeName, rsToTest.wasNull());
assertEquals(0, rsToTest.getShort(i + 1));
assertTrue("for type " + typeName, rsToTest.wasNull());
assertEquals(null, rsToTest.getTime(i + 1));
assertTrue("for type " + typeName, rsToTest.wasNull());
assertEquals(null, rsToTest.getTimestamp(i + 1));
assertTrue("for type " + typeName, rsToTest.wasNull());
assertEquals(null, rsToTest.getUnicodeStream(i + 1));
assertTrue("for type " + typeName, rsToTest.wasNull());
assertEquals(null, rsToTest.getURL(i + 1));
assertTrue("for type " + typeName, rsToTest.wasNull());
}
}
}
}
示例9: testAllFieldsForNotNull
import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
@SuppressWarnings("deprecation")
private void testAllFieldsForNotNull(ResultSet rsToTest, List<Boolean> wasDatetimeTypeList) throws Exception {
ResultSetMetaData rsmd = this.rs.getMetaData();
int numCols = rsmd.getColumnCount();
while (rsToTest.next()) {
for (int i = 0; i < numCols - 1; i++) {
boolean wasDatetimeType = wasDatetimeTypeList.get(i).booleanValue();
String typeName = rsmd.getColumnTypeName(i + 1);
int sqlType = rsmd.getColumnType(i + 1);
if (!"BIT".equalsIgnoreCase(typeName) && sqlType != Types.BINARY && sqlType != Types.VARBINARY && sqlType != Types.LONGVARBINARY) {
if (!wasDatetimeType) {
assertEquals(false, rsToTest.getBoolean(i + 1));
assertTrue(!rsToTest.wasNull());
assertEquals(0, rsToTest.getDouble(i + 1), 0 /* delta */);
assertTrue(!rsToTest.wasNull());
assertEquals(0, rsToTest.getFloat(i + 1), 0 /* delta */);
assertTrue(!rsToTest.wasNull());
assertEquals(0, rsToTest.getInt(i + 1));
assertTrue(!rsToTest.wasNull());
assertEquals(0, rsToTest.getLong(i + 1));
assertTrue(!rsToTest.wasNull());
assertEquals(0, rsToTest.getByte(i + 1));
assertTrue(!rsToTest.wasNull());
assertEquals(0, rsToTest.getShort(i + 1));
assertTrue(!rsToTest.wasNull());
}
assertNotNull(rsToTest.getObject(i + 1));
assertTrue(!rsToTest.wasNull());
assertNotNull(rsToTest.getString(i + 1));
assertTrue(!rsToTest.wasNull());
assertNotNull(rsToTest.getAsciiStream(i + 1));
assertTrue(!rsToTest.wasNull());
assertNotNull(rsToTest.getBinaryStream(i + 1));
assertTrue(!rsToTest.wasNull());
assertNotNull(rsToTest.getBlob(i + 1));
assertTrue(!rsToTest.wasNull());
assertNotNull(rsToTest.getBytes(i + 1));
assertTrue(!rsToTest.wasNull());
assertNotNull(rsToTest.getCharacterStream(i + 1));
assertTrue(!rsToTest.wasNull());
assertNotNull(rsToTest.getClob(i + 1));
assertTrue(!rsToTest.wasNull());
String columnClassName = rsmd.getColumnClassName(i + 1);
boolean canBeUsedAsDate = !("java.lang.Boolean".equals(columnClassName) || "java.lang.Double".equals(columnClassName)
|| "java.lang.Float".equals(columnClassName) || "java.lang.Real".equals(columnClassName)
|| "java.math.BigDecimal".equals(columnClassName));
if (canBeUsedAsDate) {
assertNotNull(rsToTest.getDate(i + 1));
assertTrue(!rsToTest.wasNull());
assertNotNull(rsToTest.getTime(i + 1));
assertTrue(!rsToTest.wasNull());
assertNotNull(rsToTest.getTimestamp(i + 1));
assertTrue(!rsToTest.wasNull());
}
assertNotNull(rsToTest.getUnicodeStream(i + 1));
assertTrue(!rsToTest.wasNull());
try {
assertNotNull(rsToTest.getURL(i + 1));
} catch (SQLException sqlEx) {
assertTrue(sqlEx.getMessage().indexOf("URL") != -1);
}
assertTrue(!rsToTest.wasNull());
}
}
}
}
示例10: test004
import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
public void test004() throws Exception {
if (BlancoSfdcJdbcTestConstants.isTestWithSfdc() == false)
return;
Class.forName("blanco.sfdc.jdbc.driver.BlancoSfdcJdbcDriver");
try {
final Properties prop = new Properties();
final InputStream inStream = new FileInputStream("soqlro.properties");
prop.load(new BufferedInputStream(inStream));
inStream.close();
final String url = prop.getProperty("url",
"jdbc:blanco:sfdc:soqlro://login.salesforce.com/services/Soap/u/40.0");
final String user = prop.getProperty("user", "NoUserSpesified");
final String pass = prop.getProperty("password", "NoPassSpecified");
final Connection conn = DriverManager.getConnection(url, user, pass);
final Statement stmt = conn.createStatement();
final String sql = "select Id,BillingCity,BillingState,BillingPostalCode,BillingCountry,BillingLatitude,BillingLongitude,BillingGeocodeAccuracy,BillingAddress from Account";
final ResultSet rs = stmt.executeQuery(sql);
{
final AbstractBlancoGenericJdbcStatement stmt2 = (AbstractBlancoGenericJdbcStatement) stmt;
final String tableName = "GMETA_COLUMNS_" + stmt2.getGlobalUniqueKey();
final AbstractBlancoGenericJdbcConnection conn2 = (AbstractBlancoGenericJdbcConnection) stmt2
.getConnection();
final ResultSet rs2 = conn2.getCacheConnection().createStatement()
.executeQuery("select * from " + tableName);
final ResultSetMetaData rsmd2 = rs2.getMetaData();
for (; rs2.next();) {
for (int colNum2 = 1; colNum2 <= rs2.getMetaData().getColumnCount(); colNum2++) {
rsmd2.getColumnName(colNum2);
rs2.getString(colNum2);
}
}
}
rs.next();
final ResultSetMetaData rsmd = rs.getMetaData();
for (int indexCol = 1; indexCol <= rsmd.getColumnCount(); indexCol++) {
String result = "";
result += rsmd.getColumnName(indexCol) + " (" + rsmd.getColumnTypeName(indexCol) + "): "
+ rsmd.getColumnType(indexCol);
// System.out.println(result);
}
rs.close();
stmt.close();
conn.close();
} catch (Exception ex) {
fail();
}
}
示例11: setTable
import java.sql.ResultSetMetaData; //導入方法依賴的package包/類
/**
* Configures this class, by reading in the structure of the log-table
* Throws an exception, if an database-error occurs !
*
* @param _table
* Description of Parameter
* @exception Exception
* Description of Exception
*/
public void setTable(String _table) throws Exception {
if (isconfigured) { return; }
if (poolConnectionHandler != null) {
con = poolConnectionHandler.getConnection();
if (!isConnected()) { throw new Exception(
"JDBCLogger::setTable(), Given connection isnt connected to database !"); }
}
//Fill logcols with META-informations of the table-columns
stmt = this.createUpdatableStatement();
rs = stmt.executeQuery("SELECT " + _table + ".* FROM " + _table + " WHERE 1 = 2");
JDBCLogColumn logcol;
ResultSetMetaData rsmd = rs.getMetaData();
num = rsmd.getColumnCount();
logcols = new ArrayList(num);
for (int i = 1; i <= num; i++) {
logcol = new JDBCLogColumn();
logcol.name = rsmd.getColumnName(i).toUpperCase();
logcol.sqlType = rsmd.getColumnType(i);
logcol.type = rsmd.getColumnTypeName(i);
logcol.nullable = (rsmd.isNullable(i) == ResultSetMetaData.columnNullable);
logcol.isWritable = rsmd.isWritable(i);
if (!logcol.isWritable) {
logcol.ignore = true;
}
logcols.add(logcol);
}
table = _table;
rs.close();
stmt.close();
freeConnection();
isconfigured = true;
}