2014-02-18 21 views
1

嗨基本上即時這樣做是爲了從數據庫中檢索數據陣列如何從陣列使用jackcess

for(i=0;i<numfilas;i++){ 
    HashMap<Object, Object> rowdata = new HashMap<Object, Object>(cur.getNextRow()); 
    for(j=0;j<numcolumnas;j++){ 
     datos[posicion]=rowdata.get(nombrecolumnas[j]).toString(); 
     posicion++; 
    } 
    } 

那麼我將數據傳遞到EditTexts所以用戶可以編輯數據傳遞到數據庫和我更新後數組,問題是我如何獲取這些數據並將其發送回數據庫?

我是否遇到數據類型問題?因爲數組是字符串類型,數據庫有整型,字符串類型,長型.....

在此先感謝

回答

2

我是否獲得與數據類型的麻煩嗎?因爲數組是字符串類型,數據庫有整型,字符串類型,長型.....

你可能是,如果你的任何嘗試更新的字段是在Access Date/Time領域。 Jackcess能夠隱式地將字符串轉換爲數字(無論如何,在很多情況下),但是當涉及到日期時,它無法做到這一點。

對於表中指定的樣本數據[會員]

MemberID MemberName SponsorID DateJoined FeePaid 
-------- ---------- --------- ---------- ------- 
     1 Gord     2014-01-16  0 

下面的代碼工作正常

try (Database db = DatabaseBuilder.open(new File("C:/Users/Public/mdbTest.mdb"))) { 
    Table table = db.getTable("Members"); 
    Row row = CursorBuilder.findRow(table, Collections.singletonMap("MemberID", 1)); 
    if (row != null) { 
     row.put("SponsorID", "0"); // "Long Integer" in Access 
     row.put("FeePaid", "130"); // "Currency" in Access 
     table.updateRow(row); 
    } 
    else { 
     System.out.println("row not found."); 
    } 
} catch (Exception e) { 
    e.printStackTrace(System.out); 
} 

然而,這是不行的

row.put("DateJoined", "2014-01-23"); // "Date/Time" in Access 

因爲Jackcess不能隱式地將字符串值轉換爲其內部(數字)日期值,相反,您需要執行一些操作像這樣的東西

org.joda.time.DateTime dt = new org.joda.time.DateTime("2014-01-23"); 
row.put("DateJoined", dt.getMillis()); 

作爲一種替代方法,您可能需要調查UCanAccess。它是一個純Java JDBC驅動程序,它使用Jackcess在Access數據庫上執行讀寫操作,但可以使用更多「常規」SQL方法執行此操作:

Connection conn=DriverManager.getConnection(
     "jdbc:ucanaccess://C:/Users/Public/mdbTest.mdb"); 
PreparedStatement ps = conn.prepareStatement(
     "UPDATE Members SET " + 
      "SponsorID=?, " + 
      "DateJoined=?, " + 
      "FeePaid=? " + 
     "WHERE MemberID=1"); 
ps.setInt(1, Integer.parseInt("0")); 
org.joda.time.DateTime dt = new org.joda.time.DateTime("2014-01-23"); 
ps.setTimestamp(2, new Timestamp(dt.getMillis())); 
ps.setBigDecimal(3, new BigDecimal("130")); 
ps.executeUpdate();