?
Solved

IF...THEN not working

Posted on 2003-02-26
16
Medium Priority
?
175 Views
Last Modified: 2010-04-01
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?
0
Comment
Question by:AlexNYC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 2
16 Comments
 
LVL 19

Expert Comment

by:cheekycj
ID: 8026145
are you executing the preparedStatement?

Resultset rs;

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

CJ
0
 

Author Comment

by:AlexNYC
ID: 8026204
Yes, its outside the IF...THEN:

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

ResultSet SummaryView = StatementSummaryView.executeQuery();
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8026303
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();
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:AlexNYC
ID: 8026748
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?
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8026852
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
0
 

Author Comment

by:AlexNYC
ID: 8026954
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?
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8027008
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
0
 

Author Comment

by:AlexNYC
ID: 8027115
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);
}
0
 
LVL 19

Accepted Solution

by:
cheekycj earned 200 total points
ID: 8027135
change the last code you posted to:
PreparedStatement StatementSummaryView = null;
ResultSet resulsetSummaryView = null;

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

CJ
0
 

Author Comment

by:AlexNYC
ID: 8027148
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);
}
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8027167
try what I posted.

CJ
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8027171
>> *** 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.
0
 

Author Comment

by:AlexNYC
ID: 8027175
good god ... i think this works... need to do a little more testing....
0
 

Author Comment

by:AlexNYC
ID: 8027182
good god ... i think this works... need to do a little more testing....
0
 

Author Comment

by:AlexNYC
ID: 8111839
Thanks! You are the man, sorry it took so long to close this case.
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8111879
Glad I could help and Thanx for the "A"

CJ
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
This month, Experts Exchange’s free Course of the Month is focused on CompTIA IT Fundamentals.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question