0

I have a question about getColumnName () methods. I am using it in the UCanAccess library. I'm trying to get column names and put it at table exactly as they are in MS Access DB. But I get random order. Mayby somebody know what do I incorrect?

For the test I had delete adding to the table and implemented just printing.

Code is:

public class Connections {

private final String dbUrl="jdbc:ucanaccess://C:/Users/Admin/Desktop/DB.accdb";
private final String user="";
private final String password="";

public Statement conecting(){
    Statement stat=null;
    try{
        Connection connection=DriverManager.getConnection(dbUrl,user,password);             
        stat=connection.createStatement();          
    }
    catch(SQLException exc){
        exc.printStackTrace();
    }
    return stat;
}
public String[] prepearedNamesOfColumns(String nameOfTable, Statement stat) throws SQLException{
    String[] result=null;
    ResultSet res=stat.executeQuery("select*from "+nameOfTable);
    ResultSetMetaData rsmd=res.getMetaData();
    int columnCount=rsmd.getColumnCount();
    for(int i=1; i<=columnCount;i++){
        System.out.println(rsmd.getColumnName(i));
    }
    return result;
}

Main class:

public class Demonstration {

public static void main(String[] args) throws SQLException {    
    Connections operacje= new Connections();
    String[] tab=new String[6];
    tab[0]="dluznicy";
    tab[1]="Filipczak";
    tab[2]="Karol";
    tab[3]="Poznań";
    tab[4]="M";
    tab[5]="34";
    String[] wynik=operacje.prepearedNamesOfColumns(tab[0], operacje.conecting());      
}

}

At Access I have order like: 1) id_dluznik 2) nazwisko 3) Imie 4) miejsce_urodzenia 5) plec 6) wiek

But in console (after running) I get: 1)nazwisko 2)Imie 3)miejsce_urodzenia 4)wiek 5)plec 6)id_dluznik

Why I get other order of column's names?

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • guess that is order of columns in db table when it was created. instead of select * from tablename, try listing out the columns in the order you want.. like select id_dluznik, nazwisko, Imie, miejsce_urodzenia, plec, wiek from Filipczak – Balaji Krishnan Apr 10 '17 at 13:08
  • Try adding `;columnOrder=DISPLAY` to the end of your connection URL. – Gord Thompson Apr 10 '17 at 13:47
  • Balaji Krishnan - I will use this method for different tables, so I can't write names of the columns directly. – Dmytro Cherednychenko Apr 10 '17 at 14:55
  • Gord Thompson - your recommendation solved the problem. Thank you – Dmytro Cherednychenko Apr 10 '17 at 14:56
  • Possible duplicate of [UCanAccess reads the last column of a table as the first column](http://stackoverflow.com/questions/33130475/ucanaccess-reads-the-last-column-of-a-table-as-the-first-column) – Gord Thompson Apr 14 '17 at 11:39

0 Answers0