Link to home
Start Free TrialLog in
Avatar of azsat
azsat

asked on

Java DB object to Class Mapper for multiple objects in Where clause

Hi,

I have a number of mappers defined in my package which have finder methods with WHERE clauses referencing a single DB table.  The DB tables and there fields are defined in the package.

My stateless session beans create action objects and call upon methods to perform various database queries via the perfom methods.

However, I would like to create a slightly more complex mapper class which has a number of my tables in the FROM/WHERE clause in it's finder method.  


How can a String for the SQL Prepared statement (which currently references the single table be enhanced to use other tables as well.

Thanks.

az.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You can use a placeholder in the String in conjunction with the MessageFormat class.

String select = "SELECT x FROM {0} WHERE ....";

Object args = new Object[1];
args[0] = "SomeTable";
MessageFormat mf = new MessageFormat(select);
String sql = mf.format(args);

otherwise a table name cannot be a parameter to a PreparedStatement
Avatar of azsat
azsat

ASKER

Thanks for that , i don't have an issue hard coding the table names , as the tables will only be referenced once for this particluar query. The exiting mapper is given below tblclosure is the table currently used:

NB jdbcMapper has the necessary insert update etc statements.

(
 IClosureMapper has the abtract definition :

public interface IClosureMapper extends IMapper {
      public IDataSet findBySwap(int swapID)
            throws MapperException;
}
)

public class ClosureMapper
            extends JdbcMapper
            implements IClosureMapper {

      private String findBySwapIDSqlString() {
            String result = " cl"
                  + "WHERE cl.swap_id = ? ";
            return result;
      }

      /**
       * Constructor for ClosureMapper
       */
      public ClosureMapper() {
            super(new ClosureFieldDef(), "efswap.dbo", "tblClosure");
      }

      public ClosureMapper(FieldDefVector fieldDefs, String schemaName, String tableName) {
            super(fieldDefs, schemaName, tableName);
      }

      /* (non-Javadoc)
       * @see com.ubs.eq.wisdom.mapper.IClosureMapper#findBySwap(java.lang.Integer, ...)
       */
      public IDataSet findBySwap(
                  int                   swapID,
                  String[]       states
                  )
                  throws MapperException {
            DataSet list = new DataSet(_FieldDefs);
            Record rec = null;
            Connection conn = null;
            try {
                  conn = getConnection();
                  PreparedStatement ps = conn.prepareStatement(
                        getLoadString() + findBySwapIDSqlString(states));
                  Log4jAppService.log(ITraceCapable.INFO_LEVEL, getClass().getName(), getLoadString() + findBySwapIDSqlString(states));
                  ps.setInt(1, swapID);
                  ps.setTimestamp(2, effectiveDate);
                  ps.setTimestamp(3, effectiveDate);
                  ResultSet rs = ps.executeQuery();
                  while (rs.next()) {
                        rec = (Record)mapIn(rs);
                        list.ReadRecord(rec);
                  }
                  return list;
            }
            catch (SQLException se) {
                  throw new MapperException(se.getMessage());
            }
            catch (DataSetException dse) {
                  throw new MapperException(dse.getExtendedMessage());
            }
            finally {
                  close(conn);
            }
      }

}

Can I simply hard code the other tables eg tblState as follows, plus I need to get back certain columns from
tblState as well - how can the ClosureMapper (or ancestors) be modfied to acommodate this???????

eg,

private String findBySwapIDSqlString(String[] states) {
            String result = " cl, " + this.GetSchemaName() + ".tblState st "
                  + "WHERE cl.swap_id = ? "
                  + "AND cl.closure_id = st.entity_id "
                  + "AND st.entity_type = '" + Constants.ET_CLOSURE + "' "
                  + "AND st.adjusted = 0 "
                  + "AND st.state in ('"
                  + states[0].toString();

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
8-)