Example usage for org.springframework.jdbc.core JdbcTemplate getDataSource

List of usage examples for org.springframework.jdbc.core JdbcTemplate getDataSource

Introduction

In this page you can find the example usage for org.springframework.jdbc.core JdbcTemplate getDataSource.

Prototype

@Nullable
public DataSource getDataSource() 

Source Link

Document

Return the DataSource used by this template.

Usage

From source file:eu.databata.engine.util.PropagatorTableExport.java

private static void exportTablesData(JdbcTemplate jdbcTemplate, String delimiter, String tableNamePattern) {
    try {/*w w w .jav a2 s  . c  o  m*/
        System.out.println("Trying to load tables from metadata");
        Connection connection = jdbcTemplate.getDataSource().getConnection();
        ResultSet tables = connection.getMetaData().getTables(null, null, tableNamePattern,
                new String[] { "TABLE" });
        while (tables.next()) {
            System.out.println("Exporting data from " + tables.getString("TABLE_NAME"));
            exportData(jdbcTemplate, tables.getString("TABLE_NAME"), delimiter);
        }
    } catch (SQLException e) {
        System.out.println("\nError when trying to get table names from DB.");
    }
}

From source file:dao.MetricsDAO.java

public static ObjectNode getPagedMetrics(String dashboardName, String group, Integer page, Integer size,
        String user) {/*from  w w w  .  j  av  a 2s. c o  m*/
    Integer userId = UserDAO.getUserIDByUserName(user);

    final JdbcTemplate jdbcTemplate = getJdbcTemplate();
    javax.sql.DataSource ds = jdbcTemplate.getDataSource();
    DataSourceTransactionManager tm = new DataSourceTransactionManager(ds);

    TransactionTemplate txTemplate = new TransactionTemplate(tm);

    ObjectNode result;
    final Integer id = userId;
    result = txTemplate.execute(new TransactionCallback<ObjectNode>() {
        public ObjectNode doInTransaction(TransactionStatus status) {
            List<Map<String, Object>> rows;
            if (StringUtils.isBlank(dashboardName)) {
                rows = jdbcTemplate.queryForList(SELECT_PAGED_METRICS, id, (page - 1) * size, size);
            } else if (StringUtils.isBlank(group)) {
                String dbName;
                if (dashboardName.equals("[Other]")) {
                    dbName = null;
                } else {
                    dbName = dashboardName;
                }
                rows = jdbcTemplate.queryForList(SELECT_PAGED_METRICS_BY_DASHBOARD_NAME, id, dbName, dbName,
                        (page - 1) * size, size);
            } else {
                String dbName;
                if (dashboardName.equals("[Other]")) {
                    dbName = null;
                } else {
                    dbName = dashboardName;
                }
                String grp;
                if (group.equals("[Other]")) {
                    grp = null;
                } else {
                    grp = group;
                }
                rows = jdbcTemplate.queryForList(SELECT_PAGED_METRICS_BY_DASHBOARD_AND_GROUP, id, dbName,
                        dbName, grp, grp, (page - 1) * size, size);
            }

            List<Metric> pagedMetrics = new ArrayList<>();
            for (Map row : rows) {
                Metric metric = new Metric();
                metric.id = (int) row.get("metric_id");
                metric.name = (String) row.get("metric_name");
                metric.description = (String) row.get("metric_description");
                metric.refID = (String) row.get("metric_ref_id");
                metric.refIDType = (String) row.get("metric_ref_id_type");
                metric.dashboardName = (String) row.get("dashboard_name");
                metric.category = (String) row.get("metric_category");
                metric.group = (String) row.get("metric_group");
                metric.watchId = (Long) row.get("watch_id");
                pagedMetrics.add(metric);
            }
            long count = 0;
            try {
                count = jdbcTemplate.queryForObject("SELECT FOUND_ROWS()", Long.class);
            } catch (EmptyResultDataAccessException e) {
                Logger.error("Exception = " + e.getMessage());
            }

            ObjectNode resultNode = Json.newObject();
            resultNode.put("count", count);
            resultNode.put("page", page);
            resultNode.put("itemsPerPage", size);
            resultNode.put("totalPages", (int) Math.ceil(count / ((double) size)));
            resultNode.set("metrics", Json.toJson(pagedMetrics));

            return resultNode;
        }
    });

    return result;
}

From source file:dao.SearchDAO.java

public static ObjectNode getPagedCommentsByKeyword(String category, String keyword, int page, int size) {
    List<Dataset> pagedDatasets = new ArrayList<Dataset>();
    final JdbcTemplate jdbcTemplate = getJdbcTemplate();
    javax.sql.DataSource ds = jdbcTemplate.getDataSource();
    DataSourceTransactionManager tm = new DataSourceTransactionManager(ds);

    TransactionTemplate txTemplate = new TransactionTemplate(tm);

    ObjectNode result;/*  w ww  .  jav a  2s  . co m*/
    result = txTemplate.execute(new TransactionCallback<ObjectNode>() {
        public ObjectNode doInTransaction(TransactionStatus status) {
            List<Map<String, Object>> rows = null;
            String query = SEARCH_DATASET_BY_COMMENTS_WITH_PAGINATION.replace("$keyword", keyword);
            rows = jdbcTemplate.queryForList(query, (page - 1) * size, size);

            for (Map row : rows) {

                Dataset ds = new Dataset();
                ds.id = (long) row.get(DatasetRowMapper.DATASET_ID_COLUMN);
                ds.name = (String) row.get(DatasetRowMapper.DATASET_NAME_COLUMN);
                ds.source = (String) row.get(DatasetRowMapper.DATASET_SOURCE_COLUMN);
                ds.urn = (String) row.get(DatasetRowMapper.DATASET_URN_COLUMN);
                ds.schema = (String) row.get(DatasetRowMapper.DATASET_SCHEMA_COLUMN);
                pagedDatasets.add(ds);
            }
            long count = 0;
            try {
                count = jdbcTemplate.queryForObject("SELECT FOUND_ROWS()", Long.class);
            } catch (EmptyResultDataAccessException e) {
                Logger.error("Exception = " + e.getMessage());
            }

            ObjectNode resultNode = Json.newObject();
            resultNode.put("count", count);
            resultNode.put("page", page);
            resultNode.put("category", category);
            resultNode.put("itemsPerPage", size);
            resultNode.put("totalPages", (int) Math.ceil(count / ((double) size)));
            resultNode.set("data", Json.toJson(pagedDatasets));

            return resultNode;
        }
    });

    return result;
}

From source file:dao.SearchDAO.java

public static ObjectNode getPagedFlowByKeyword(String category, String keyword, int page, int size) {
    final List<FlowJob> pagedFlows = new ArrayList<FlowJob>();
    final JdbcTemplate jdbcTemplate = getJdbcTemplate();
    javax.sql.DataSource ds = jdbcTemplate.getDataSource();
    DataSourceTransactionManager tm = new DataSourceTransactionManager(ds);

    TransactionTemplate txTemplate = new TransactionTemplate(tm);

    ObjectNode result;//w w  w. ja v  a  2 s  .  com
    result = txTemplate.execute(new TransactionCallback<ObjectNode>() {
        public ObjectNode doInTransaction(TransactionStatus status) {
            String query = SEARCH_FLOW_WITH_PAGINATION.replace("$keyword", keyword);
            List<Map<String, Object>> rows = null;

            rows = jdbcTemplate.queryForList(query, (page - 1) * size, size);
            for (Map row : rows) {

                FlowJob flow = new FlowJob();
                flow.flowId = (Long) row.get(FlowRowMapper.FLOW_ID_COLUMN);
                flow.flowName = (String) row.get(FlowRowMapper.FLOW_NAME_COLUMN);
                flow.flowPath = (String) row.get(FlowRowMapper.FLOW_PATH_COLUMN);
                flow.flowGroup = (String) row.get(FlowRowMapper.FLOW_GROUP_COLUMN);
                flow.appCode = (String) row.get(FlowRowMapper.APP_CODE_COLUMN);
                flow.appId = (Integer) row.get(FlowRowMapper.APP_ID_COLUMN);
                flow.displayName = flow.flowName;
                flow.link = "#/flows/name/" + flow.appCode + "/" + Long.toString(flow.flowId) + "/page/1?urn="
                        + flow.flowGroup;
                flow.path = flow.appCode + "/" + flow.flowPath;
                pagedFlows.add(flow);
            }
            long count = 0;
            try {
                count = jdbcTemplate.queryForObject("SELECT FOUND_ROWS()", Long.class);
            } catch (EmptyResultDataAccessException e) {
                Logger.error("Exception = " + e.getMessage());
            }

            ObjectNode resultNode = Json.newObject();
            resultNode.put("count", count);
            resultNode.put("isFlowJob", true);
            resultNode.put("page", page);
            resultNode.put("category", category);
            resultNode.put("itemsPerPage", size);
            resultNode.put("totalPages", (int) Math.ceil(count / ((double) size)));
            resultNode.set("data", Json.toJson(pagedFlows));

            return resultNode;
        }
    });

    return result;
}

From source file:dao.SearchDAO.java

public static ObjectNode getPagedDatasetByKeyword(String category, String keyword, String source, int page,
        int size) {
    List<Dataset> pagedDatasets = new ArrayList<Dataset>();
    final JdbcTemplate jdbcTemplate = getJdbcTemplate();
    javax.sql.DataSource ds = jdbcTemplate.getDataSource();
    DataSourceTransactionManager tm = new DataSourceTransactionManager(ds);

    TransactionTemplate txTemplate = new TransactionTemplate(tm);

    ObjectNode result;//  w w  w.j  a v  a  2s  .co m
    result = txTemplate.execute(new TransactionCallback<ObjectNode>() {
        public ObjectNode doInTransaction(TransactionStatus status) {
            List<Map<String, Object>> rows = null;
            if (StringUtils.isBlank(source) || source.toLowerCase().equalsIgnoreCase("all")) {
                String query = SEARCH_DATASET_WITH_PAGINATION.replace("$keyword", keyword);
                rows = jdbcTemplate.queryForList(query, (page - 1) * size, size);
            } else {
                String query = SEARCH_DATASET_BY_SOURCE_WITH_PAGINATION.replace("$keyword", keyword);
                rows = jdbcTemplate.queryForList(query, source, (page - 1) * size, size);
            }

            for (Map row : rows) {

                Dataset ds = new Dataset();
                ds.id = (Long) row.get(DatasetRowMapper.DATASET_ID_COLUMN);
                ds.name = (String) row.get(DatasetRowMapper.DATASET_NAME_COLUMN);
                ds.source = (String) row.get(DatasetRowMapper.DATASET_SOURCE_COLUMN);
                ds.urn = (String) row.get(DatasetRowMapper.DATASET_URN_COLUMN);
                ds.schema = (String) row.get(DatasetRowMapper.DATASET_SCHEMA_COLUMN);
                pagedDatasets.add(ds);
            }
            long count = 0;
            try {
                count = jdbcTemplate.queryForObject("SELECT FOUND_ROWS()", Long.class);
            } catch (EmptyResultDataAccessException e) {
                Logger.error("Exception = " + e.getMessage());
            }

            ObjectNode resultNode = Json.newObject();
            resultNode.put("count", count);
            resultNode.put("page", page);
            resultNode.put("category", category);
            resultNode.put("source", source);
            resultNode.put("itemsPerPage", size);
            resultNode.put("totalPages", (int) Math.ceil(count / ((double) size)));
            resultNode.set("data", Json.toJson(pagedDatasets));

            return resultNode;
        }
    });

    return result;
}

From source file:dao.SearchDAO.java

public static ObjectNode getPagedJobByKeyword(String category, String keyword, int page, int size) {
    final List<FlowJob> pagedFlowJobs = new ArrayList<FlowJob>();
    final JdbcTemplate jdbcTemplate = getJdbcTemplate();
    javax.sql.DataSource ds = jdbcTemplate.getDataSource();
    DataSourceTransactionManager tm = new DataSourceTransactionManager(ds);

    TransactionTemplate txTemplate = new TransactionTemplate(tm);

    ObjectNode result;/*w  w  w.  j  a  va 2 s . co m*/
    result = txTemplate.execute(new TransactionCallback<ObjectNode>() {
        public ObjectNode doInTransaction(TransactionStatus status) {
            String query = SEARCH_JOB_WITH_PAGINATION.replace("$keyword", keyword);
            List<Map<String, Object>> rows = null;

            rows = jdbcTemplate.queryForList(query, (page - 1) * size, size);
            for (Map row : rows) {

                FlowJob flowJob = new FlowJob();
                flowJob.flowId = (Long) row.get(FlowRowMapper.FLOW_ID_COLUMN);
                flowJob.jobId = (Long) row.get(FlowRowMapper.JOB_ID_COLUMN);
                flowJob.jobName = (String) row.get(FlowRowMapper.JOB_NAME_COLUMN);
                flowJob.jobPath = (String) row.get(FlowRowMapper.JOB_PATH_COLUMN);
                flowJob.jobType = (String) row.get(FlowRowMapper.JOB_TYPE_COLUMN);
                flowJob.flowName = (String) row.get(FlowRowMapper.FLOW_NAME_COLUMN);
                flowJob.flowPath = (String) row.get(FlowRowMapper.FLOW_PATH_COLUMN);
                flowJob.flowGroup = (String) row.get(FlowRowMapper.FLOW_GROUP_COLUMN);
                flowJob.appCode = (String) row.get(FlowRowMapper.APP_CODE_COLUMN);
                flowJob.appId = (Integer) row.get(FlowRowMapper.APP_ID_COLUMN);
                flowJob.displayName = flowJob.jobName;
                flowJob.link = "#/flows/name/" + flowJob.appCode + "/" + Long.toString(flowJob.flowId)
                        + "/page/1?urn=" + flowJob.flowGroup;
                flowJob.path = flowJob.appCode + "/" + flowJob.jobPath;

                pagedFlowJobs.add(flowJob);
            }
            long count = 0;
            try {
                count = jdbcTemplate.queryForObject("SELECT FOUND_ROWS()", Long.class);
            } catch (EmptyResultDataAccessException e) {
                Logger.error("Exception = " + e.getMessage());
            }

            ObjectNode resultNode = Json.newObject();
            resultNode.put("count", count);
            resultNode.put("isFlowJob", true);
            resultNode.put("page", page);
            resultNode.put("category", category);
            resultNode.put("itemsPerPage", size);
            resultNode.put("totalPages", (int) Math.ceil(count / ((double) size)));
            resultNode.set("data", Json.toJson(pagedFlowJobs));

            return resultNode;
        }
    });

    return result;
}

From source file:dao.SearchDAO.java

public static ObjectNode getPagedMetricByKeyword(final String category, String keyword, int page, int size) {
    List<Metric> pagedMetrics = new ArrayList<Metric>();
    final JdbcTemplate jdbcTemplate = getJdbcTemplate();
    javax.sql.DataSource ds = jdbcTemplate.getDataSource();
    DataSourceTransactionManager tm = new DataSourceTransactionManager(ds);

    TransactionTemplate txTemplate = new TransactionTemplate(tm);

    ObjectNode result;/*from w w w.j a  v a  2s  .co  m*/
    result = txTemplate.execute(new TransactionCallback<ObjectNode>() {
        public ObjectNode doInTransaction(TransactionStatus status) {
            String query = SEARCH_METRIC_WITH_PAGINATION.replace("$keyword", keyword);
            List<Map<String, Object>> rows = null;
            rows = jdbcTemplate.queryForList(query, (page - 1) * size, size);
            for (Map row : rows) {

                Metric metric = new Metric();
                metric.id = (Integer) row.get(MetricRowMapper.METRIC_ID_COLUMN);
                metric.name = (String) row.get(MetricRowMapper.METRIC_NAME_COLUMN);
                metric.refID = (String) row.get(MetricRowMapper.METRIC_REF_ID_COLUMN);
                metric.refIDType = (String) row.get(MetricRowMapper.METRIC_REF_ID_TYPE_COLUMN);
                metric.description = (String) row.get(MetricRowMapper.METRIC_DESCRIPTION_COLUMN);
                metric.dashboardName = (String) row.get(MetricRowMapper.METRIC_DASHBOARD_NAME_COLUMN);
                metric.category = (String) row.get(MetricRowMapper.METRIC_CATEGORY_COLUMN);
                metric.group = (String) row.get(MetricRowMapper.METRIC_GROUP_COLUMN);
                metric.source = "metric";
                metric.urn = "";
                if (StringUtils.isNotBlank(metric.dashboardName)) {
                    metric.urn += metric.dashboardName + "/";
                }
                if (StringUtils.isNotBlank(metric.group)) {
                    metric.urn += metric.group + "/";
                }
                if (StringUtils.isNotBlank(metric.name)) {
                    metric.urn += metric.name;
                }

                ObjectNode schema = Json.newObject();
                schema.put(MetricRowMapper.METRIC_REF_ID_COLUMN, metric.refID);
                schema.put(MetricRowMapper.METRIC_REF_ID_TYPE_COLUMN, metric.refIDType);
                schema.put(MetricRowMapper.METRIC_DESCRIPTION_COLUMN, metric.description);
                schema.put(MetricRowMapper.METRIC_DASHBOARD_NAME_COLUMN, metric.dashboardName);
                schema.put(MetricRowMapper.METRIC_CATEGORY_COLUMN, metric.category);
                schema.put(MetricRowMapper.METRIC_GROUP_COLUMN, metric.group);
                metric.schema = schema.toString();
                pagedMetrics.add(metric);
            }
            long count = 0;
            try {
                count = jdbcTemplate.queryForObject("SELECT FOUND_ROWS()", Long.class);
            } catch (EmptyResultDataAccessException e) {
                Logger.error("Exception = " + e.getMessage());
            }

            ObjectNode resultNode = Json.newObject();
            resultNode.put("count", count);
            resultNode.put("page", page);
            resultNode.put("category", category);
            resultNode.put("itemsPerPage", size);
            resultNode.put("totalPages", (int) Math.ceil(count / ((double) size)));
            resultNode.set("data", Json.toJson(pagedMetrics));

            return resultNode;
        }
    });

    return result;
}

From source file:eu.databata.engine.util.PropagatorTableExport.java

private static void exportData(JdbcTemplate jdbcTemplate, String tableName, String delimiter) {
    String exportCommand = "* *DSV_COL_DELIM = " + delimiter + "\n\\x " + tableName + "\n";
    System.out.println("Exporting table using command:\n" + exportCommand);
    System.out.println("Result file with name '" + tableName
            + "'.dsv will be located inside current process execution folder.\n");
    Connection connection = null;
    try {/*  www. j a v a 2  s. com*/
        SqlFile file = new SqlFile(exportCommand, null, tableName, new SqlExecutionCallback() {
            @Override
            public void handleExecuteSuccess(String sql, int i, double d) {
                System.out.println("Sql execution success -> " + sql);
            }

            @Override
            public void handleException(SQLException sqlexception, String sql) throws SQLException {
                System.out.println("Sql execution error -> " + sql);
            }
        }, null);

        connection = jdbcTemplate.getDataSource().getConnection();
        file.setConnection(connection);
        file.execute();
    } catch (IOException e) {
        System.out.println("Export finished with error\n" + e);
    } catch (SqlToolError e) {
        System.out.println("Export finished with error\n" + e);
    } catch (SQLException e) {
        System.out.println("Export finished with error\n" + e);
    } finally {
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.emc.ecs.sync.service.MySQLDbService.java

protected void close(JdbcTemplate template) {
    try {/*w w  w.ja v a 2 s .c  o  m*/
        ((BasicDataSource) template.getDataSource()).close();
    } catch (SQLException e) {
        log.warn("could not close data source", e);
    }
}

From source file:org.string_db.psicquic.index.StringDbScoresDataReader.java

StringDbScoresDataReader(DbFacade dbFacade, JdbcTemplate jdbcTemplate, Integer speciesId) {
    log.info("init()");
    try {//www. ja v  a2  s. c  o m
        this.scoreTypes = dbFacade.loadScoreTypes();
        jdbcTemplate.setFetchSize(FETCH_SIZE);
        preparedStatement = jdbcTemplate.getDataSource().getConnection()
                .prepareStatement(scoresQuery + speciesId);

        //            this.rs = jdbcTemplate.queryForRowSet(scoresQuery + speciesId);
        this.rs = preparedStatement.executeQuery();
        nextCalled = false;
    } catch (Exception e) {
        throw new ExceptionInInitializerError(e);
    }
    log.info("done()");
}