【问题标题】:how to pass data from array to database using jackcess如何使用jackcess将数据从数组传递到数据库
【发布时间】:2014-02-18 00:56:06
【问题描述】:

嗨,基本上我这样做是为了将数据从数据库检索到数组

 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型、String型、long型.....

提前致谢

【问题讨论】:

    标签: java arrays ms-access gridview jackcess


    【解决方案1】:

    我是否遇到了数据类型的问题?因为数组是String类型,数据库有int型、String型、long型.....

    如果您尝试更新的任何字段是 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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 2016-07-28
      • 2015-12-19
      • 1970-01-01
      • 1970-01-01
      • 2010-10-17
      相关资源
      最近更新 更多