I started to modify some old code and ended up creating a sort of spring jdbc framework.
I know just the most popular design pattern, so I was wondering if my design was good.
For example, to call store procedures, I created an interface PlsqlParameter
, to hold in and out parameter and cycle them.
But for example, InPlsqlParameter
is forced to implements getItemFromStatement
, with no return (because it's not an output).
How can I improve my design? My project on github
the main method:
public void genericProcedure(Connection conn, String functionName, PlsqlParameter ... queryParams) throws Exception
{
CallableStatement cstm = null;
String query = getQueryProcedure(functionName, queryParams).toString();
boolean paramEmpty = queryParams.length==1 && queryParams[0]==null;
try
{
cstm = conn.prepareCall(query);
int i=1;
if (!paramEmpty)
{
for (PlsqlParameter ob:queryParams)
{
ob.setParameter(i++, cstm);
}
}
cstm.execute();
if (!paramEmpty)
{
i=1;
for (PlsqlParameter ob:queryParams)
{
ob.getItemFromStatement(i++, cstm);
}
}
}
catch (Exception e)
{
traceErrorLog(e, query, (Object[])queryParams);
throw e;
} finally
{
if (cstm!=null)
{
cstm.close();
}
}
}
getItemFromStatement return null:
public class InPlsqlParameter <T> implements PlsqlParameter {
ItemContainer<T> item;
public InPlsqlParameter() {
super();
}
public InPlsqlParameter(T item) {
super();
this.item = new SingleItem<T>(item);
}
public InPlsqlParameter(ItemContainer<T> item) {
super();
this.item = item;
}
@Override
public void setParameter(int parameterIndex, CallableStatement cstm) throws SQLException {
cstm.setObject(parameterIndex, item!=null ? item.getObject() : null);
}
@Override
public ItemContainer<?> getItemFromStatement(int parameterIndex,
CallableStatement cstm) throws Exception {
return null;
}
@Override
public String toString() {
return item!=null ? item.toString() : "";
}
}
This is ok:
public class OutPlsqlParameter implements PlsqlParameter {
ItemContainer<?> item;
public OutPlsqlParameter(ItemContainer<?> item) {
super();
this.item = item;
}
@Override
public void setParameter(int parameterIndex, CallableStatement cstm) throws SQLException {
item.setOutParameter(parameterIndex, cstm);
}
@Override
public ItemContainer<?> getItemFromStatement(int parameterIndex, CallableStatement cstm) throws Exception
{
return item.getObjectFromStatement(parameterIndex, cstm);
}
@Override
public String toString() {
return item!=null ? item.toString() : "";
}
}