Java tutorial
package com.che.software.testato.domain.dao.jdbc.impl; import java.sql.Connection; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.DbUtils; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.log4j.Logger; import org.springframework.stereotype.Repository; import com.che.software.testato.domain.dao.IRandomIndexDAO; import com.che.software.testato.domain.dao.jdbc.adao.AbstractDAO; import com.che.software.testato.domain.dao.jdbc.exception.RandomIndexSearchDAOException; import com.che.software.testato.domain.entity.RandomIndex; import com.che.software.testato.domain.entity.search.RandomIndexSearch; /** * JDBC implementation of the DAO interface dedicated to the random index * management. * * @author Clement HELIOU (clement.heliou@che-software.com). * @copyright Che Software. * @license GNU General Public License. * @see AbstractDAO, IRandomIndexDAO. * @since August, 2011. * * This file is part of Testato. * * Testato is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or (at your * option) any later version. * * Testato is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. * * You should have received a copy of the GNU General Public License * along with Testato. If not, see <http://www.gnu.org/licenses/>. * * Testato's logo is a creation of Arrioch * (http://arrioch.deviantart.com/) and it's distributed under the terms * of the Creative Commons License. */ @Repository("randomIndexDAO") public class RandomIndexDAO extends AbstractDAO implements IRandomIndexDAO { /** * Constants. */ private static final Logger LOGGER = Logger.getLogger(RandomIndexDAO.class); /** * Random index search from a bean of criterions. * * @author Clement HELIOU (clement.heliou@che-software.com). * @param searchBean the criterions to use for the search. * @return the resulting object list. * @since August, 2011. * @throws RandomIndexSearchDAOException if an error occurs during the * search. */ @Override public List<RandomIndex> searchRandomIndex(RandomIndexSearch searchBean) throws RandomIndexSearchDAOException { LOGGER.debug("searchRandomIndex()."); Connection connection = null; try { connection = getDataSource().getConnection(); List<Object> params = new ArrayList<Object>(); return getQueryRunner().query(connection, getRandomIndexSearchQueryFromCriterion(searchBean, params), new BeanListHandler<RandomIndex>(RandomIndex.class), params.toArray()); } catch (SQLException e) { throw new RandomIndexSearchDAOException(e); } finally { if (null != connection) { DbUtils.closeQuietly(connection); } } } /** * Recovery of the random index search query from criterion. * * @author Clement HELIOU (clement.heliou@che-software.com). * @param searchBean the object containing the criterions. * @param params the parameters list corresponding to the built query. * @return the built query. * @since August, 2011. */ private String getRandomIndexSearchQueryFromCriterion(RandomIndexSearch searchBean, List<Object> params) { LOGGER.debug("getRandomIndexSearchQueryFromCriterion()."); setWhereClauseEnabled(false); StringBuilder sBuilder = new StringBuilder( "SELECT random_index_id AS randomIndexId, \"number\", \"value\" FROM random_index "); if (null != searchBean && null != searchBean.getNumber() && 0 != searchBean.getNumber()) { sBuilder.append(getWhereClauseBegin()); sBuilder.append("\"number\" = ? "); params.add(searchBean.getNumber()); } return sBuilder.toString(); } }