List of utility methods to do DataSource
boolean | isDataSourceAvailable(String dataSourceName) is Data Source Available try { DataSource.class.cast(new InitialContext().lookup(dataSourceName)); return true; } catch (NamingException e) { return false; |
void | logQueryResultToFile(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(); |
DataSource | lookupDataSource(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); |
DataSource | lookupDataSource(String dataSourceName, final Hashtable 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); ... |
DataSource | myDataSource(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; |
void | purgeTables(DataSource ds) Purge the sample's tables try (Connection cn = ds.getConnection()) { Statement stmt = cn.createStatement(); stmt.execute("DELETE FROM persons"); |
void | registerSpringDataSource(String name, DataSource dataSource) register Spring Data Source SPRING_DATASOURCES.put(name, dataSource); |
int | selectCount(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); |
void | setUpJndiDatasource() 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(); |
void | sqlExe(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();
...
|