【问题标题】:How to enter LinkedHashMap Values to database without using bean?如何在不使用 bean 的情况下将 LinkedHashMap 值输入数据库?
【发布时间】:2015-05-25 07:08:09
【问题描述】:

我是 LinkedHashMap 的新手。

现在我有一些像 LinkedHashMap 这样的数据-

article_ID=386247568292, author_externalId=235697849816687, author_fbid=235697849816687, ...

现在这是我的数据库入口代码

public void InsertEntry(LinkedHashMap<String, Object> entMap) throws SQLException {

    Connection conn = null;
    PreparedStatement pstmt = null;

    conn = pool.getConnection();
    String sql="INSERT into socialmedia(article_ID,author_externalId, author_fbid)"
                + "VALUES(?,?,?)";
   pstmt = conn.prepareStatement(sql);

现在我如何将 My LinkedHashMap 值输入到字符串中,以便我可以将所有这些值输入到数据库中。

我不想使用任何 Bean。

【问题讨论】:

    标签: java dictionary linkedhashmap


    【解决方案1】:

    从 LinkedHashMap 中获取值

    // either you loop thru the entries
    Set<String> keys = entMap.keySet();
    int param = 1;
    for(String k:keys) {
        pstmt.setString( (param++), (String)entMap.get(k) );
    }
    
    or
    
    // if you prefer use the key names, e.g.
    String aid = (String) entMap.get("article_ID");
    

    关于 LinkedHashMap

    • 请记住,您将从 LinkedHashMap 按输入条目的放置顺序获取值。

    • 您必须确保它们始终按此顺序排列

    • 一种解决方案可能是您检查密钥(按名称)并确定它是否是您期望的顺序/类型,以使您的解决方案更加可靠。并确保这些值将被 setString、setInt 等处理。

    SQL IN 参数

    • 请记住,setString 会将所有插入作为 String 对象处理。这适用于大多数 jdbc 支持的数据库。但这并不是 100% 的舒适。如果你知道你有整数等,那么将它们作为 setInt 处理。

    来自PreparedStatement规范:

    用于设置 IN 参数值的 setter 方法(setShortsetString 等)必须指定与输入参数的已定义 SQL 类型兼容的类型。 例如,如果 IN 参数的 SQL 类型为 INTEGER,则应使用方法 setInt

    现在这在所有数据库服务器/jdbc 驱动程序中可能并不那么严格,但如果您的插入失败,则值得一提。

    【讨论】:

      【解决方案2】:

      下面应该可以工作....

      public void InsertEntry(LinkedHashMap<String, Object> entMap) throws SQLException {
          Connection conn = null;
          PreparedStatement pstmt = null;
          conn = pool.getConnection();
          String sql="INSERT into socialmedia(user_name,article_count,epoch,description,authorId,article_url,publish_date,harvest_date,following,followers,updates,sentiment)"
                  + "VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
          pstmt = conn.prepareStatement(sql);
          Set keys = entmap.keySet();  
          int rowId=1;
          for(String key : keys)
          {
             String val = entMap.get(key);
             pstmt.setString(rowId,val);
             rowId++;
          }
          pstmt.executeUpdate();
      }
      

      如果您有任何问题,请告诉我。

      【讨论】:

      • 地图中有字符串键...所以 LinkedHashMap 应该包含所有字符串键..根据问题中提供的参数化映射
      • 可能......我们可以这样做......因为从概念上来说 rowId 变量代表 INSERT 查询中的列
      【解决方案3】:

      感谢大家的回复。我是这样做的-

      String sql = "INSERT INTO students(article_ID, description_charset, author_appScopedId, author_externalId) values(?,?,?,?);
      pstmt = conn.prepareStatement(sql);
      
      pstmt.setString(1, (String) entMap.get("article_ID"));
      pstmt.setString(2, (String) entMap.get("description_charset"));
      pstmt.setString(3, (String) entMap.get("author_appScopedId"));
      pstmt.setString(4, (String) entMap.get("author_externalId"));
      
      pstmt.executeUpdate();
      

      【讨论】:

      • 好,我也会用钥匙拿到它们。正如我所写的,这将为您提供更可靠的解决方案,因为您知道您得到了什么(类型)、以什么顺序以及您在表格中插入了什么。
      猜你喜欢
      • 2019-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 2017-02-20
      相关资源
      最近更新 更多