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

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

Introduction

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

Prototype

public static void close(Statement stmt) throws SQLException 

Source Link

Document

Close a Statement, avoid closing if null.

Usage

From source file:org.silverpeas.dbbuilder.sql.QueryExecutor.java

public static void executeUpdate(Connection connection, String query) throws SQLException {
    Statement stmt = connection.createStatement();
    try {/*  w  w  w  .  j a va  2s  .c o  m*/
        stmt.executeUpdate(query);
    } catch (SQLException e) {
        throw e;
    } finally {
        DbUtils.close(stmt);
    }
}

From source file:org.silverpeas.migration.classified.XmlFormImagesToAttachment.java

public Collection<String> getListClassifiedInstance() throws SQLException {
    Statement stmt = null;//from w ww . j a va2s.  com
    ResultSet rs = null;
    Collection<String> listInstanceId = new ArrayList<String>();
    try {
        stmt = getConnection().createStatement();
        rs = stmt.executeQuery("SELECT id FROM st_componentinstance where componentname='classifieds'");
        while (rs.next()) {
            String instanceId = rs.getString("id");
            listInstanceId.add(instanceId);
        }
        return listInstanceId;
    } finally {
        DbUtils.closeQuietly(rs);
        DbUtils.close(stmt);
    }
}

From source file:org.silverpeas.migration.classified.XmlFormImagesToAttachment.java

public String getXMLFormName(int instanceId) throws SQLException {
    PreparedStatement pstmt = null;
    ResultSet rs = null;/*from   w w  w.j ava 2  s . c om*/
    String xmlFormName = null;
    try {
        pstmt = getConnection().prepareStatement(
                "SELECT value FROM st_instance_data where componentid = ? and name='XMLFormName'");
        pstmt.setInt(1, instanceId);
        rs = pstmt.executeQuery();
        if (rs.next()) {
            xmlFormName = rs.getString("value");
        }
        return xmlFormName;
    } finally {
        DbUtils.closeQuietly(rs);
        DbUtils.close(pstmt);
    }
}

From source file:org.silverpeas.migration.classified.XmlFormImagesToAttachment.java

public Collection<Integer> getListTemplate(String xmlFormName, String instanceId) throws SQLException {
    PreparedStatement pstmt = null;
    ResultSet rs = null;/* w w  w.  j a va 2 s .  c  o  m*/
    Collection<Integer> listTemplateId = new ArrayList<Integer>();
    try {
        pstmt = getConnection().prepareStatement(
                "SELECT templateId FROM sb_formtemplate_template where templateName=? and externalId like ?");
        pstmt.setString(1, xmlFormName);
        pstmt.setString(2, "classifieds" + instanceId + "%");
        rs = pstmt.executeQuery();

        while (rs.next()) {
            int templateId = rs.getInt("templateId");
            listTemplateId.add(new Integer(templateId));
        }
        return listTemplateId;
    } finally {
        DbUtils.closeQuietly(rs);
        DbUtils.close(pstmt);
    }
}

From source file:org.silverpeas.migration.classified.XmlFormImagesToAttachment.java

public Collection<RecordTemplate> getListRecord(int templateId) throws SQLException {
    PreparedStatement pstmt = null;
    ResultSet rs = null;//from  www  .j  a  va 2s. c o m
    Collection<RecordTemplate> listRecord = new ArrayList<RecordTemplate>();
    try {
        pstmt = getConnection().prepareStatement(
                "SELECT recordId, externalId FROM sb_formtemplate_record where templateid = ?");
        pstmt.setInt(1, templateId);
        rs = pstmt.executeQuery();

        while (rs.next()) {
            int recordId = rs.getInt("recordId");
            String externalId = rs.getString("externalId");
            RecordTemplate recordTemplate = new RecordTemplate(recordId, externalId);
            listRecord.add(recordTemplate);
        }
        return listRecord;
    } finally {
        DbUtils.closeQuietly(rs);
        DbUtils.close(pstmt);
    }
}

From source file:org.silverpeas.migration.classified.XmlFormImagesToAttachment.java

public Collection<FieldTemplate> getListValue(int recordId) throws SQLException {
    PreparedStatement pstmt = null;
    ResultSet rs = null;/*  w  ww  .j  av a2 s . c o m*/
    Collection<FieldTemplate> listValue = new ArrayList<FieldTemplate>();
    try {
        pstmt = getConnection().prepareStatement(
                "SELECT fieldName, fieldValue FROM sb_formtemplate_textfield where recordid = ?");
        pstmt.setInt(1, recordId);
        rs = pstmt.executeQuery();

        while (rs.next()) {
            String fieldName = rs.getString("fieldName");
            String fieldValue = rs.getString("fieldValue");
            FieldTemplate fieldTemplate = new FieldTemplate(fieldName, fieldValue);
            listValue.add(fieldTemplate);
        }
        return listValue;
    } finally {
        DbUtils.closeQuietly(rs);
        DbUtils.close(pstmt);
    }
}

From source file:org.silverpeas.migration.classified.XmlFormImagesToAttachment.java

public void updateClassified(String instanceId, int classifiedId, String description) throws SQLException {
    PreparedStatement pstmt = null;
    try {/*w w  w. j a  v a  2 s.c om*/
        pstmt = getConnection().prepareStatement(
                "UPDATE SC_Classifieds_Classifieds set description = ? where instanceId = ? and classifiedId = ? ");
        pstmt.setString(1, description);
        pstmt.setString(2, "classifieds" + instanceId);
        pstmt.setInt(3, classifiedId);
        pstmt.executeUpdate();
    } finally {
        DbUtils.close(pstmt);
    }
}

From source file:org.silverpeas.migration.classified.XmlFormImagesToAttachment.java

public void deleteDescriptionValue(int recordId) throws SQLException {
    PreparedStatement pstmt = null;
    try {//from   ww  w  .j a  v a  2  s .  co m
        pstmt = getConnection().prepareStatement(
                "DELETE FROM sb_formtemplate_textfield where recordId = ? and fieldName = 'description'");
        pstmt.setInt(1, recordId);
        pstmt.executeUpdate();
    } finally {
        DbUtils.close(pstmt);
    }
}

From source file:org.silverpeas.migration.classified.XmlFormImagesToAttachment.java

public void deletePhotoValue(int recordId, String fieldPhotoName) throws SQLException {
    PreparedStatement pstmt = null;
    try {//from  w  ww . ja  v  a2 s . c  o  m
        pstmt = getConnection()
                .prepareStatement("DELETE FROM sb_formtemplate_textfield where recordId = ? and fieldName = ?");
        pstmt.setInt(1, recordId);
        pstmt.setString(2, fieldPhotoName);
        pstmt.executeUpdate();
    } finally {
        DbUtils.close(pstmt);
    }
}

From source file:org.sonar.db.version.v50.FeedFileSourcesTest.java

@Test
public void migrate_sources_with_invalid_duplication() throws Exception {
    db.prepareDbUnit(getClass(), "before.xml");

    Connection connection = null;
    PreparedStatement duplicationDataStmt = null;
    try {/*  w  w  w .  ja  v a2s  .  co m*/
        connection = db.openConnection();

        connection
                .prepareStatement("insert into snapshot_sources " + "(snapshot_id, data, updated_at) "
                        + "values " + "(6, 'class Foo {\r\n  // Empty\r\n}\r\n', '2014-10-31 16:44:02.000')")
                .executeUpdate();

        db.executeUpdateSql("insert into snapshot_sources " + "(snapshot_id, data, updated_at) " + "values "
                + "(7, '', '2014-10-31 16:44:02.000')");

        duplicationDataStmt = connection.prepareStatement("insert into project_measures "
                + "(metric_id, snapshot_id, text_value) " + "values " + "(13, 6, ?)");
        duplicationDataStmt.setBytes(1,
                "<duplications><g><b s=\"1\" l=\"1\" r=\"MyProject:src/main/xoo/prj/MyFile.xoo\"/><b s=\"2\" l=\"1\" r=\"MyProject:src/main/xoo/prj/MyFile.xoo\"/><b s=\"3\" l=\"1\" r=\"MyProject:src/main/xoo/prj/AnotherFile.xoo\"/"
                        .getBytes(StandardCharsets.UTF_8));
        duplicationDataStmt.executeUpdate();
    } finally {
        DbUtils.close(duplicationDataStmt);
        DbUtils.commitAndCloseQuietly(connection);
    }

    migration.execute();

    // db.assertDbUnit(getClass(), "after-with-invalid-duplication.xml", "file_sources");

    List<Map<String, Object>> results = getFileSources();
    assertThat(results).hasSize(2);

    assertThat(results.get(0).get("projectUuid")).isEqualTo("uuid-MyProject");
    assertThat(results.get(0).get("fileUuid")).isEqualTo("uuid-Migrated.xoo");
    assertThat(results.get(0).get("data")).isEqualTo("");
    assertThat(results.get(0).get("lineHashes")).isEqualTo("");
    assertThat(results.get(0).get("dataHash")).isEqualTo("");
    assertThat(results.get(0).get("updatedAt")).isEqualTo(NOW);
    assertThat(results.get(0).get("createdAt")).isEqualTo(1416238020000L);

    assertThat(results.get(1).get("projectUuid")).isEqualTo("uuid-MyProject");
    assertThat(results.get(1).get("fileUuid")).isEqualTo("uuid-MyFile.xoo");
    assertThat(results.get(1).get("data")).isEqualTo(
            ",,,,,,,,,,,,,,,class Foo {\r\n,,,,,,,,,,,,,,,  // Empty\r\n,,,,,,,,,,,,,,,}\r\n,,,,,,,,,,,,,,,\r\n");
    assertThat(results.get(1).get("lineHashes")).isEqualTo(
            "6a19ce786467960a3a9b0d26383a464a\naab2dbc5fdeaa80b050b1d049ede357c\ncbb184dd8e05c9709e5dcaedaa0495cf\n\n");
    assertThat(results.get(1).get("dataHash")).isEqualTo("");
    assertThat(formatLongDate((long) results.get(1).get("updatedAt")).toString()).startsWith("2014-10-31");
    assertThat(results.get(1).get("createdAt")).isEqualTo(NOW);
}