Why not use Spring's DAO abstraction (a very useful and reasonably lightweight library around raw JDBC which eliminates the need for boilerplate code) you can subclass the StoredProcedure
class.
class MySproc extends StoredProcedure {
public MySproc(DataSource ds) {
super(" { exec MY_SPROC ?, ? }", ds);
declare(new SqlParameter("p1", Types.VARCHAR));
declare(new SqlParameter("p2", Types.INT));
}
public void execute(String p1, int p2) {
Map m = new HashMap();
m.put("p1", p1);
m.put("p2", p2);
super.execute(m);
}
}
Then this is executed very simply as follows:
new MySproc(ds).execute("Hello", 12);
With no database Connection
s, CallableStatement
s anywhere to be seen. Lovely! Oh yes, and it also provides annotation-based Transaction
s.
If your sproc returns a table, this is incredibly easy using Spring. Simply declare:
declare(new SqlReturnResultSet("rs", mapper));
Where mapper
is an instance that converts a line of a ResultSet
into the desired object. Then modify your line:
Map out = super.execute(m);
return (Collection) out.get("rs");
The returned Collection
will contain instances of objects created by your mapper
implementation.