Home:ALL Converter>UCanAccess/ResultSetMetaData/getColumnName() random return names of columns

UCanAccess/ResultSetMetaData/getColumnName() random return names of columns

Ask Time:2017-04-10T21:03:40         Author:Dmytro Cherednychenko

Json Formatter

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?

Author:Dmytro Cherednychenko,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/43323946/ucanaccess-resultsetmetadata-getcolumnname-random-return-names-of-columns
yy