Java DataSource selectCount(DataSource ds, String tableName)

Here you can find the source of selectCount(DataSource ds, String tableName)

Description

select Count

License

Open Source License

Declaration

public static int selectCount(DataSource ds, String tableName) 

Method Source Code


//package com.java2s;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Main {
    public static int selectCount(DataSource ds, String tableName) {
        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;
            }//  w w  w .jav  a2 s  .c o  m
        } catch (SQLException sqle) {
            throw new RuntimeException("select failed", sqle);
        }
    }
}

Related

  1. lookupDataSource(String dataSourceName)
  2. lookupDataSource(String dataSourceName, final Hashtable jndiProperties)
  3. myDataSource(String username, String password)
  4. purgeTables(DataSource ds)
  5. registerSpringDataSource(String name, DataSource dataSource)
  6. setUpJndiDatasource()
  7. sqlExe(DataSource dataSource, String sql)
  8. tableRecommendationExistsHsqdb(DataSource dataSource)
  9. update(DataSource dataSource, String sql, Object params[])