Java SQL Table getSensorsList( final Connection jdbcConnection, String tableName)

Here you can find the source of getSensorsList( final Connection jdbcConnection, String tableName)

Description

get Sensors List

License

Apache License

Declaration

public static List<String> getSensorsList(
            final Connection jdbcConnection, String tableName)
            throws SQLException 

Method Source Code

//package com.java2s;
/*//from   www.  j  a  v  a2  s . co  m
 * Copyright 2013-2015 Lakshmisha Bhat
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.sql.*;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
    private static final String PREDICATE = " WHERE polltime > '2013-12-15' ";

    public static List<String> getSensorsList(
            final Connection jdbcConnection, String tableName)
            throws SQLException {
        final List<String> sensors = new ArrayList<String>();
        ResultSet rs = null;
        Statement stmt = null;
        try {
            stmt = jdbcConnection.createStatement();
            String totalSensors = MessageFormat.format(
                    "SELECT DISTINCT(sensor) FROM {0} {1}", tableName,
                    PREDICATE);
            rs = stmt.executeQuery(totalSensors);
            while (rs.next()) {
                sensors.add(rs.getString("sensor"));
            }
        } finally {
            cleanupAfterQuery(rs, stmt);
        }
        Collections.sort(sensors);
        return sensors;
    }

    private static void cleanupAfterQuery(final ResultSet rs,
            final Statement stmt) throws SQLException {
        if (rs != null) {
            rs.close();
        }
        if (stmt != null) {
            stmt.close();
        }
    }
}

Related

  1. getImmutableDefaults()
  2. getLastCreatedEntry(Connection conn, String tableName)
  3. getMetaDataMap(Statement statement, String tableOrViewName)
  4. getNextId(Connection con, String table, String identityFieldName)
  5. getRowCount(Statement stmt, String tableName)
  6. getShapeText(Connection conn, String tableName)
  7. getTableFields(Connection con, String tableName)
  8. getTableList(Connection connection)
  9. getTableName(String sql)