Java DataSource doSelect(DataSource ds, String tableName, int id)

Here you can find the source of doSelect(DataSource ds, String tableName, int id)

Description

do Select

License

Open Source License

Declaration

public static List<String> doSelect(DataSource ds, String tableName, int id) 

Method Source Code


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

public class Main {
    private static final String ID_COLUMN_NAME = "id";
    private static final String VALUE_COLUMN_NAME = "a";

    public static List<String> doSelect(DataSource ds, String tableName, int id) {
        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));
            }//  w w  w. j  a  v  a 2s  . c  om
            return result;
        } catch (SQLException sqle) {
            throw new RuntimeException("Select of id " + id + " in table " + tableName + " failed", sqle);
        }
    }
}

Related

  1. createTable(DataSource ds, String tableName)
  2. createTableRecommendation(DataSource dataSource)
  3. createTables(DataSource ds)
  4. createTables(final DataSource ds)
  5. doInsert(DataSource ds, String tableName, int id, String value)
  6. execute(DataSource ds, String sql, Object... args)
  7. execute(String execute, DataSource datasource)
  8. executeQuery(DataSource dataSource, String... queries)
  9. getConnection(CommonDataSource dataSource)