Link to home
Start Free TrialLog in
Avatar of AlexNYC
AlexNYC

asked on

IF...THEN not working

I have a simple IF...THEN statement that is not working the way it should:

if(timeFrame.equals("today")){
out.println("today works");
PreparedStatement StatementSummaryView = ConnSummaryView.prepareStatement(sqlToday);
}

When the timeFrame variable is set to 'today' the out.println() statement works and 'today works' is printed to the browser.  The problem is that the PreparedStatement is never executed and it screws up my SQL.  What's going on here?
Avatar of cheekycj
cheekycj
Flag of United States of America image

are you executing the preparedStatement?

Resultset rs;

if(timeFrame.equals("today")){
  out.println("today works");
  PreparedStatement StatementSummaryView = ConnSummaryView.prepareStatement(sqlToday);
  rs = StatementSummaryView.executeQuery();
}

CJ
Avatar of AlexNYC
AlexNYC

ASKER

Yes, its outside the IF...THEN:

if(timeFrame.equals("today")){
out.println("today");
PreparedStatement StatementSummaryView = ConnSummaryView.prepareStatement(sqlToday);
}

ResultSet SummaryView = StatementSummaryView.executeQuery();
1. you cannot declare StatementSummaryView inside the if block and used outside if block.
2. try to print out the sqlToday to make sure what you are executing.
3. make sure the StatementSummaryView statement is not change in between.

PreparedStatement StatementSummaryView=null;

// other code

if(timeFrame.equals("today")){
out.println("today sql: " + sqlToday + "<p>");
StatementSummaryView = ConnSummaryView.prepareStatement(sqlToday);
out.println( "prepared statement: " + StatementSummaryView + "<p>" );
}

// other code

out.println( "using statement: " + StatementSummaryView + "<p>" );
ResultSet SummaryView = StatementSummaryView.executeQuery();
Avatar of AlexNYC

ASKER

When to to execute:

out.println( "using statement: " + StatementSummaryView + "<p>" );

I get:

*** Error: No entity named "StatementSummaryView" was found in this environment.

My goal is to execute one SQL statement if the 'today' variable is set, and a different SQL statement if the today variable is 'null'.  Are there any other options?
Did u add this:
PreparedStatement StatementSummaryView=null;

try this:
<%
PreparedStatement StatementSummaryView = null;
Resultset resulsetSummaryView = null;

if(timeFrame.equals("today")){
 out.println("today works");
 StatementSummaryView = ConnSummaryView.prepareStatement(sqlToday);
}
if (StatementSummaryView != null) {
 resulsetSummaryView = StatementSummaryView.executeQuery();
}

if (resulsetSummaryView != null) {
  while (resulsetSummaryView.next()) {%>
    display query results here.
<% }  
}
%>

CJ
Avatar of AlexNYC

ASKER

For some reason the Strings that I set inside the brackets don't contain anything outside the brackets.  Why is this? Is there a way to get around it?
you have to declare the stings outside of the bracket.

<%
PreparedStatement StatementSummaryView = null;
Resultset resulsetSummaryView = null;
String column1 = "";
String column2 = "";

if(timeFrame.equals("today")){
out.println("today works");
StatementSummaryView = ConnSummaryView.prepareStatement(sqlToday);
}
if (StatementSummaryView != null) {
resulsetSummaryView = StatementSummaryView.executeQuery();
}

if (resulsetSummaryView != null) {
 while (resulsetSummaryView.next()) {
   column1 = resulsetSummaryView.getString("column1");
   column2 = resulsetSummaryView.getString("column2");
 }  
}
%>
column1:<%=column1%><br>
column2:<%=column2%><br>

if the above is empty, then you may not be getting any results back.

This will only work if you are always expecting one row.

For multiple rows its better to use the strings as you iterate over the resultset or create a list.

CJ
Avatar of AlexNYC

ASKER

I am getting

*** Error: Duplicate declaration of local variable "StatementSummaryView".


PreparedStatement StatementSummaryView = null;
ResultSet resulsetSummaryView = null;

if(timeFrame.equals("today")){
out.println("Today SQL: " + sqlToday + "<P>");
PreparedStatement StatementSummaryView = ConnSummaryView.prepareStatement(sqlToday);
} else {
PreparedStatement StatementSummaryView = ConnSummaryView.prepareStatement(sqlHistoric);
}
ASKER CERTIFIED SOLUTION
Avatar of cheekycj
cheekycj
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AlexNYC

ASKER

I am getting

*** Error: Duplicate declaration of local variable "StatementSummaryView".


PreparedStatement StatementSummaryView = null;
ResultSet resulsetSummaryView = null;

if(timeFrame.equals("today")){
out.println("Today SQL: " + sqlToday + "<P>");
PreparedStatement StatementSummaryView = ConnSummaryView.prepareStatement(sqlToday);
} else {
PreparedStatement StatementSummaryView = ConnSummaryView.prepareStatement(sqlHistoric);
}
try what I posted.

CJ
>> *** Error: Duplicate declaration of local variable "StatementSummaryView".
that means you have already declared this var in somewhere else. did you do declared it in somewhere else or are you using <@include> to include this jsp page?

could you please post the entire jsp page instead of changed code snip? we can help your better if we know more detail.
Avatar of AlexNYC

ASKER

good god ... i think this works... need to do a little more testing....
Avatar of AlexNYC

ASKER

good god ... i think this works... need to do a little more testing....
Avatar of AlexNYC

ASKER

Thanks! You are the man, sorry it took so long to close this case.
Glad I could help and Thanx for the "A"

CJ