Skip to content

Instantly share code, notes, and snippets.

@greghelton
Last active December 1, 2020 20:45
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/aa4466bb6fcf34966456394fdbcde8e8 to your computer and use it in GitHub Desktop.
Save greghelton/aa4466bb6fcf34966456394fdbcde8e8 to your computer and use it in GitHub Desktop.
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');
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());
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>key-value-getter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>KeyValueGetter</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Test*.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>
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