?
Solved

IF...THEN not working

Posted on 2003-02-26
16
Medium Priority
?
178 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
  • 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
Industry Leaders: 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

I am posting this in case anyone runs into similar issues that I did, this may save you a lot of grief: Condition: 1. Your NetBIOS domain name contains an ampersand " & " character.  (e.g. AT&T) 2. You've tried to run any Microsoft installation…
The following information will get you familiar with your new DV server, including the (mt) Account Center, the Plesk Control Panel, our world-renowned support department and the rest of the (mt) tools that come with your new service.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month9 days, 5 hours left to enroll

621 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