John
John

Reputation: 1189

ArrayList or Hashset

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?

Upvotes: 0

Views: 749

Answers (3)

Sanjeev Kulkarni N
Sanjeev Kulkarni N

Reputation: 381

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.

Upvotes: 1

卢声远 Shengyuan Lu
卢声远 Shengyuan Lu

Reputation: 32004

more efficient:

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

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500525

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>();

Upvotes: 1

Related Questions