Java Utililty Methods DataSource

List of utility methods to do DataSource

Description

The list of methods to do DataSource are organized into topic(s).

Method

booleanisDataSourceAvailable(String dataSourceName)
is Data Source Available
try {
    DataSource.class.cast(new InitialContext().lookup(dataSourceName));
    return true;
} catch (NamingException e) {
    return false;
voidlogQueryResultToFile(DataSource dataSource, String path, String sqlStatement, Object... boundVariables)
log Query Result To File
try {
    Connection connection = dataSource.getConnection();
    PreparedStatement statement = connection.prepareStatement(sqlStatement);
    bindValues(statement, boundVariables);
    writeResultSetToFile(path, statement.executeQuery());
} catch (SQLException e) {
    e.printStackTrace();
DataSourcelookupDataSource(String dataSourceName)
lookup Data Source
try {
    return (DataSource) InitialContext.doLookup(dataSourceName);
} catch (Exception e) {
    throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e);
DataSourcelookupDataSource(String dataSourceName, final Hashtable jndiProperties)
lookup Data Source
try {
    if (jndiProperties == null || jndiProperties.isEmpty()) {
        return (DataSource) InitialContext.doLookup(dataSourceName);
    final InitialContext context = new InitialContext(jndiProperties);
    return (DataSource) context.doLookup(dataSourceName);
} catch (Exception e) {
    throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e);
...
DataSourcemyDataSource(String username, String password)
my Data Source
InitialContext cxt;
DataSource ds = null;
try {
    cxt = new InitialContext();
    ds = (DataSource) cxt.lookup("java:/comp/env/jdbc/scuola247");
} catch (NamingException e) {
    throw new SQLException(e);
return ds;
voidpurgeTables(DataSource ds)
Purge the sample's tables
try (Connection cn = ds.getConnection()) {
    Statement stmt = cn.createStatement();
    stmt.execute("DELETE FROM persons");
voidregisterSpringDataSource(String name, DataSource dataSource)
register Spring Data Source
SPRING_DATASOURCES.put(name, dataSource);
intselectCount(DataSource ds, String tableName)
select Count
try (Connection conn = ds.getConnection()) {
    ResultSet resultset = conn.createStatement()
            .executeQuery(String.format("SELECT count(1) FROM %s", tableName));
    if (resultset.next()) {
        return resultset.getInt(1);
    } else {
        return Integer.MIN_VALUE;
} catch (SQLException sqle) {
    throw new RuntimeException("select failed", sqle);
voidsetUpJndiDatasource()
set Up Jndi Datasource
try {
    InitialContext ctx = new InitialContext();
    DataSource ds = (DataSource) ctx.lookup("jdbc." + DS_NAME);
    ctx.rebind("java:/comp/env/jdbc/" + DS_NAME, ds);
} catch (Exception ex) {
    ex.printStackTrace();
voidsqlExe(DataSource dataSource, String sql)
sql Exe
Connection conn = null;
Statement stmt = null;
try {
    conn = dataSource.getConnection();
    conn.setAutoCommit(false);
    stmt = conn.createStatement();
    stmt.execute(sql);
    conn.commit();
...