java 获取主键和外键约束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15357528/
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
Get Primary Key and Foreign Key constraint
提问by rahul bilove
Get Primary Key and Foreign Key constraint from MS Access Database in java Program.
在 java 程序中从 MS Access 数据库获取主键和外键约束。
I tried to use desc table_name
but it didn't work.
我尝试使用,desc table_name
但没有用。
I have even tried DatabaseMetaData.getMetaData(null,null,"table_name")
but it didn't work either.
我什至尝试过,DatabaseMetaData.getMetaData(null,null,"table_name")
但它也不起作用。
采纳答案by Arsen Alexanyan
I don't know if this will bring problems working with MS Access Database, but reading the JDBC specification You should do the following steps.
我不知道这是否会带来使用 MS Access 数据库的问题,但是阅读 JDBC 规范您应该执行以下步骤。
For getting table from DB use
用于从数据库获取表使用
DatabaseMetaData meta = connection.getMetaData();
ResultSet tablesRs = meta.getTables(null, null, "table_name", new String[]{"TABLE"});
For getting primary keys use
获取主键使用
meta.getPrimaryKeys(null, null, "table_name");
For getting Foreign keys use
获取外键使用
meta.getExportedKeys(null, null, "table_name");
EDIT:It seems that it wasn't implemented for ODBC Driver,
You have to look this post how to get primary keys with ODBC driver working with MS Access Database.
hereis used meta.getIndexInfo(...)
to get primary keys
编辑:似乎它没有为 ODBC 驱动程序实现,您必须查看这篇文章如何使用 ODBC 驱动程序与 MS Access 数据库一起获取主键。
这里用于meta.getIndexInfo(...)
获取主键
回答by Bhushan
use DatabaseMetadata
class.
使用DatabaseMetadata
类。
From that class use getPrimaryKeys()
method
从那个班级使用getPrimaryKeys()
方法
回答by Shailesh Saxena
The retrieval of the meta data for a table is easy. You construct an SQL
on your table and execute it. From the resulting ResultSet
you can retrieve the MetaDataResultSet
which contains among other stuff the field name, the field type, nullable and autoincrement
:
Just try this:
检索表的元数据很容易。你SQL
在你的桌子上构造一个并执行它。从结果中,ResultSet
您可以检索MetaDataResultSet
包含字段名称、字段类型等内容的nullable and autoincrement
: 试试这个:
ResultSet set =stmt.executeQuery(sql);//stmt is your Prepared or Callable Statement object
ResultSetMetaData resultMeta = set.getMetaData();
for (int i=1;i< =resultMeta.getColumnCount();i++)
{
String fieldName = resultMeta.getColumnName(i);
int type = resultMeta.getColumnType(i); // java.sql.Types
Class _fieldType = convertType(type);
boolean nullable = resultMeta.isNullable(i)!=java.sql.ResultSetMetaData.columnNoNulls;
boolean isAutoincrement = resultMeta.isAutoIncrement(i);
System.out.printf("Field: %s, %s, nullable %b, autoincrement
%b%n",fieldName,_fieldType.getName(),nullable,isAutoincrement);
}
The type is specified as an integer constant in java.sql.Types. Information on the constraints can be retrieved from the DatabaseMetaData which can bee retrieved from the connection. There are 3 methods:
该类型在 java.sql.Types 中指定为整数常量。可以从可以从连接中检索的 DatabaseMetaData 中检索有关约束的信息。有3种方法:
getPrimaryKeys(catalog,schema,table):
to retrieve a ResultSet that contains; one entry for each column for each primary key.
getPrimaryKeys(catalog,schema,table):
检索包含的 ResultSet;每个主键的每一列对应一个条目。
getIndexInfo(catalog,schema,table,unique,false):
to retrieve indices unique and otherwise. You retrieve a ResultSet that contains one entry for each column for each index. If unique is false the result contains unique and non unique keys.
getIndexInfo(catalog,schema,table,unique,false):
检索唯一的索引,否则。您检索一个 ResultSet,其中每个索引的每一列都包含一个条目。如果 unique 为 false 结果包含唯一和非唯一键。
getExportedKeys(catalog,schema,table):
to retrieve a ResultSet that contains all foreign keys; one entry for each column for each key.
getExportedKeys(catalog,schema,table):
检索包含所有外键的 ResultSet;每个键的每一列对应一个条目。
回答by Siva
//For getting the Column name of primary key
//获取主键的列名
DatabaseMetaData meta=conn.getMetaData();
rs= meta.getTables(null, null, tablename, new String[]{"TABLE"});
rs=meta.getPrimaryKeys(null, null, tablename);
while(rs.next())
System.out.println("Primery Key :"+rs.getString(4));
//and also foreign key constraint
//还有外键约束
rs=meta.getExportedKeys(null, null, tablename);
while(rs.next())
System.out.println("Foreign Key :"+rs.getString(4));
回答by 21stking
Simple solution:
简单的解决方案:
public String getPrimaryKey(String table) throws SQLException
{
con = null;
con = DriverManager.getConnection("jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=//access_file_path/database_name.mdb", "username", "password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM "+table);
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
String primaryKey_columnName = "";
boolean isAutoIncrement;
for(int x = 1;x<=cols;x++){
isAutoIncrement = rsmd.isAutoIncrement(x);
if(isAutoIncrement){
primaryKey_columnName = rsmd.getColumnName(x);
}
}
rs.close();
con.close();
return primaryKey_columnName;
}