Skip to content

Commit cb92f7e

Browse files
committed
initial commit
1 parent 04e1d24 commit cb92f7e

File tree

7 files changed

+254
-0
lines changed

7 files changed

+254
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package ca.credativ.jdbc;
2+
3+
4+
/**
5+
* Created by davec on 2014-04-16.
6+
*/
7+
public interface PostgresArray
8+
{
9+
String[] get() throws Exception;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package ca.credativ.jdbc;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.jdbc.core.SqlOutParameter;
5+
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
6+
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
7+
import org.springframework.jdbc.core.support.JdbcDaoSupport;
8+
9+
import org.postgresql.jdbc4.Jdbc4Array;
10+
import org.springframework.stereotype.Repository;
11+
import org.springframework.transaction.annotation.Transactional;
12+
13+
import javax.sql.DataSource;
14+
import java.sql.SQLException;
15+
import java.util.Map;
16+
import java.util.logging.Level;
17+
import java.util.logging.Logger;
18+
19+
/**
20+
* Created by davec on 2014-04-16.
21+
*/
22+
@Transactional
23+
@Repository
24+
public class PostgresArrayImpl extends JdbcDaoSupport implements PostgresArray
25+
{
26+
@Autowired
27+
public PostgresArrayImpl(DataSource dataSource) {
28+
setDataSource(dataSource);
29+
}
30+
31+
@Override
32+
public String [] get() throws Exception
33+
{
34+
SimpleJdbcCall call = new SimpleJdbcCall(this.getJdbcTemplate());
35+
call.withFunctionName("usecase01_get");
36+
call.withSchemaName("public");
37+
call.withoutProcedureColumnMetaDataAccess();
38+
39+
call.declareParameters(new SqlOutParameter("testcolumn", java.sql.Types.ARRAY));
40+
MapSqlParameterSource map = new MapSqlParameterSource();
41+
42+
Map<String, Object> result = call.execute(map);
43+
Jdbc4Array jdbc4Array = (Jdbc4Array)result.get("testcolumn");
44+
try {
45+
Object o = jdbc4Array.getArray();
46+
Logger.getLogger(PostgresArrayImpl.class.getName()).log(Level.SEVERE, null, o);
47+
return (String [])o;
48+
49+
} catch (SQLException ex) {
50+
Logger.getLogger(PostgresArrayImpl.class.getName()).log(Level.SEVERE, null, ex);
51+
throw ex;
52+
}
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package ca.credativ.jdbc;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.ApplicationContext;
5+
import org.springframework.context.support.ClassPathXmlApplicationContext;
6+
7+
import java.sql.Array;
8+
9+
/**
10+
* Created by davec on 2014-04-16.
11+
*/
12+
public class SpringTransactionExample
13+
{
14+
15+
public static void main(String []args)
16+
{
17+
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("context.xml");
18+
19+
20+
try
21+
{
22+
PostgresArray postgresArray = (PostgresArray)applicationContext.getBean("postgresArrayImpl");
23+
24+
String []strings = postgresArray.get() ;
25+
for (int i=0;i < strings.length;i++)
26+
{
27+
System.out.println(strings[i]);
28+
}
29+
}
30+
catch( Exception ex )
31+
{
32+
ex.printStackTrace();
33+
}
34+
}
35+
}

main/java/resources/context.xml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:context="http://www.springframework.org/schema/context"
5+
xmlns:tx="http://www.springframework.org/schema/tx"
6+
xsi:schemaLocation="http://www.springframework.org/schema/beans
7+
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
8+
http://www.springframework.org/schema/context
9+
http://www.springframework.org/schema/context/spring-context-3.2.xsd
10+
http://www.springframework.org/schema/tx
11+
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
12+
13+
14+
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
15+
<property name="driverClassName" value="org.postgresql.Driver"/>
16+
<property name="url" value="jdbc:postgresql://localhost:5432/usecase"/>
17+
<property name="username" value="postgres"/>
18+
<property name="password" value="########"/>
19+
</bean>
20+
21+
<context:component-scan base-package="ca.credativ.jdbc" />
22+
<tx:annotation-driven transaction-manager="txManager"/>
23+
24+
25+
<!-- a PlatformTransactionManager is still required -->
26+
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
27+
<!-- (this dependency is defined somewhere else) -->
28+
<property name="dataSource" ref="dataSource"/>
29+
</bean>
30+
</beans>

pom.xml

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>ca.credativ.spring.jdbc</groupId>
8+
<artifactId>ca-credativ-spring-jdbc</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<name>ca-credativ-spring-jdbc</name>
13+
<url>http://maven.apache.org</url>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<!-- Define Spring version as a constant -->
18+
<spring.version>3.2.7.RELEASE</spring.version>
19+
</properties>
20+
21+
<dependencies>
22+
23+
<dependency>
24+
<groupId>org.springframework</groupId>
25+
<artifactId>spring-core</artifactId>
26+
<version>${spring.version}</version>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>org.springframework</groupId>
31+
<artifactId>spring-context</artifactId>
32+
<version>${spring.version}</version>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.springframework</groupId>
37+
<artifactId>spring-tx</artifactId>
38+
<version>${spring.version}</version>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.springframework</groupId>
43+
<artifactId>spring-jdbc</artifactId>
44+
<version>${spring.version}</version>
45+
</dependency>
46+
47+
<dependency>
48+
<groupId>org.springframework</groupId>
49+
<artifactId>spring-test</artifactId>
50+
<version>${spring.version}</version>
51+
</dependency>
52+
53+
<dependency>
54+
<groupId>commons-dbcp</groupId>
55+
<artifactId>commons-dbcp</artifactId>
56+
<version>1.2.2</version>
57+
</dependency>
58+
59+
<dependency>
60+
<groupId>org.postgresql</groupId>
61+
<artifactId>postgresql</artifactId>
62+
<version>9.3-1100-jdbc41</version>
63+
</dependency>
64+
<dependency>
65+
<groupId>junit</groupId>
66+
<artifactId>junit-dep</artifactId>
67+
<version>4.10</version>
68+
<scope>test</scope>
69+
</dependency>
70+
71+
</dependencies>
72+
<build>
73+
<plugins>
74+
<plugin>
75+
<groupId>org.apache.maven.plugins</groupId>
76+
<artifactId>maven-compiler-plugin</artifactId>
77+
<configuration>
78+
<source>1.7</source>
79+
<target>1.7</target>
80+
</configuration>
81+
</plugin>
82+
</plugins>
83+
</build>
84+
</project>

site/site.xml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="ISO-8859-1"?>
2+
<project name="ca-credativ-spring-jdbcdemo">
3+
<bannerLeft>
4+
<name>ca-credativ-spring-jdbcdemo</name>
5+
<src>https://appfuse.dev.java.net/images/appfuse-title.gif</src>
6+
<href>http://appfuse.org</href>
7+
</bannerLeft>
8+
<publishDate format="dd MMM yyyy"/>
9+
<skin>
10+
<groupId>org.apache.tapestry</groupId>
11+
<artifactId>maven-skin</artifactId>
12+
<version>1.1</version>
13+
</skin>
14+
<body>
15+
${reports}
16+
</body>
17+
</project>
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package ca.credativ.jdbc;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.test.context.ContextConfiguration;
7+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
8+
import org.springframework.transaction.annotation.Transactional;
9+
10+
@RunWith(SpringJUnit4ClassRunner.class)
11+
@ContextConfiguration
12+
public class PostgresArrayTest{
13+
14+
@Autowired
15+
private PostgresArrayImpl postgresArray;
16+
17+
@Test
18+
@Transactional
19+
public void testGet() throws Exception {
20+
postgresArray.get();
21+
22+
}
23+
}

0 commit comments

Comments
 (0)