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

voidcreateTableRecommendation(DataSource dataSource)
create Table Recommendation
try (Connection connection = dataSource.getConnection(); Statement stmt = connection.createStatement()) {
    stmt.execute("CREATE TABLE recommendation (person_id int, index_ int, skill_id int, feedback boolean)");
} catch (SQLException e) {
    throw new RuntimeException(e);
voidcreateTables(DataSource ds)
create Tables
try (Connection cn = ds.getConnection()) {
    Statement stmt = cn.createStatement();
    stmt.execute("CREATE TABLE persons " + "(" + "id INTEGER NOT NULL," + "firstname VARCHAR(40) NOT NULL,"
            + "lastname VARCHAR(40) NOT NULL," + "PRIMARY KEY (id)" + ")");
} catch (SQLException e) {
    if (e.getLocalizedMessage().contains("already exists"))
        return;
    else
...
voidcreateTables(final DataSource ds)
Creates the tables in the database
try {
    final String stmt = "create table USERS(username varchar(50), email varchar(50), Primary Key (username));";
    ds.getConnection().prepareStatement(stmt).execute();
} catch (Exception e) {
voiddoInsert(DataSource ds, String tableName, int id, String value)
do Insert
try (Connection conn = ds.getConnection()) {
    int updated = conn.createStatement()
            .executeUpdate(String.format("INSERT INTO %s (id, a) VALUES (%s,'%s')", tableName, id, value));
    System.out.println("INSERT to table " + tableName + " outcome: " + updated);
} catch (SQLException sqle) {
    throw new RuntimeException("insert failed", sqle);
ListdoSelect(DataSource ds, String tableName, int id)
do Select
try (Connection conn = ds.getConnection()) {
    ResultSet resultset = conn.createStatement().executeQuery(String
            .format("SELECT %s FROM %s WHERE %s = %s", VALUE_COLUMN_NAME, tableName, ID_COLUMN_NAME, id));
    List<String> result = new ArrayList<>();
    while (resultset.next()) {
        result.add(resultset.getString(1));
    return result;
...
intexecute(DataSource ds, String sql, Object... args)
execute
if (ds == null || sql == null || sql.trim().length() == 0)
    return 0;
int result = 0;
PreparedStatement pstmt = null;
Connection con = null;
try {
    con = ds.getConnection();
    pstmt = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
...
voidexecute(String execute, DataSource datasource)
execute
try {
    Connection connection = datasource.getConnection();
    Statement statement = connection.createStatement();
    statement.execute(execute);
    statement.close();
    connection.close();
} catch (SQLException e) {
    e.printStackTrace();
...
voidexecuteQuery(DataSource dataSource, String... queries)
execute Query
Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
for (String query : queries) {
    stmt.execute(query);
conn.close();
ConnectiongetConnection(CommonDataSource dataSource)
get Connection
Connection connection = null;
if (dataSource != null) {
    if (dataSource instanceof ConnectionPoolDataSource) {
        connection = ((ConnectionPoolDataSource) dataSource).getPooledConnection().getConnection();
    } else if (dataSource instanceof DataSource) {
        connection = ((DataSource) dataSource).getConnection();
    } else if (dataSource instanceof XADataSource) {
        connection = ((XADataSource) dataSource).getXAConnection().getConnection();
...
ConnectiongetConnection(DataSource dataSource)
get Connection
Connection conn = dataSource.getConnection();
return conn;