Solved

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

Posted on 2004-04-14
4
211 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
  • 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 125 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

730 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