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.prepareSta tement(sql Today);
}
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?
if(timeFrame.equals("today
out.println("today works");
PreparedStatement StatementSummaryView = ConnSummaryView.prepareSta
}
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?
ASKER
Yes, its outside the IF...THEN:
if(timeFrame.equals("today ")){
out.println("today");
PreparedStatement StatementSummaryView = ConnSummaryView.prepareSta tement(sql Today);
}
ResultSet SummaryView = StatementSummaryView.execu teQuery();
if(timeFrame.equals("today
out.println("today");
PreparedStatement StatementSummaryView = ConnSummaryView.prepareSta
}
ResultSet SummaryView = StatementSummaryView.execu
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.prepareSta tement(sql Today);
out.println( "prepared statement: " + StatementSummaryView + "<p>" );
}
// other code
out.println( "using statement: " + StatementSummaryView + "<p>" );
ResultSet SummaryView = StatementSummaryView.execu teQuery();
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.prepareSta
out.println( "prepared statement: " + StatementSummaryView + "<p>" );
}
// other code
out.println( "using statement: " + StatementSummaryView + "<p>" );
ResultSet SummaryView = StatementSummaryView.execu
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?
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.prepareSta tement(sql Today);
}
if (StatementSummaryView != null) {
resulsetSummaryView = StatementSummaryView.execu teQuery();
}
if (resulsetSummaryView != null) {
while (resulsetSummaryView.next( )) {%>
display query results here.
<% }
}
%>
CJ
PreparedStatement StatementSummaryView=null;
try this:
<%
PreparedStatement StatementSummaryView = null;
Resultset resulsetSummaryView = null;
if(timeFrame.equals("today
out.println("today works");
StatementSummaryView = ConnSummaryView.prepareSta
}
if (StatementSummaryView != null) {
resulsetSummaryView = StatementSummaryView.execu
}
if (resulsetSummaryView != null) {
while (resulsetSummaryView.next(
display query results here.
<% }
}
%>
CJ
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.prepareSta tement(sql Today);
}
if (StatementSummaryView != null) {
resulsetSummaryView = StatementSummaryView.execu teQuery();
}
if (resulsetSummaryView != null) {
while (resulsetSummaryView.next( )) {
column1 = resulsetSummaryView.getStr ing("colum n1");
column2 = resulsetSummaryView.getStr ing("colum n2");
}
}
%>
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
<%
PreparedStatement StatementSummaryView = null;
Resultset resulsetSummaryView = null;
String column1 = "";
String column2 = "";
if(timeFrame.equals("today
out.println("today works");
StatementSummaryView = ConnSummaryView.prepareSta
}
if (StatementSummaryView != null) {
resulsetSummaryView = StatementSummaryView.execu
}
if (resulsetSummaryView != null) {
while (resulsetSummaryView.next(
column1 = resulsetSummaryView.getStr
column2 = resulsetSummaryView.getStr
}
}
%>
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
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.prepareSta tement(sql Today);
} else {
PreparedStatement StatementSummaryView = ConnSummaryView.prepareSta tement(sql Historic);
}
*** 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.prepareSta
} else {
PreparedStatement StatementSummaryView = ConnSummaryView.prepareSta
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.prepareSta tement(sql Today);
} else {
PreparedStatement StatementSummaryView = ConnSummaryView.prepareSta tement(sql Historic);
}
*** 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.prepareSta
} else {
PreparedStatement StatementSummaryView = ConnSummaryView.prepareSta
}
try what I posted.
CJ
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.
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.
ASKER
good god ... i think this works... need to do a little more testing....
ASKER
good god ... i think this works... need to do a little more testing....
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
CJ
Resultset rs;
if(timeFrame.equals("today
out.println("today works");
PreparedStatement StatementSummaryView = ConnSummaryView.prepareSta
rs = StatementSummaryView.execu
}
CJ