?
Solved

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

Posted on 2004-04-14
4
Medium Priority
?
221 Views
Last Modified: 2012-05-04
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
Comment
Question by:azsat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10828357
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
 

Author Comment

by:azsat
ID: 10832464
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 375 total points
ID: 10836867
>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10869938
8-)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question