コード例 #1
0
  public static Date getDateFromDB(String strDbName) {

    Session session = null;
    Transaction tx = null;
    Date rightNow = null;
    try {

      session = getSessionDude(strDbName);
      tx = session.beginTransaction();

      Query query = session.createSQLQuery("SELECT SYSDATE FROM Dual");
      List listQuery = query.list();

      if (listQuery != null && !listQuery.isEmpty()) {
        for (int i = 0; i < listQuery.size(); i++) {
          java.sql.Timestamp rightNowTimeStamp = (java.sql.Timestamp) listQuery.get(i);
          rightNow = new Date(rightNowTimeStamp.getTime());
          if (rightNow != null) {
            break;
          }
        }
      }

    } catch (Exception xcp) {
      xcp.printStackTrace();
    } finally {
      if (session != null && session.isOpen()) {
        session.close();
      }
    }

    return rightNow;
  }
コード例 #2
0
  /**
   * @param classBean
   * @param strKey
   * @param value
   * @return a List of obejcts
   */
  public static java.util.List getListOfObjectByKey(
      Class classBean, String strKey, Object value, String strDbName) {

    Session session = null;
    Transaction tx = null;
    List result = null;
    try {
      session = getSessionDude(strDbName); // sessionFactory.getCurrentSession();
      tx = session.beginTransaction();

      Criteria criteria = session.createCriteria(classBean);

      if (strKey != null) {
        criteria.add(Restrictions.eq(strKey, value));
      }
      result = criteria.list();

      tx.commit();
    } catch (Exception xcp) {
      xcp.printStackTrace();
    } finally {
      if (session != null && session.isOpen()) {
        session.close();
      }
    }
    return result;
  }
コード例 #3
0
  public static List getListObjectsByAnds(
      Class myClass, Map<String, Object> mapArgs, String strDbName) {
    List result = null;
    Session session = null;
    Transaction tx = null;

    if (mapArgs != null && mapArgs.size() > 0) {

      try {
        session = getSessionDude(strDbName); // sessionFactory.getCurrentSession();
        tx = session.beginTransaction();

        Criteria criteria = session.createCriteria(myClass);
        Iterator iter = mapArgs.entrySet().iterator();

        while (iter.hasNext()) {
          Map.Entry<String, Object> pair = (Map.Entry<String, Object>) iter.next();
          criteria.add(Restrictions.eq(pair.getKey(), pair.getValue()));
        }

        tx.commit();
        result = criteria.list();

      } catch (Exception xcp) {
        xcp.printStackTrace();
      } finally {
        if (session != null && session.isOpen()) {
          session.close();
        }
      }
    }
    return result;
  }
コード例 #4
0
  /**
   * @param classBean
   * @param strKey
   * @param value
   * @return a first Object from a List of obejcts
   */
  public static Object getObjectByKey(
      Class classBean, String strKey, Object value, String strDbName) {

    Session session = getSessionDude(strDbName); // sessionFactory.getCurrentSession();
    Transaction tx = session.beginTransaction();
    List result = null;
    Object obj = null;
    try {

      Criteria criteria = session.createCriteria(classBean);

      if (strKey != null) {
        criteria.add(Restrictions.eq(strKey, value));
      }
      result = criteria.list();
      tx.commit();

      if (result != null && result.size() > 0) {
        obj = result.get(0);
      }

    } catch (Exception xcp) {
      xcp.printStackTrace();
    } finally {
      if (session != null && session.isOpen()) {
        session.close();
      }
    }
    return obj;
  }
コード例 #5
0
  public static void procedureExecuterPSTATUSREQUISICAO(String reqStCodigo, String strDbName) {
    Session session = getSessionDude(strDbName);
    Transaction tx = session.beginTransaction();
    Connection con = session.connection();
    List result;

    try {
      CallableStatement cs = con.prepareCall("{ call PSTATUSREQUISICAO(?,?,?) }");
      cs.setString(1, reqStCodigo); // first parameter index start with 1
      cs.setString(2, ""); // second parameter
      cs.setString(3, ""); // second parameter
      cs.execute(); // call stored procedure
      tx.commit();

    } catch (Exception xcp) {
      xcp.printStackTrace();
      ;
    } finally {
      if (session != null && session.isOpen()) {
        session.close();
      }
    }
  }