Exemple #1
0
  /**
   * @return JDBCDAO object.
   * @throws DAOException throw DAOException
   */
  public JDBCDAO getJDBCDAO() throws DAOException {
    JDBCDAO dao = null;
    try {
      dao = (JDBCDAO) Class.forName(defaultDAOClassName).newInstance();
      dao.setConnectionManager(getConnectionManager());

    } catch (Exception excp) {
      ErrorKey errorKey = ErrorKey.getErrorKey("db.operation.error");
      throw new DAOException(errorKey, excp, "DAOFactory.java :");
    }
    return dao;
  }
  /**
   * This method will be called to insert hashed data values.
   *
   * @param tableName :Name of the table
   * @param columnValues :List of column values
   * @param columnNames :List of column names.
   * @param jdbcDAO : Database jdbcDAO
   * @throws DAOException :DAOException
   * @throws SQLException : SQLException
   */
  public void insertHashedValues(
      String tableName, List<Object> columnValues, List<String> columnNames, JDBCDAO jdbcDAO)
      throws DAOException, SQLException {

    List<String> columnNamesList = new ArrayList<String>();
    ResultSetMetaData metaData;

    PreparedStatement stmt = null;
    try {
      if (columnNames != null && !columnNames.isEmpty()) {
        metaData = getMetaData(tableName, columnNames, jdbcDAO);
        columnNamesList = columnNames;
      } else {
        metaData = getMetaDataAndUpdateColumns(tableName, columnNamesList, jdbcDAO);
      }

      String insertQuery = createInsertQuery(tableName, columnNamesList);
      stmt = jdbcDAO.getPreparedStatement(insertQuery);
      setStmtIndexValue(columnValues, metaData, stmt);
      stmt.executeUpdate();
    } catch (SQLException sqlExp) {
      logger.error(sqlExp.getMessage(), sqlExp);
      ErrorKey errorKey = ErrorKey.getErrorKey("db.operation.error");
      throw new DAOException(
          errorKey, sqlExp, "HashedDataHandler.java :" + DAOConstants.INSERT_OBJ_ERROR);
    }
  }
  /**
   * This method returns the metaData associated to the table specified in tableName.
   *
   * @param tableName Name of the table whose metaData is requested
   * @param columnNames Table columns
   * @param jdbcDAO : Database connections to retrieve meta data.
   * @return It will return the metaData associated to the table.
   * @throws DAOException : DAOException
   * @throws SQLException :
   */
  protected final ResultSetMetaData getMetaData(
      String tableName, List<String> columnNames, JDBCDAO jdbcDAO)
      throws DAOException, SQLException {

    ResultSetMetaData metaData;
    StringBuffer sqlBuff = new StringBuffer(DAOConstants.TAILING_SPACES);
    sqlBuff.append("Select").append(DAOConstants.TAILING_SPACES);

    for (int i = 0; i < columnNames.size(); i++) {
      sqlBuff.append(columnNames.get(i));
      if (i != columnNames.size() - 1) {
        sqlBuff.append("  ,");
      }
    }
    sqlBuff.append(" from " + tableName + " where 1!=1");
    metaData = jdbcDAO.getQueryResultSet(sqlBuff.toString()).getMetaData();

    return metaData;
  }
  /**
   * This method will returns the metaData associated to the table specified in tableName and update
   * the list columnNames.
   *
   * @param tableName Name of the table whose metaData is requested
   * @param columnNames Table columns
   * @param jdbcDAO : Database connections to retrieve meta data.
   * @return It will return the metaData associated to the table.
   * @throws DAOException : DAOException
   */
  protected final ResultSetMetaData getMetaDataAndUpdateColumns(
      String tableName, List<String> columnNames, JDBCDAO jdbcDAO) throws DAOException {
    ResultSetMetaData metaData;
    try {

      StringBuffer sqlBuff = new StringBuffer(DAOConstants.TAILING_SPACES);
      sqlBuff.append("Select * from ").append(tableName).append(" where 1!=1");
      metaData = jdbcDAO.getQueryResultSet(sqlBuff.toString()).getMetaData();

      for (int i = 1; i <= metaData.getColumnCount(); i++) {
        columnNames.add(metaData.getColumnName(i));
      }
    } catch (SQLException sqlExp) {
      logger.fatal(sqlExp.getMessage(), sqlExp);
      ErrorKey errorKey = ErrorKey.getErrorKey("db.operation.error");
      throw new DAOException(
          errorKey, sqlExp, "HashedDataHandler.java :" + DAOConstants.RS_METADATA_ERROR);
    }

    return metaData;
  }