嗨,我基本上是这样做的,以便从数据库检索数据到数组
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,以便用户可以对其进行编辑,然后更新数组,问题是如何获取此数据并将其发送回数据库?
我在数据类型方面遇到麻烦了吗?因为数组是String类型,并且数据库具有int Type,String Type,long type .....
提前致谢
最佳答案
我在数据类型方面遇到麻烦了吗?因为数组是String类型,并且数据库具有int Type,String Type,long type .....
如果您尝试更新的任何字段都是Access中的Date/Time
字段,则可能是这样。 Jackcess能够将字符串隐式转换为数字(无论如何,无论如何),但是在日期上却无法做到这一点。
对于名为[Members]的表中的示例数据
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();