Last active
December 11, 2015 17:08
-
-
Save lischenko/4632533 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
driverclassname=oracle.jdbc.OracleDriver | |
url=jdbc:oracle:thin:@localhost:1521:XE | |
username=me | |
password=me |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Collections; | |
import java.util.Map; | |
import java.util.Properties; | |
import javax.sql.DataSource; | |
import org.apache.commons.dbcp.BasicDataSourceFactory; | |
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; | |
public class Main { | |
private static String SQL_CONSTANT_INTERVAL = "select current_timestamp - interval '1' hour from dual"; | |
private static String SQL_PARAMETERIZED_INTERVAL_QUOTED = "select current_timestamp - interval ':hours' hour from dual"; | |
private static String SQL_PARAMETERIZED_INTERVAL_UNQUOTED = "select current_timestamp - interval :hours hour from dual"; | |
private static String SQL_PARAMETERIZED_INTERVAL_NUMTODSINTERVAL = | |
"select current_timestamp - NUMTODSINTERVAL(:hours, 'HOUR' ) from dual"; | |
public static void main(String[] args) throws Exception { | |
Properties properties = new Properties(); | |
properties.load(Main.class.getClassLoader().getSystemResourceAsStream("db.properties")); | |
DataSource dataSource = BasicDataSourceFactory.createDataSource(properties); | |
NamedParameterJdbcTemplate npTemplate = new NamedParameterJdbcTemplate(dataSource); | |
Map<String, String> params = Collections.singletonMap("hours", "1"); | |
String[] queries = | |
new String[] { | |
SQL_CONSTANT_INTERVAL, | |
SQL_PARAMETERIZED_INTERVAL_QUOTED, | |
SQL_PARAMETERIZED_INTERVAL_UNQUOTED, | |
SQL_PARAMETERIZED_INTERVAL_NUMTODSINTERVAL }; | |
for (String q : queries) { | |
System.out.println("Executing " + q); | |
try { | |
System.out.println("Result = " + npTemplate.queryForObject(q, params, String.class)); | |
} catch (RuntimeException e) { | |
System.out.println("Error: " + e); | |
} | |
System.out.println(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> | |
<groupId>examples</groupId> | |
<artifactId>oracle-interval</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>oracle-interval</name> | |
<url>http://maven.apache.org</url> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
</properties> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>2.3.2</version> | |
<configuration> | |
<source>1.6</source> | |
<target>1.6</target> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
<dependencies> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.8.1</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>commons-dbcp</groupId> | |
<artifactId>commons-dbcp</artifactId> | |
<version>1.4</version> | |
<type>jar</type> | |
<scope>compile</scope> | |
</dependency> | |
<dependency> | |
<groupId>com.oracle</groupId> | |
<artifactId>ojdbc6</artifactId> | |
<scope>runtime</scope> | |
<version>11.2.0.1.0</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-jdbc</artifactId> | |
<version>3.1.2.RELEASE</version> | |
<type>jar</type> | |
<scope>compile</scope> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment