1

I am trying to retrieve column descriptions for MS Access columns using I am able to retrieve all columns of database like field name, data type.

I need description column data using Java.

Kindly give some suggestion or guidance.

Note: I used this code to get column name

public ArrayList<String> fetchtable(String value)
{       
    try
    {
       makeConnection();
       String str1="Select * from "+ value;
       ResultSet rs = st.executeQuery(str1);
       rsmd = rs.getMetaData();
       NumOfCol= rsmd.getColumnCount();
       for(int i=1;i<=NumOfCol;i++)
       {
          ColumnName = rsmd.getColumnName(i);
          System.out.println(ColumnName);
          columns.add(ColumnName);
        }       
        //System.out.println("Columns Valuessss is:" +columns);         
        }catch(Exception ae){
            ae.printStackTrace();
        }
        return columns;
    }

1 Answer 1

1

The only way I know to retrieve a field's Description is via Microsoft DAO. One way to do that would be to have the Java program

  • write a little VBScript,
  • execute it, and
  • capture the results,

something like this:

package com.example.getaccessfielddescription;

import java.io.*;

public class Main {

    public static void main(String[] args) {
        // test data
        String dbFileSpec = "C:\\Users\\Public\\Database1.accdb";
        String tableName = "Clients";
        String fieldName = "LastName";

        String vbsFilePath = System.getenv("TEMP") + "\\GetAccessFieldDescription.vbs";
        File vbsFile = new File(vbsFilePath);
        PrintWriter pw;
        try {
            pw = new PrintWriter(vbsFile);
            pw.println("Set dbe = CreateObject(\"DAO.DBEngine.120\")");
            pw.println("Set db = dbe.OpenDatabase(\"" + dbFileSpec + "\")");
            pw.println("Set fld = db.TableDefs(\"" + tableName + "\").Fields(\"" + fieldName + "\")");
            pw.println("WScript.Echo fld.Properties(\"Description\").Value");
            pw.println("Set fld = Nothing");
            pw.println("Set db = Nothing");
            pw.println("Set dbe = Nothing");
            pw.close();

            Process p = Runtime.getRuntime().exec("cscript /nologo \"" + vbsFilePath + "\"");
            p.waitFor();
            BufferedReader rdr = 
                    new BufferedReader(new InputStreamReader(p.getInputStream()));
            String fieldDescription = rdr.readLine();

            vbsFile.delete();

            System.out.println(fieldDescription);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

Your approach is excellent, I just want to know whether this approach is feasible for servlet also. Thank you
@user3599755 If you're talking about using the above approach on a web server then there would probably be some permissions issues that would need to be sorted out. The account under which a web server runs is usually prohibited from running arbitrary code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.