Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

StringTokenizer not returning all tokens...

Status
Not open for further replies.

msloan

Programmer
May 30, 2001
39
US
Hello,

I am using a StringTokenizer to isolate the "WHERE" clause in an SQL statement. This SQL statement is stored and is being returned correctly from the database, but when I do a System.out.println(stViewDef.nextToken());, not all token are listed....

Here is what I ahve so far:
Code:
   boolean bolWhereFlag = false;
   String sql = "SELECT * FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = '" + strView + "'"
   ResultSet rResult = getQueryResult(sql);
    try
    {
      if(rResult.next())
      {
        StringTokenizer stViewDef = new StringTokenizer(rResult.getString("VIEW_DEFINITION"));
        while(stViewDef.hasMoreTokens())
        {
          if(stViewDef.nextToken().equals("WHERE") || bolWhereFlag)
          {
            bolWhereFlag = true;
            try
            {
              System.out.println(stViewDef.nextToken());
            }
            catch(NoSuchElementException nsee)
            {
              System.out.println(nsee.getMessage());
            }
          }
        }
      }
      closeResultSet(rResult);
    }
    catch(SQLException e)
    {
      System.out.println(e.getMessage());
    }
  }
and here is the SQL statement that is being returned from the database:
CREATE VIEW dbo.vwExplorerNodeProcNewUnsignDetail

AS

SELECT tblProcurement.fldProcurementKey,

tblProcurement.fldProcurementCode,

tblProcurement.fldProcurementAgentID,

tblProcurement.fldProcurementShortDesc,

tblSectionLocators.fldSectionLocatorsUnit,

tblVendor.fldVendorName,

CASE WHEN tblProcurement.fldProcurementUrgent = 1 THEN 'Yes'

ELSE 'No' END AS fldProcurementUrgent,

tblProcurement.fldPostKey,

tblProcurementType.fldProcurementTypeCode,

CASE WHEN tblActions.fldActionsKey = 3 THEN 'R' ELSE ' ' END as fldReturnIndicator

FROM tblProcurement INNER JOIN

tblSectionLocators ON

tblProcurement.fldRqstSectionLocatorsKey = tblSectionLocators.fldSectionLocatorsKey

INNER JOIN

tblActions ON

tblProcurement.fldActionsKey = tblActions.fldActionsKey LEFT

JOIN

tblVendor ON

tblProcurement.fldVendorKey = tblVendor.fldVendorKey LEFT

JOIN

tblProcurementType ON

tblProcurement.fldProcurementTypeKey = tblProcurementType.fldProcurementTypeKey

WHERE tblActions.fldActionsKey IN (1, 3) AND ((tblProcurementType.fldProcurementTypeCode = 'Z' AND

fldProcurementCOAuthKey > 0) OR (tblProcurementType.fldProcurementTypeCode <> 'Z'))


and this is what prints out when I call the System.out.println(stViewDef.nextToken()):
tblActions.fldActionsKey

(1,

AND

=

AND

fldProcurementCOAuthKey

0)

(tblProcurementType.fldProcurementTypeCode

'Z'))


Any ideas/suggestions are greatly appreciated - thanks in advance
 
As far as I can remember using nextToken() automatically advances your pointer along the tokeniser (regardless of whether or not the condition is satisfied), so each time your program comes across the main &quot;if&quot; condition it is already advancing the pointer along one token and then you print out the next token - this would imply that you are at least missing out every other token.



 
Well I figured out a way around this problem using the indexOf() -
Code:
String strWhere = &quot;&quot;;
   String sql = &quot;SELECT * FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = '&quot; + strView + &quot;'&quot;;
   ResultSet rResult = getQueryResult(sql);
    try
    {
      if(rResult.next())
      {
        int index = rResult.getString(&quot;VIEW_DEFINITION&quot;).indexOf(&quot;WHERE&quot;);
        strWhere = rResult.getString(&quot;VIEW_DEFINITION&quot;).substring(index);

      }
      closeResultSet(rResult);
    }
    catch(SQLException e)
    {
      System.out.println(e.getMessage());
    }

This gets the job done, but I would still like to see how to do it using StringTokenizer.

Thanks again

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top