Search code examples
jackcess

how to get a Table object given its name (as String) in Jackcess


im starting with java and jackcess, I have this code

public class DataManagement {
static Database db;
static Database dbopen;
static Table Users;
static Set<String> tablename;
static String[] nametabla;

public static void opendatabase(){
   try {
    dbopen=DatabaseBuilder.open(new File("D:/alex.accdb"));
    tablename=dbopen.getTableNames();
    nametabla = tablename.toArray(new String[0]);
} catch (IOException e) {
    e.printStackTrace();
}
}

public static void readtabla (String name){
   System.out.println(name);
   for(Column column : name.getColumns()) {
    String columnName = column.getName();
    System.out.println(columnName + " " );


  }
}

public static void main(String[] args){

    /*opening an existing database*/
    opendatabase();
    System.out.println("the name of tables are " + tablename);
    System.out.println("the name of table 2 is " + nametabla[1]);
    readtabla(nametabla[1]);


}
}

without the "for" inside of the method readtabla I get this

 the name of the tables are [Establecimiento, Estado, Medición, Ubicación]
 the name of table 2 is Estado
 Estado

the problem lies here

 for(Column column : name.getColumns())

because name must be (I think) a Table type and I send it as a string type, so how can I convert string type into Table type? or there's another way ? thanks in advance


Solution

  • You can use the getTable() method of the Database object to return a Table object:

    public static void readtabla (String name) {
        System.out.println(name);
        Table tbl;
        try {
            tbl = dbopen.getTable(name);
            for(Column column : tbl.getColumns()) {
                String columnName = column.getName();
                System.out.println(columnName + " " );
            }
        } catch (IOException e) {
            e.printStackTrace();
        } 
    }