Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

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.
0
azsat
Asked:
azsat
  • 3
1 Solution
 
CEHJCommented:
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
0
 
azsatAuthor Commented:
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();

0
 
CEHJCommented:
>>Can I simply hard code the other tables eg tblState as follows

Don't see why not. I would personally use a StringBuffer though. Of course you have not closed the set bracket. Should you not also enable more than a single member in the set too?

>>plus I need to get back certain columns from
tblState as well - how can the ClosureMapper (or ancestors) be modfied to acommodate this???????

Dont' understand this i'm afraid

0
 
CEHJCommented:
8-)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now