Java 获取 JDBC 列类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18041697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the JDBC column types
提问by Salah
I need to ask about my column jdbc type in a table, today I am looping throw my columns and then asking about its type, see my code below :
我需要在表中询问我的列 jdbc 类型,今天我循环抛出我的列然后询问它的类型,请参阅下面的代码:
public int getColumnDataTypeFromDB(String columnName) {
int datatype = 0;
ResultSet columns = null;
try {
String schema =
getMetaDataCatalogName() != null
? getMetaDataCatalogName()
: getMetaDataSchemaName();
if (TableManagerFactory.isCatalogBasedDatabase()) {
columns =
getMetaData().getColumns(
schema,
null,
tableName,
columnName);
} else {
columns =
getMetaData().getColumns(
null,
schema,
tableName,
columnName);
}
// columns =
// getMetaData().getColumns(getMetaDataCatalogName(), getMetaDataSchemaName(), tableName, columnName);
if (columns.next()) {
datatype = columns.getInt("DATA_TYPE");
}
} catch (SQLException ex) {
Log.error(
this,
"Error while getting columns information: " + ex.getMessage(),
ex);
//return false;
} catch (DDLCreationException ex) {
Log.error(this, ex.getMessage());
} finally {
try {
if (columns != null) {
columns.close();
}
} catch (SQLException ex) {
Log.error(this, ex.getLocalizedMessage());
}
}
return datatype;
}
Can I get all the columns metadata in such a table at the same time ? if yes, how can I do it ??
我可以同时获取此类表中的所有列元数据吗?如果是,我该怎么做?
采纳答案by Mark Rotteveel
The parameters to DatabaseMetaData.getColumns(..)
are supposed to be a LIKE
-pattern. So if you want to get all columns from a table, you simply need to pass in "%"
to the last parameter, columnNamePattern
:
to 的参数DatabaseMetaData.getColumns(..)
应该是一个LIKE
-pattern。所以如果你想从一个表中获取所有列,你只需要传入"%"
最后一个参数,columnNamePattern
:
getMetaData().getColumns(null, schema, tableName, "%");
Some drivers (also) allow null
here, but not all drivers do that (the JDBC specification and API documentation is not entirely clear whether that is allowed or not for this parameter)
某些驱动程序(也)允许null
此处,但并非所有驱动程序都这样做(JDBC 规范和 API 文档并不完全清楚此参数是否允许)
回答by Shamim Ahmmed
Use jdbc ResultSetMetaData class to get the detail information of the table columns.
使用 jdbc ResultSetMetaData 类获取表列的详细信息。
ResultSet res=stmt.executeQuery("select * from tableName where 1<0");
ResultSetMetaData rsmd=res.getMetaData();
rsmd.getColumnType(1);
rsmd.getColumnLabel(1);
rsmd.getColumnDisplaySize(1);
回答by saub90
It seems you are looking for data type of column then getColumnTypeName would the one
看来您正在寻找列的数据类型,然后 getColumnTypeName 将是那个
System.out.println(rsMaster.getMetaData().getColumnName(1));
System.out.println(rsMaster.getMetaData().getColumnType(1));
System.out.println(rsMaster.getMetaData().getColumnTypeName(1));
System.out.println(rsMaster.getMetaData().getColumnDisplaySize(1));
System.out.println(rsMaster.getMetaData().getColumnLabel(1));