acerbic coffee
走自己的路,让别人跑步
posts - 26,comments - 14,trackbacks - 0
      用java获取表中各个字段的数据类型,在网上逛了半天终于在csdn上找到了一篇文章,有了一些思路,下面是部分代码1
 1/**   
 2            *     取得当前连接数据库指定表的字段信息。   
 3            *   @param   tableName                     表名称   
 4            *   @return                                       字段信息列表   
 5            *   @exception     SQLException     Description   of   the   Exception   
 6            *   @throws     Exception                 失败时抛出   
 7            */
   
 8          public   ParameterList   getFieldList(   String   tableName   )   
 9                  throws   SQLException   
10          {   
11                  ResultSet                   rs   =   executeQuery(   "SELECT   *   FROM   "   +   tableName   );   
12                  ResultSetMetaData   meta                 =   rs.getMetaData(     );   
13                  int                               columnCount   =   meta.getColumnCount(     );   
14                  ParameterList           result             =   new   ParameterList(     );   
15    
16                  for   (   int   i   =   0;   i   <   columnCount;   i++   )   
17                  {   
18                          DBTableFieldStruct   field     =   new   DBTableFieldStruct(     );      
19                          int                                 cursor   =   i   +   1;   
20                          field.name                       =   meta.getColumnName(   cursor   );         (2)
21                          field.type                       =   meta.getColumnType(   cursor   );            (1)
22                          field.size                       =   meta.getColumnDisplaySize(   cursor   );   (3)
23                          field.scale                     =   meta.getScale(   cursor   );                           (4)
24                          field.isNullable           =   meta.isNullable(   cursor   );                        (5)
25    
26                          //field.precision             =   meta.getPrecision(   cursor   );   
27                          result.append(   field   );   
28                  }
   
29    
30                  return   result;   
31          }
所需要的部分在(1)  处,此处返回一int型数据。这样通过java.sql.Types类来匹配这个int型数据,便可知道字段的数据类型
(2),(3),(4),(5) 得到的是字段的其他信息,显而易见,不多废话了!
posted on 2007-02-25 16:40 acerbic coffee 阅读(5074) 评论(0)  编辑  收藏 所属分类: articles

只有注册用户登录后才能发表评论。


网站导航: