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

ConnectiongetJNDIConnectionByContainer(String dataSource)
GetConnection with jdbc/xxxDS
try {
    Connection conn = null;
    InitialContext ctx = new InitialContext();
    if (ctx != null) {
        Object o = ctx.lookup(dataSource);
        DataSource ds = (DataSource) o;
        conn = ds.getConnection();
    return conn;
} catch (Exception e) {
    e.printStackTrace();
    return null;
intgetProductAllCount(DataSource ds, String key, String sql_allcount)
get Product All Count
Connection c = null;
PreparedStatement ps = null;
ResultSet rs = null;
int ret = 0;
try {
    c = ds.getConnection();
    ps = c.prepareStatement(sql_allcount, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    ps.setString(1, key);
...
StringgetSchemaSeparator(DataSource dataSource)
Retrieve the schema separator for the database associated with the specified data source.
try (Connection connection = dataSource.getConnection()) {
    DatabaseMetaData metaData = connection.getMetaData();
    String schemaSeparator = metaData.getCatalogSeparator();
    if ((schemaSeparator == null) || (schemaSeparator.length() == 0)) {
        schemaSeparator = ".";
    return schemaSeparator;
intgetTableColumnCount(DataSource dataSource, String selectedTable)
get Table Column Count
Connection connection = null;
ResultSet rs = null;
try {
    connection = dataSource.getConnection();
    DatabaseMetaData meta = connection.getMetaData();
    rs = meta.getColumns(null, null, selectedTable, null);
    rs.last();
    return rs.getRow();
...
ListgetTables(DataSource datasource)
get Tables
if (_tnames != null) {
    return _tnames;
_tnames = new CopyOnWriteArrayList<String>();
Connection conn = null;
ResultSet rs = null;
try {
    conn = datasource.getConnection();
...
booleanhasTable(DataSource ds, String table)
Checks if the given database contains a table with the given name.
try (Connection conn = ds.getConnection()) {
    return hasTable(conn, table);
voidinitDataSource()
init Data Source
Context initContext = new InitialContext();
Context context = (Context) initContext.lookup("java:comp/env");
DataSource ds = (javax.sql.DataSource) context.lookup("jdbc/webrod");
dataSource = ds;
DataSourceinitDatasource(String jndiName)
init Datasource
try {
    javax.naming.Context ctx = new InitialContext();
    javax.naming.Context envCtx = (javax.naming.Context) ctx.lookup("java:comp/env");
    final DataSource ds = (DataSource) envCtx.lookup(jndiName);
    return new DataSource() {
        @Override
        public Connection getConnection() throws SQLException {
            Connection conn = ds.getConnection();
...
voidinitDb(DataSource ds)
Initialize the sample's database.
createTables(ds);
purgeTables(ds);
voidinitializeDataSource()
initialize Data Source
if (ussdDatasource != null) {
    return;
String statdataSourceName = "jdbc/CONNECT_DB";
if (statdataSourceName != null) {
    try {
        Context ctx = new InitialContext();
        ussdDatasource = (DataSource) ctx.lookup(statdataSourceName);
...