Skip to content

Instantly share code, notes, and snippets.

@greghelton
Last active May 5, 2021 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greghelton/0fb17b5a975cd8b0344e6305d145f034 to your computer and use it in GitHub Desktop.
Save greghelton/0fb17b5a975cd8b0344e6305d145f034 to your computer and use it in GitHub Desktop.
package com.example;
import org.slf4j.*;
import java.sql.*;
import java.util.stream.Collectors;
import org.apache.commons.collections4.*;
import org.apache.commons.collections4.multimap.*;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
@EnableAutoConfiguration
public class KeyValueGetter implements CommandLineRunner {
private static final Logger log = LoggerFactory.getLogger(KeyValueGetter.class);
ArrayListValuedHashMap keyvals = new ArrayListValuedHashMap();
@Autowired
JdbcTemplate jdbcTemplate;
String sql = new StringBuilder().append("select key1, prop1 from states_afflictions where key1 = ? UNION")
.append(" select key2, prop2 from states_afflictions where key2 = ? UNION")
.append(" select key3, prop3 from states_afflictions where key3 = ?").toString();
public static void main(String... args) {
SpringApplication.run(KeyValueGetter.class, args);
}
@Override
public void run(String... args) throws Exception {
jdbcTemplate.query(sql, new Object[] {"Texas", "Texas", "Texas"},
(rs, rownum) -> keyvals.put(rs.getString(1), rs.getString(2)));
MapIterator mit = keyvals.mapIterator();
while(mit.hasNext()) {
mit.next();
log.error("keyval = " + mit.getKey() + ", " + mit.getValue());
}
}
}
insert into states_afflictions values('Texas', 'tornadoes', 'Oklahoma', 'tornadoes', 'Arkansas', 'floods');
insert into states_afflictions values('Idaho', 'potato famine', 'Texas', 'rodeos', 'Lousiana', 'hurricanes');
insert into states_afflictions values('Arkansas', 'cotton', 'Louisiana', 'mosquitoes', 'Texas', 'hurricanes');
insert into states_afflictions values('Texas', 'heat', 'Texas', 'drought', 'Florida', 'hurricanes');
create table states_afflictions(
key1 CHAR(10)
, prop1 VARCHAR(20)
, key2 CHAR(10)
, prop2 VARCHAR(20)
, key3 CHAR(10)
, prop3 VARCHAR(20));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment