Jsp中有什么方法可以获得数据库表中字段的类型?

menglionel 2004-03-19 09:20:18
如题,谢谢!
...全文
89 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
CoolAbu 2004-03-19
  • 打赏
  • 举报
回复
用DatabaseMetaData,它有一个getTables方法


public ResultSet getTables(String catalog,
String schemaPattern,
String tableNamePattern,
String[] types)
throws SQLExceptionRetrieves a description of the tables available in the given catalog. Only table descriptions matching the catalog, schema, table name and type criteria are returned. They are ordered by TABLE_TYPE, TABLE_SCHEM and TABLE_NAME.
Each table description has the following columns:

TABLE_CAT String => table catalog (may be null)
TABLE_SCHEM String => table schema (may be null)
TABLE_NAME String => table name
TABLE_TYPE String => table type. Typical types are "TABLE", "VIEW", "SYSTEM TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM".
REMARKS String => explanatory comment on the table
TYPE_CAT String => the types catalog (may be null)
TYPE_SCHEM String => the types schema (may be null)
TYPE_NAME String => type name (may be null)
SELF_REFERENCING_COL_NAME String => name of the designated "identifier" column of a typed table (may be null)
REF_GENERATION String => specifies how values in SELF_REFERENCING_COL_NAME are created. Values are "SYSTEM", "USER", "DERIVED". (may be null)
Note: Some databases may not return information for all tables.


Parameters:
catalog - a catalog name; must match the catalog name as it is stored in the database; "" retrieves those without a catalog; null means that the catalog name should not be used to narrow the search
schemaPattern - a schema name pattern; must match the schema name as it is stored in the database; "" retrieves those without a schema; null means that the schema name should not be used to narrow the search
tableNamePattern - a table name pattern; must match the table name as it is stored in the database
types - a list of table types to include; null returns all types
Returns:
ResultSet - each row is a table description
xunyiren 2004-03-19
  • 打赏
  • 举报
回复
用DatabaseMetaData的getCatalogs()方法试试
menglionel 2004-03-19
  • 打赏
  • 举报
回复
多谢多谢了,再问一个问题,有没有什么方法可以得到所有数据库中的表名吗?
xunyiren 2004-03-19
  • 打赏
  • 举报
回复
java.sql.Types (这是返回的整数值所对应的字段类型)
public static final int ARRAY 2003
public static final int BIGINT -5
public static final int BINARY -2
public static final int BIT -7
public static final int BLOB 2004
public static final int BOOLEAN 16
public static final int CHAR 1
public static final int CLOB 2005
public static final int DATALINK 70
public static final int DATE 91
public static final int DECIMAL 3
public static final int DISTINCT 2001
public static final int DOUBLE 8
public static final int FLOAT 6
public static final int INTEGER 4
public static final int JAVA_OBJECT 2000
public static final int LONGVARBINARY -4
public static final int LONGVARCHAR -1
public static final int NULL 0
public static final int NUMERIC 2
public static final int OTHER 1111
public static final int REAL 7
public static final int REF 2006
public static final int SMALLINT 5
public static final int STRUCT 2002
public static final int TIME 92
public static final int TIMESTAMP 93
public static final int TINYINT -6
public static final int VARBINARY -3
public static final int VARCHAR 12
menglionel 2004-03-19
  • 打赏
  • 举报
回复
多谢两位,我先去试一下
xunyiren 2004-03-19
  • 打赏
  • 举报
回复
ResultSetMetaData rsmd =rs.getMetaData();
int type1=rsmd .getColumnType(1);
CoolAbu 2004-03-19
  • 打赏
  • 举报
回复
楼上说的对,用ResultSetMetaData,它有一个getColumnTypeName(int column) 方法可以返回。

还有其他的很多方法,你最好看看JAVA API
http://java.sun.com/j2se/1.4.2/docs/api/
wangyonghe 2004-03-19
  • 打赏
  • 举报
回复
可以使用ResultSetMetaData接口实现,ResultSet.getMetaData()方法就是返回实现这个接口的类,详细的请参考JDK文档

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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