如何使用Java从MS ACCESS数据库中检索列描述

时间:2014-05-13 08:56:16

标签: java ms-access jdbc ms-access-2007 jdbc-odbc

我正在尝试检索MS Access列的列描述,我可以检索数据库的所有列,如字段名称,数据类型。

我需要使用Java描述列数据。

请提出一些建议或指导。

注意:我使用此代码获取列名

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 个答案:

答案 0 :(得分:1)

我知道检索字段描述的唯一方法是通过Microsoft DAO。一种方法是使用Java程序

  • 写一点VBScript,
  • 执行它,
  • 捕获结果,

类似的东西:

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();
        }
    }

}
相关问题