Solved

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

Posted on 2004-04-14
4
198 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumHeights2  challenge 7 75
commonTwo  challenge 63 98
Python Assistance 7 31
Java / Linux and Regular Expressions 11 43
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now