Example usage for org.apache.commons.dbutils DbUtils closeQuietly

List of usage examples for org.apache.commons.dbutils DbUtils closeQuietly

Introduction

In this page you can find the example usage for org.apache.commons.dbutils DbUtils closeQuietly.

Prototype

public static void closeQuietly(Statement stmt) 

Source Link

Document

Close a Statement, avoid closing if null and hide any SQLExceptions that occur.

Usage

From source file:com.che.software.testato.domain.dao.jdbc.impl.ProjectDAO.java

/**
 * Checks if a project with given name is already existing.
 * //from   w  ww .j av  a2s  . co m
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @param name the project name to check
 * @return true if this name is already used, else false.
 * @since July, 2011.
 * @throws ProjectSearchDAOException if an error occurs during the search.
 */
@Override
public boolean isProjectExists(String name) throws ProjectSearchDAOException {
    LOGGER.debug("isProjectExists().");
    Connection connection = null;
    try {
        connection = getDataSource().getConnection();
        return (Boolean) getQueryRunner().query(connection,
                "SELECT EXISTS( SELECT project_id FROM project WHERE name = ?) AS result ",
                new ScalarHandler("result"), new Object[] { name });
    } catch (SQLException e) {
        throw new ProjectSearchDAOException(e);
    } finally {
        if (null != connection) {
            DbUtils.closeQuietly(connection);
        }
    }
}

From source file:net.orpiske.ssps.common.db.derby.DerbyDatabaseManager.java

@Override
public void close() {
    DbUtils.closeQuietly(conn);
}

From source file:com.mirth.connect.server.userutil.DatabaseConnection.java

/**
 * Executes a query on the database and returns a CachedRowSet.
 * //from  www.ja va  2 s.  c om
 * @param expression
 *            The query expression to be executed.
 * @return The result of the query, as a CachedRowSet.
 * @throws SQLException
 */
public CachedRowSet executeCachedQuery(String expression) throws SQLException {
    Statement statement = null;

    try {
        statement = connection.createStatement();
        logger.debug("executing query:\n" + expression);
        ResultSet result = statement.executeQuery(expression);
        CachedRowSet crs = new MirthCachedRowSet();
        crs.populate(result);
        DbUtils.closeQuietly(result);
        return crs;
    } catch (SQLException e) {
        throw e;
    } finally {
        DbUtils.closeQuietly(statement);
    }
}

From source file:de.unibremen.informatik.tdki.combo.data.DBLayout.java

/**
 *
 * @param project//from ww w  . ja v a2  s  . c  o  m
 * @return true if the project already exists
 */
public boolean createProject(String project) {
    CallableStatement callableStatement = null;
    boolean projectExists = false;
    try {
        callableStatement = connection.prepareCall("CALL combo_create_project('" + project + "',?)");
        callableStatement.registerOutParameter(1, java.sql.Types.INTEGER);
        callableStatement.executeUpdate();

        projectExists = (callableStatement.getInt(1) != 0);
    } catch (SQLException ex) {
        throw new RuntimeException(ex);
    } finally {
        DbUtils.closeQuietly(callableStatement);
    }
    return projectExists;
}

From source file:azkaban.trigger.JdbcTriggerLoader.java

@Override
public List<Trigger> loadTriggers() throws TriggerLoaderException {
    logger.info("Loading all triggers from db.");
    Connection connection = getConnection();

    QueryRunner runner = new QueryRunner();
    ResultSetHandler<List<Trigger>> handler = new TriggerResultHandler();

    List<Trigger> triggers;

    try {//from  w  w w . ja  v a  2  s .  c  om
        triggers = runner.query(connection, GET_ALL_TRIGGERS, handler);
    } catch (SQLException e) {
        logger.error(GET_ALL_TRIGGERS + " failed.");

        throw new TriggerLoaderException("Loading triggers from db failed. ", e);
    } finally {
        DbUtils.closeQuietly(connection);
    }

    logger.info("Loaded " + triggers.size() + " triggers.");

    return triggers;
}

From source file:com.ouc.cpss.dao.BaseDao.java

/**
 * ??/* w w w. ja  v  a  2  s.c o  m*/
 *
 * @param sql
 * @param clazz
 * @return
 */
public Object get(String sql, Class clazz, Object[] params) {
    Object obj = null;
    Connection conn = null;
    try {
        conn = getConnection();
        QueryRunner qRunner = new QueryRunner();
        obj = qRunner.query(conn, sql, new BeanHandler(clazz), params);
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DbUtils.closeQuietly(conn);
    }
    return obj;
}

From source file:com.splicemachine.derby.test.framework.SpliceIndexWatcher.java

/**
 * Use this static method in cases where you want to create an index after creating/loading table.
 * TODO: redirect starting(Description) to call this method
 * @param connection/*from  www.  jav a  2 s . c  o m*/
 * @param schemaName
 * @param tableName
 * @param indexName
 * @param definition
 * @param unique
 * @throws Exception
 */
public static void createIndex(Connection connection, String schemaName, String tableName, String indexName,
        String definition, boolean unique) throws Exception {
    PreparedStatement statement = null;
    ResultSet rs = null;
    try {
        //            connection = SpliceNetConnection.getConnection();
        statement = connection.prepareStatement(SELECT_SPECIFIC_INDEX);
        statement.setString(1, schemaName);
        statement.setString(2, indexName);
        rs = statement.executeQuery();
        if (rs.next()) {
            SpliceIndexWatcher.executeDrop(connection, schemaName, indexName);
        }
        try (Statement s = connection.createStatement()) {
            System.out.println(String.format("create " + (unique ? "unique" : "") + " index %s.%s on %s.%s %s",
                    schemaName, indexName, schemaName, tableName, definition));
            s.execute(String.format("create " + (unique ? "unique" : "") + " index %s.%s on %s.%s %s",
                    schemaName, indexName, schemaName, tableName, definition));
        }
    } finally {
        DbUtils.closeQuietly(rs);
        DbUtils.closeQuietly(statement);
    }
}

From source file:com.sql.WebHistory.java

public static List<WebHistoryModel> getWebHistoryList() {
    List<WebHistoryModel> list = new ArrayList();
    Connection conn = null;/*from ww w.  j  a va  2 s.c o m*/
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        conn = DBConnection.connectToDB();

        String subSQL = "SELECT TOP 1 "
                + "(ISNULL(CaseParty.lastName,'') + ', ' + ISNULL(CaseParty.firstname,'')) " + "FROM CaseParty "
                + "WHERE CaseParty.caseRelation = 'Appellant' " + "AND (CaseParty.caseYear = Activity.caseYear "
                + "AND CaseParty.caseType = Activity.caseType "
                + "AND CaseParty.caseMonth = Activity.caseMonth "
                + "AND CaseParty.caseNumber = Activity.caseNumber)";

        String sql = "SELECT " + "RIGHT(Activity.CaseYear, 2) AS caseYear, "
                + "LEFT(Activity.CaseMonth, 2) AS caseMonth, " + "Activity.CaseNumber, "
                + "LEFT(Activity.caseType, 3) AS caseType, " + "CONVERT(char(10), Activity.date,126) as date, "
                + "LEFT((" + subSQL + "), 39) AS Appellant, "
                + "LEFT(Activity.action + ' ' + ISNULL(Activity.comment,''), 54) AS action " + "FROM "
                + "Activity INNER JOIN CaseType ON " + "Activity.caseType = CaseType.caseType " + "WHERE "
                + "((Activity.action NOT LIKE '%CREATED ON%' " + "AND Activity.action NOT LIKE '%Generated%' "
                //+ "AND Activity.action NOT LIKE '%Case was Filed and Started%' " <-- removed at JB request R3-009
                + "AND Activity.action NOT LIKE 'Added %' " + "AND Activity.action NOT LIKE 'Set %' "
                + "AND Activity.action NOT LIKE 'Changed %' " + "AND Activity.action NOT LIKE 'Ebody %' "
                + "AND Activity.action NOT LIKE 'Removed %' " + "AND Activity.type != 'I' "
                + "AND Activity.type != 'L' " + "AND Activity.type != 'U' " + "AND Activity.type != 'W') "
                + "OR (Activity.type = 'A'   AND Activity.action LIKE '%initial case preparation%') "
                + "OR (Activity.type = 'Z'   AND Activity.action LIKE '%initial case preparation%') "
                + "OR (Activity.type = 'E'   AND Activity.action LIKE '%Procedural Order & Questionnaire Mailed%') "
                + "OR (Activity.type = 'O'   AND Activity.action LIKE '%Procedural Order & Questionnaire Mailed%') "
                + "OR (Activity.type = 'Z'   AND Activity.action LIKE '%Pre Hearing Scheduled%') "
                + "OR (Activity.type = 'Z'   AND Activity.action LIKE '%Record Hearing Scheduled%') "
                + "OR (Activity.type = 'E'   AND Activity.action LIKE '%Procedural Order Mailed%') "
                + "OR (Activity.type = 'Z'   AND Activity.action LIKE '%Status Conference Scheduled%') "
                + "OR (Activity.type = 'Z'   AND Activity.action LIKE '%Notice Of Appeal Filed%') "
                + "OR (Activity.type = 'A'   AND Activity.action LIKE '%Notice of Appearance%') "
                + "OR (Activity.type = 'E'   AND Activity.action LIKE '%Procedural Order Mailed%') "
                + "OR (Activity.type = 'A'   AND Activity.action LIKE '%Notice of Withdrawal Of Counsel%') "
                + "OR (Activity.type = 'Z'   AND Activity.action LIKE '%Miscellaneous Witness And Documents List%')"
                + "OR (Activity.type IS NULL AND Activity.action LIKE '%Case was Filed and Started%')) "
                + "AND (Activity.caseYear > YEAR(GETDATE()) - 5) " + "AND Activity.active = 1 "
                + "AND CaseType.section = 'CMDS' " + "ORDER BY Activity.caseyear, Activity.caseNumber";
        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();
        while (rs.next()) {
            WebHistoryModel item = new WebHistoryModel();
            item.setCaseYear(rs.getString("CaseYear") == null ? "" : rs.getString("CaseYear"));
            item.setCaseType(rs.getString("caseType") == null ? "" : rs.getString("caseType"));
            item.setCaseMonth(rs.getString("caseMonth") == null ? "" : rs.getString("caseMonth"));
            item.setCaseNumber(rs.getString("CaseNumber") == null ? "" : rs.getString("CaseNumber"));
            item.setAppellant(rs.getString("Appellant") == null ? "" : rs.getString("Appellant"));
            item.setEntryDate(rs.getString("date") == null ? "" : rs.getString("date"));
            item.setEntryDescription(rs.getString("action") == null ? "" : rs.getString("action"));
            list.add(item);
        }
    } catch (SQLException ex) {
        Logger.getLogger(WebHistory.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        DbUtils.closeQuietly(conn);
        DbUtils.closeQuietly(ps);
        DbUtils.closeQuietly(rs);
    }
    return list;
}

From source file:com.che.software.testato.domain.dao.jdbc.impl.ScriptDAO.java

/**
 * Creates the scripts for a given hierarchy.
 * /*from  w w  w.j a  v a2  s . c o  m*/
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @param hierarchyId the hierarchy id.
 * @param scripts the scripts to create.
 * @since July, 2011.
 * @throws ScriptCreationDAOException if an error occurs during the
 *         creation.
 */
@Override
public void createScriptsFromHierarchy(int hierarchyId, List<ScriptCreation> scripts)
        throws ScriptCreationDAOException {
    LOGGER.debug("createScriptsFromHierarchy(" + hierarchyId + "," + scripts.size() + " scripts).");
    Connection connection = null;
    try {
        connection = getDataSource().getConnection();
        connection.setAutoCommit(false);
        for (ScriptCreation script : scripts) {
            getQueryRunner().update(connection,
                    "INSERT INTO script(script_id, hierarchy_id, label, depth) VALUES(nextval('script_seq'),?,'', ?) ",
                    new Object[] { hierarchyId, 1 });
            Integer createdScript = (Integer) getQueryRunner().query(connection,
                    "SELECT MAX(script_id)::int AS scriptId FROM script ", new ScalarHandler("scriptId"));
            createItems(connection, script.getScriptArrows(), createdScript, 1, hierarchyId);
        }
        connection.commit();
    } catch (SQLException e) {
        try {
            connection.rollback();
        } catch (SQLException e1) {
            throw new ScriptCreationDAOException(e1);
        }
        throw new ScriptCreationDAOException(e);
    } finally {
        if (null != connection) {
            DbUtils.closeQuietly(connection);
        }
    }
}

From source file:com.akman.excel.view.frmExportExcel.java

public BufferedImage getSignature() {
    Connection conn = Javaconnect.ConnecrDb();
    PreparedStatement pst = null;
    ResultSet rs = null;//from w ww.  j a  v  a  2 s  . c o m

    BufferedImage img = null;
    try {

        String sql = "SELECT MAX(ID), Image FROM ExcelData";

        pst = conn.prepareStatement(sql);

        rs = pst.executeQuery();

        if (rs.next()) {

            byte[] blob = rs.getBytes("Image");
            InputStream in = new ByteArrayInputStream(blob);
            img = ImageIO.read(in);

        }

    } catch (SQLException ex) {
        Logger.getLogger(frmSelectImage.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(frmExportExcel.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        DbUtils.closeQuietly(rs);
        DbUtils.closeQuietly(pst);
        DbUtils.closeQuietly(conn);
    }

    return img;
}