2
\$\begingroup\$

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() : "";
    }

}
\$\endgroup\$
  • \$\begingroup\$ Why don't you use jdbcTemplate? It already have all these features implemented. \$\endgroup\$ – Dmitriy Dec 1 '17 at 9:59
  • \$\begingroup\$ I know, I just started to modify and refactor some old code and ended with this. However it's a good way to get practicing and I like it :D \$\endgroup\$ – Accollativo Dec 2 '17 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.