jdbc的问题

wxzh 2004-09-16 01:53:14
环境:
数据库:oracle9i
jdbc驱动:ojdbc14.jar

希望在程序中获得表中每列的列名及其说明(comment),代码如下:
...
ResultSet columns = dbMetaData.getColumns(null,dbMetaData.getUserName(),"table","%");
while(columns.next()){
System.out.println(columns.getString("COLUMN_NAME")+":"+columns.getString("REMARKS"));
}

运行结果列名能打印出来,但是说明一直是null。(在数据库中是有说明的)

是哪出问题了?谢谢
...全文
154 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
titaninwinter 2004-09-17
  • 打赏
  • 举报
回复
试试用ResultSetMetaData
wxzh 2004-09-17
  • 打赏
  • 举报
回复
对的,我就是这么个意思,:)
只是columns.getString("REMARKS")的值一直是null
baffling 2004-09-17
  • 打赏
  • 举报
回复
to 楼上:
其实楼主的意思是:
public ResultSet getColumns(String catalog,
String schemaPattern,
String tableNamePattern,
String columnNamePattern)
throws SQLException
返回一个ResultSet,该ResultSet有Column_Name,remarks,TABLE_CAT等字段
所以用columns.getString("COLUMN_NAME")+":"+columns.getString("REMARKS")是可以的啊。
weimenren 2004-09-16
  • 打赏
  • 举报
回复
那你下面用的是

ResultSet.getString("COLUMN_NAME")

你怎么可以用

getColumns(String catalog,
String schemaPattern,
String tableNamePattern,
String columnNamePattern)
中的解释呢?

wxzh 2004-09-16
  • 打赏
  • 举报
回复
我用的是java.sql.ResultSet
weimenren 2004-09-16
  • 打赏
  • 举报
回复
请问楼主是否使用的是

java.sql.ResultSet

weimenren 2004-09-16
  • 打赏
  • 举报
回复
我只知道

ResultSet

getString
public String getString(String columnName)
throws SQLExceptionRetrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language.

Parameters:
columnName - the SQL name of the column
Returns:
the column value; if the value is SQL NULL, the value returned is null
Throws:
SQLException - if a database access error occurs



请问楼主是否将ResultSet.getString(String columnName) 改写了,还是楼主的
ResultSet自己重新写了
wxzh 2004-09-16
  • 打赏
  • 举报
回复
谢谢,不过好像没有SQL的啊。我可能没讲清楚,在讲细点。

...
//dbMetaData是由Connection得到的一个DatabaseMetaData,"table"
//是数据库中一张表的名字
ResultSet columns = dbMetaData.getColumns(null,
dbMetaData.getUserName(),
"table",
"%");
while(columns.next()){
//COLUMN_NAME和REMARKS是参考j2sdk文档上给出的列名
System.out.println(columns.getString("COLUMN_NAME")+":"
+columns.getString("REMARKS"));
}

运行后,能得到列名,但是没有得到该列的comment。

api文档:
getColumns
public ResultSet getColumns(String catalog,
String schemaPattern,
String tableNamePattern,
String columnNamePattern)
throws SQLException
Retrieves a description of table columns available in the specified catalog.
Only column descriptions matching the catalog, schema, table and column name criteria are

returned. They are ordered by TABLE_SCHEM, TABLE_NAME, and ORDINAL_POSITION.

Each column description has the following columns:

1.TABLE_CAT String => table catalog (may be null)
2.TABLE_SCHEM String => table schema (may be null)
3.TABLE_NAME String => table name
4.COLUMN_NAME String => column name
5.DATA_TYPE int => SQL type from java.sql.Types
6.TYPE_NAME String => Data source dependent type name, for a UDT the type name is
fully qualified
7.COLUMN_SIZE int => column size. For char or date types this is the maximum
number of characters, for numeric or decimal types this is precision.
8.BUFFER_LENGTH is not used.
9.DECIMAL_DIGITS int => the number of fractional digits
10.NUM_PREC_RADIX int => Radix (typically either 10 or 2)
11.NULLABLE int => is NULL allowed.
*columnNoNulls - might not allow NULL values
*columnNullable - definitely allows NULL values
*columnNullableUnknown - nullability unknown
12.REMARKS String => comment describing column (may be null)
13.COLUMN_DEF String => default value (may be null)
...................

weimenren 2004-09-16
  • 打赏
  • 举报
回复
columns.getString("COLUMN_NAME");

columns.getString("REMARKS");

不是同一列

你是否可以把你SQL写出来
wxzh 2004-09-16
  • 打赏
  • 举报
回复
帮帮我啊

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧