UCanAccess reads the last column of a table as the first column [duplicate]

橙三吉。 提交于 2019-12-10 22:18:53

问题


My program always reads the first column in the Access database as the 2nd column, the 2nd one as the 3rd column and so on, but the last column it reads as the first column.

public void actionPerformed(ActionEvent e) {
  try{
    String query = "insert into Staff values ('2', 'w', 'w', 'w', 'e', 'w', 'd','d','end')";
    stmt.executeUpdate(query);
    status.setText("1 row of record have been inserted");
  }catch(Exception ex){
    ex.printStackTrace();
  }
}

But when i specify the column names, it reads normally

public void actionPerformed(ActionEvent e) {
  try{
    String query = "insert into Staff (id, lastName, firstName, mi, address, city, state, telephone, email) "
    + "values ('2', 'w', 'w', 'w', 'e', 'w', 'd','d','end')";
    stmt.executeUpdate(query);
    status.setText("1 row of record have been inserted");
  }catch(Exception ex){
    ex.printStackTrace();
  }
}

回答1:


By default, UCanAccess treats the columns of a table in "DATA" order. That is the order in which the columns have been defined internally in the Access metadata, which can be different from the "DISPLAY" order.

If you want UCanAccess to treat the columns in "DISPLAY" order, then add the argument

;columnOrder=DISPLAY

to the end of your connection URL, e.g.,

jdbc:ucanaccess://c:/db/cico.mdb;columnOrder=DISPLAY

However, you should also realize by now that by not specifying the actual column names in your INSERT statement you are leaving your code vulnerable to breakage by assuming the names (and order) of the columns.

Just as SELECT * FROM is frowned upon, so is INSERT INTO tablename VALUES ( .... You are already specifying the values for each column so you really should be specifying the names of the columns as well.



来源:https://stackoverflow.com/questions/33130475/ucanaccess-reads-the-last-column-of-a-table-as-the-first-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!