コード例 #1
0
ファイル: BaseDAO.java プロジェクト: happyzhaosong/DataTiger
  private void insertDto(Object dto, String idFieldName) throws Exception {
    Connection conn = null;
    Exception ex = null;
    String sql = "";
    try {
      conn = DBManager.getInstance().getDataSource().getConnection();
      /*
      Statement stmt = conn.createStatement();
      sql = DBTool.getInsertSqlFromObjectClass(dto);
      stmt.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
      ResultSet rs = stmt.getGeneratedKeys();
      */
      sql = DBTool.getInsertSqlFromObjectClass(dto);
      PreparedStatement pstmt = (PreparedStatement) conn.prepareStatement(sql, new String[] {"ID"});
      pstmt.executeUpdate();
      ResultSet rs = pstmt.getGeneratedKeys();

      if (rs != null) {
        if (rs.next()) {
          int id = rs.getInt(1);
          ClassTool.invokeSetMethod(dto, idFieldName, String.valueOf(id));
          rs.close();
        }
      }
    } catch (Exception e) {
      LogTool.logError(e, this.getClass().getName());
      LogTool.logText(sql, this.getClass().getName());
      ex = e;
    } finally {
      DBManager.getInstance().closeDBConn(conn);
      if (ex != null) throw ex;
    }
  }