Java Code Examples for org.apache.commons.dbutils.DbUtils#closeQuietly()

The following examples show how to use org.apache.commons.dbutils.DbUtils#closeQuietly() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RMBController.java    From NationStatesPlusPlus with MIT License 6 votes vote down vote up
private int getRMBCommentNation(Connection conn, int commentId) throws SQLException {
	PreparedStatement select = null;
	ResultSet result = null;
	try {
		select = conn.prepareStatement("SELECT nation_id FROM assembly.rmb_comments WHERE id = ?");
		select.setInt(1, commentId);
		result = select.executeQuery();
		if (result.next()) {
			return result.getInt(1);
		}
		return -1;
	} finally {
		DbUtils.closeQuietly(result);
		DbUtils.closeQuietly(select);
	}
}
 
Example 2
Source File: WorldAssemblyController.java    From NationStatesPlusPlus with MIT License 6 votes vote down vote up
public Result getWADelegates() throws SQLException {
	List<String> delegates = new ArrayList<String>();
	Connection conn = null; 
	try {
		conn = getConnection();
		PreparedStatement select = conn.prepareStatement("SELECT delegate FROM assembly.region WHERE delegate <> \"0\" AND alive = 1");
		ResultSet set = select.executeQuery();
		while(set.next()) {
			delegates.add(set.getString(1));
		}
		DbUtils.closeQuietly(set);
		DbUtils.closeQuietly(select);
	} finally {
		DbUtils.closeQuietly(conn);
	}
	Result result = Utils.handleDefaultGetHeaders(request(), response(), String.valueOf(delegates.hashCode()));
	if (result != null) {
		return result;
	}
	return ok(Json.toJson(delegates)).as("application/json");
}
 
Example 3
Source File: SpliceViewWatcher.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
public static void executeDrop(String schemaName,String viewName) {
    LOG.trace("executeDrop");
    Connection connection = null;
    Statement statement = null;
    try {
        connection = SpliceNetConnection.getConnection();
        statement = connection.createStatement();
        statement.execute("drop view " + schemaName.toUpperCase() + "." + viewName.toUpperCase());
        connection.commit();
    } catch (Exception e) {
        LOG.error("error Dropping " + e.getMessage());
        e.printStackTrace(System.err);
        throw new RuntimeException(e);
    } finally {
        DbUtils.closeQuietly(statement);
        DbUtils.commitAndCloseQuietly(connection);
    }
}
 
Example 4
Source File: ScriptRunner.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
    * Executes an array of statements againt a connection.
    * Note that it will *WILL NOT* close the connection, commit or roll back.
    */
   protected void executeStatements(String[] statements, Connection newConn) throws DeployException {
Statement stmt = null;
try {
    stmt = newConn.createStatement();
    for (int i = 0, length = statements.length; i < length; i++) {
	stmt.addBatch(statements[i]);
    }

    stmt.executeBatch();

} catch (SQLException sqlex) {
    throw new DeployException("Could not execute statements", sqlex);
} finally {
    DbUtils.closeQuietly(stmt);
}
   }
 
Example 5
Source File: RegionController.java    From NationStatesPlusPlus with MIT License 6 votes vote down vote up
public static JsonNode getRegionalMap(Connection conn, String region) throws SQLException {
	Map<String, String> links = new HashMap<String, String>(3);
	PreparedStatement update = conn.prepareStatement("SELECT regional_map, regional_map_preview FROM assembly.region WHERE name = ?");
	update.setString(1, Utils.sanitizeName(region));
	ResultSet set = update.executeQuery();
	if (set.next()) {
		String mapLink = set.getString(1);
		if (!set.wasNull()) {
			links.put("regional_map", mapLink);
		}
		String mapPreview = set.getString(2);
		if (!set.wasNull()) {
			links.put("regional_map_preview", mapPreview);
		}
	}
	DbUtils.closeQuietly(set);
	DbUtils.closeQuietly(update);
	return Json.toJson(links);
}
 
Example 6
Source File: RMBController.java    From NationStatesPlusPlus with MIT License 6 votes vote down vote up
@Deprecated
public static JsonNode calculatePostRatings(Connection conn, int rmbPost) throws SQLException {
	List<Map<String, String>> list = new ArrayList<Map<String, String>>();
	PreparedStatement select = conn.prepareStatement("SELECT nation_name, rating_type FROM assembly.rmb_ratings WHERE rmb_post = ?");
	select.setInt(1, rmbPost);
	ResultSet result = select.executeQuery();
	while(result.next()) {
		Map<String, String> ratings = new HashMap<String, String>(2);
		ratings.put("nation", result.getString(1));
		ratings.put("type", String.valueOf(result.getInt(2)));
		ratings.put("rmb_post", String.valueOf(rmbPost));
		list.add(ratings);
	}
	DbUtils.closeQuietly(result);
	DbUtils.closeQuietly(select);
	return Json.toJson(list);
}
 
Example 7
Source File: AbstractConnect.java    From mcg-helper with Apache License 2.0 5 votes vote down vote up
public boolean testConnect() {
    boolean result = false;
       try {
           Connection connection = dbConnect.getDataSource().getConnection();
           if (connection != null) {
               DbUtils.closeQuietly(connection);
               result = true;
           }            
       } catch (SQLException e) {
       	logger.error("测试连接出错,异常信息:", e);
           return result;
       }	    
    
    return result;
}
 
Example 8
Source File: TimestampAdminIT.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Tests SYSCS_GET_TIMESTAMP_GENERATOR_INFO system procedure.
 */
@Test
public void testGetTimestampGeneratorInfo() throws Exception {
	String template = "call SYSCS_UTIL.SYSCS_GET_TIMESTAMP_GENERATOR_INFO()";
    CallableStatement cs = methodWatcher.prepareCall(template);
    ResultSet rs = cs.executeQuery();
    int rowCount = 0;
    while (rs.next()) {
    	rowCount++;
    	long num = rs.getLong(1);
        Assert.assertTrue("Unexpected number of timestamps", num > 0);
    }
    Assert.assertTrue(rowCount == 1);
    DbUtils.closeQuietly(rs);
}
 
Example 9
Source File: NewspaperController.java    From NationStatesPlusPlus with MIT License 5 votes vote down vote up
public Result administrateNewspaper(int newspaper) throws SQLException {
	Result result = Utils.validateRequest(request(), response(), getAPI(), getDatabase());
	if (result != null) {
		return result;
	}
	String nation = Utils.getPostValue(request(), "nation");
	String title = Utils.getPostValue(request(), "title");
	String byline = Utils.getPostValue(request(), "byline");
	String columns = Utils.getPostValue(request(), "columns");

	Utils.handleDefaultPostHeaders(request(), response());
	if (title == null || title.length() > 255 || byline == null || byline.length() > 255) {
		return Results.badRequest();
	}
	Connection conn = null;
	try {
		conn = getConnection();

		if (!isEditorInChief(newspaper, nation, conn)) {
			Utils.handleDefaultPostHeaders(request(), response());
			return Results.unauthorized();
		}

		try (PreparedStatement update = conn.prepareStatement("UPDATE assembly.newspapers SET title = ?, byline = ?" + (columns != null ? ", newspapers.columns = ?" : "") + " WHERE id = ?")) {
			update.setString(1, title);
			update.setString(2, byline);
			if (columns != null) {
				update.setInt(3, Math.max(1, Math.min(3, Integer.parseInt(columns))));
				update.setInt(4, newspaper);
			} else {
				update.setInt(3, newspaper);
			}
			update.executeUpdate();
		}
	} finally {
		DbUtils.closeQuietly(conn);
	}
	return Results.ok();
}
 
Example 10
Source File: CallStatementOperationIT.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testCallIndexInfo() throws Exception {
    	DatabaseMetaData dmd = methodWatcher.getOrCreateConnection().getMetaData();
    	ResultSet rs = dmd.getIndexInfo(null,"SYS","SYSSCHEMAS",false,true);
        while(rs.next()){ // TODO No Test
        }
        DbUtils.closeQuietly(rs);
}
 
Example 11
Source File: RegionController.java    From NationStatesPlusPlus with MIT License 5 votes vote down vote up
public Result getRegionalMap(String region) throws SQLException {
	Connection conn = null;
	try {
		conn = getConnection();
		JsonNode map = getRegionalMap(conn, region);
		Result result = Utils.handleDefaultGetHeaders(request(), response(), String.valueOf(map.hashCode()), "60");
		if (result != null) {
			return result;
		}
		return Results.ok(Json.toJson(map)).as("application/json");
	} finally {
		DbUtils.closeQuietly(conn);
	}
}
 
Example 12
Source File: ToolDBDeployTask.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void updateToolDefaultContentId(long toolId, long defaultContentId, Connection conn)
    throws DeployException {
PreparedStatement stmt = null;
try {
    stmt = conn.prepareStatement("UPDATE lams_tool SET default_tool_content_id = ? WHERE tool_id = ?");
    stmt.setLong(1, defaultContentId);
    stmt.setLong(2, toolId);
    stmt.execute();

} catch (SQLException sqlex) {
    throw new DeployException("Could not update default content id into tool", sqlex);
} finally {
    DbUtils.closeQuietly(stmt);
}
   }
 
Example 13
Source File: ToolDBDeployTask.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void updateToolLibraryId(long newToolId, long libraryId, Connection conn) throws DeployException {
PreparedStatement stmt = null;
try {
    stmt = conn.prepareStatement("UPDATE lams_tool SET learning_library_id = ? WHERE tool_id = ?");
    stmt.setLong(1, libraryId);
    stmt.setLong(2, newToolId);
    stmt.execute();

} catch (SQLException sqlex) {
    throw new DeployException("Could not update library id into tool", sqlex);
} finally {
    DbUtils.closeQuietly(stmt);
}
   }
 
Example 14
Source File: MultiEnvDeployScenarioTest.java    From obevo with Apache License 2.0 5 votes vote down vote up
private void validateInstance(String instanceName, String schemaName) throws Exception {
    DataSource ds = JdbcDataSourceFactory.createFromJdbcUrl(org.h2.Driver.class, H2JdbcDataSourceFactory.getUrl(instanceName, false), new Credential("sa", ""));
    JdbcHelper jdbc = new JdbcHelper();

    Connection conn = ds.getConnection();
    try {
        int count = jdbc.queryForInt(conn, "select count(*) from " + schemaName + ".TABLE_A where 1=0");
        assertEquals("Expecting a successful return for " + instanceName + " and schemaName as the table should exist",
                0, count);
    } finally {
        DbUtils.closeQuietly(conn);
    }
}
 
Example 15
Source File: EmbeddedCallStatementOperationIT.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testCallGetIndexInfo() throws Exception{
    DatabaseMetaData dmd = methodWatcher.getOrCreateConnection().getMetaData();
    ResultSet rs = dmd.getIndexInfo(null, "SYS", "SYSSCHEMAS", false, true);
    int count = 0;
    while(rs.next()){
    	count++;
        LOG.trace(rs.getString(1));
    }
    Assert.assertTrue(count > 0);
    DbUtils.closeQuietly(rs);
}
 
Example 16
Source File: SpliceViewWatcher.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void starting(Description description) {
    LOG.trace("Starting");
    Connection connection = null;
    Statement statement = null;
    ResultSet rs = null;
    try {
        connection = userName==null?SpliceNetConnection.getConnection():SpliceNetConnection.getConnectionAs(userName,password);
        rs = connection.getMetaData().getTables(null, schemaName, viewName, null);
        if (rs.next()) {
            executeDrop(schemaName, viewName);
        }
        connection.commit();
        statement = connection.createStatement();
        statement.execute(CREATE_VIEW + schemaName + "." + viewName + " " + createString);
        connection.commit();
    } catch (Exception e) {
        LOG.error("Create view statement is invalid ");
        e.printStackTrace(System.err);
        throw new RuntimeException(e);
    } finally {
        DbUtils.closeQuietly(rs);
        DbUtils.closeQuietly(statement);
        DbUtils.commitAndCloseQuietly(connection);
    }
    super.starting(description);
}
 
Example 17
Source File: DbUtilsUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@After
public void closeBD() {
    DbUtils.closeQuietly(connection);
}
 
Example 18
Source File: HappeningsTask.java    From NationStatesPlusPlus with MIT License 4 votes vote down vote up
private void joinWorldAssembly(Connection conn, int nationId) throws SQLException {
	PreparedStatement endorsements = conn.prepareStatement("UPDATE assembly.nation SET wa_member = 1 WHERE id = ?");
	endorsements.setInt(1, nationId);
	endorsements.executeUpdate();
	DbUtils.closeQuietly(endorsements);
}
 
Example 19
Source File: CsvStaticDataWriterTest.java    From obevo with Apache License 2.0 4 votes vote down vote up
@After
public void teardown() throws Exception {
    DbUtils.closeQuietly(conn);
}
 
Example 20
Source File: MetadataGroupTest.java    From obevo with Apache License 2.0 4 votes vote down vote up
@After
public void teardown() {
    DbUtils.closeQuietly(conn);
}