JSP: Differing Prepared statements based on if/else logic

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robtyketto
    New Member
    • Nov 2006
    • 108

    JSP: Differing Prepared statements based on if/else logic

    Greetings,

    Im struggling with the very simple task of having TWO different prepated statements based on a string value (code below)


    Code:
    if(userSelectedCategory != null) {
    
    		PreparedStatement myFAQStatement = null, myFAQStatement1 = null, myFAQStatement2 = null;
    
    		if (userSelectedCategory == "ALL categories") 
    		{ 
    			PreparedStatement myFAQStatement1 = conn.prepareStatement("SELECT * FROM FAQ");
    		}
    		else 
    		{
    			PreparedStatement myFAQStatement2 = conn.prepareStatement("SELECT Id, Category, Question, Answer, Sequence, ViewCount, Rating, UserId, Created, LastUpdated, LastUpdateBy FROM FAQ WHERE Category ='" + userSelectedCategory + "'");
    		} 
    				
    		if(myFAQStatement1 == null) myFAQStatement= myFAQStatement2;
    		else myFAQStatement = myFAQStatement1;
    It errors with
    Duplicate local variable myFAQStatement1
    Duplicate local variable myFAQStatement2
    Any help would be appreciated, I get confused in java/jsp as with some other languages you can set variables value all over the place without complaints.

    Cheers
    Rob
  • robtyketto
    New Member
    • Nov 2006
    • 108

    #2
    Bump, any advice (I've use this method before for connections choices) ?

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Leave out the 'PreparedStatem ent' at the start of line 7 and 11. That way you
      don't redefine those two variables. Read a tutorial on Java because this would
      happen with any type, not just with PreparedStateme nts.

      kind regards,

      Jos

      Comment

      Working...