0
ArrayList queryParms = new ArrayList(); StringBuffer sql = new StringBuffer(); sql.append( "SELECT A.FLDREC_NUM, " + "@CHARDATE(A.FLDDATE), " + "T.FLDDESCR, @DEC(A.FLDLEVEL,3) " + " FROM @SCHEMAALCOHOL A LEFT OUTER JOIN @SCHEMADRUGCAUS T " + " ON A.FLDREASON = T.FLDCODE " + " WHERE A.FLDEMPLOYEE = ? " + " ORDER BY A.FLDDATE DESC" ); queryParms.add(new Long(empRec)); 
  1. Can i use HashSet instead of ArrayList above and does it make any sense in terms of performance?.

  2. What does the query do, do we need to append the query in StringBuffer. Why can't i directly add to ArrayList?

2
  • 1
    DON'T use a HashSet. It will not keep your parameters in the same order, so your SQL can get really funny results (if you have more than one parameter). Commented Sep 27, 2010 at 10:20
  • I think you really need to cover my Internal life of ArrayList tutorial and Internal life of HashSet tutorial Commented Jul 27, 2013 at 8:47

3 Answers 3

1

In general, you would want to use an ArrayList for query parameters - because the order matters. When you've only got a single parameter (as you have here) it's not an issue - but I'd definitely use a list for consistency.

As for using StringBuffer - you haven't shown what you're doing with sql here. If you're not appending anything else, you could use a simple String... but you wouldn't be adding it to the ArrayList, as it's the query itself, not a query parameter.

Finally, I'd recommend using generics, e.g.

ArrayList<Long> queryParameters = new ArrayList<Long>(); 
Sign up to request clarification or add additional context in comments.

1 Comment

Or at least, use StringBuilder instead of StringBuffer, which is not synchronized and thus faster.
1

As for performance, consider:

  1. Using StringBuilder(instead of StringBuffer) which is not synchronized and is not an issue, since you're creating a new StringBuilder() for each request. If it's the same statement used for each request, it should be a final String, globally declared.

  2. As suggested by Jon, use ArrayList with Generics. The queryParams is used as a part of QueryService implementation used to query DB. As it appears, it's a pre-compiled sql query and order of query params is important at run-time. HashSet doesn't guarantee order.

Comments

0

more efficient:

sql.append("SELECT A.FLDREC_NUM, "); sql.append("@CHARDATE(A.FLDDATE), "); ...... 

2 Comments

Nope, that's less efficient, because it's concatenated at execution time instead of at compile time. More efficient would be just to use a String to start with.
Yes, don't use the StringBuilder unless you have if statements or loops when constructing the query.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.