"like" clause not operating properly
843854Feb 20 2002 — edited Apr 30 2004I am using a very simple prepared statement query with a "like" clause and it is not returning the correct results. I am using JDK 1.3.1_01 and SQLServer (2000 client to a 7.0 DB). The query works fine in Query Analyzer, but returns nothing when I implement in Java. The Java looks like:
---------------------------------------------------------------------
String SQL_GET_METRIC_IDS =
"SELECT m.metric_id " +
"FROM metric m " +
"WHERE m.name LIKE ? " ;
ArrayList metricIdList = new ArrayList();
Connection conn = null;
try
{
conn = DBAccess.getConnection();
PreparedStatement pstmt = StatementFactory.getStatement(conn,
SQL_GET_METRIC_IDS, DebugLevel.ON);
try
{
pstmt.setString(1, metricName);
System.out.println("MUSCMetricBean.getMetricIds(): pstmt=" + pstmt.toString());
ResultSet results = pstmt.executeQuery();
...and so on
------------------------------------------------------------------------
I even print out the Prepared Statement and it displays:
pstmt=SELECT m.metric_id FROM metric m WHERE m.name LIKE 'total fakes'
I realize that I have no wildcard, but there is a record that is exactly 'total fakes'. Also, the reason I am here is that my query using the wildcard wasn't working properly, so I started eliminating variables. This is my desired query:
pstmt=SELECT m.metric_id FROM metric m WHERE m.name LIKE '%total fakes'
Again, neither one of these produces records in Java, but both work fine in Query Analyzer.
Tony