Sunday, May 24, 2009

Java MS Access Primary and Foriegn Key

Here is my problem statement : To Implement these 4 functions 1.// return a list of all the table names in the database // as an ArrayList<string> which looks like: // [tableName1, tableName2, tableName3, ..., tableNameN]

ArrayList<string> getAllTableNames()

2. // return the primary key of a given table

// as an ArrayList<string> with the primary key columns in proper // order

ArrayList<string> getPrimaryKey(String tableName) OR PREFERABLY:

write a method to return all the primary keys of all the tables at once.

3. // for a given table, return a list of 4-tuples which are // the relational constraints from foreign tables into this // table's primary keys. Each 4-tuple looks like this: // [PKTABLE_NAME, PKCOLUMN_NAME, FKTABLE_NAME, FKCOLUMN_NAME]

public ArrayList<arraylist<string>> getTableConstraints(String tableName)

OR PREFERABLY:

write a method to return all the relational constraints of all the tables at once.

4.// return a list of all the columns in the database as // a list of triples. Each triple looks like this: // [TABLE_NAME, COLUMN_NAME, DATA_TYPE]

ArrayList<arraylist<string>> reallyGetColumns()

My PROPOSED SOLUTION: For getting the foreign key relationships you'll have to enable the read permission to system table via access (refer to screenshots). Next :

For getting the foreign key relationships you'll have to enable the read permission to system table vi access (refer to screenshots).

Here are the Function Prototypes that are called :

1. private ArrayList<string> gettablenames(Connection con) throws SQLException

2. private ArrayList<string> getprimarykey(Connection con,String tablename) throws SQLException

3. ArrayList<arraylist><string>> getforeignKeys(Connection con,String tablename) throws SQLException

4. ArrayList<arraylist><string>> reallyGetColumns(Connection con) throws SQLException

Test The application by double clicking the jar. I am also attaching the database that i used to test it.

Here is the Executable To Test. Get it Now (sample Db included). Here is the Source Code And Executable : NetBeans Project

: Eclipse Project

No comments: