Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Single value SQL Select statement into a variable

 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been programming for four decades in a whole bunch of languages. Java is throwing me for a loop. This should be so simple. All I want to do is execute a SQL STatement which I know will have one value. I want to compare that value with "AEP". Obviously there is something silly that I just can't figure out. I read about string being immutable but surely there is a way to have a string variable. The debugger complais about my Sting CustomerName="xx"

Query query = em.createQuery("select CustomerName from web_cost_file,customer " +
"where web_cost_file.customer=customer.id and web_cost_file.customer=" +
costsFileList[0]);
//string CustomerName = query.getResult();
String CustomerName="xx";
List<Object[]> listCustomerName = query.getResultList();
if (listCustomerName != null && !listCustomerName.isEmpty()) {
CustomerName = listCustomerName.iterator().next().toString();
}
if (CustomerName.equals("AEP")) ..my business logic..

I am hoping that somebody's wisdom will save me tons of time in experimenting.
 
Carey Brown
Bartender
Posts: 1953
24
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may know that there is only zero or one result but the code needs to be written to expect more.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65337
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your post you've used Sting, string, and String. Which is it really?
 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String - not sting or string.

Why does something simple like String CustomerName="xx"; get flagged as a problem in the Eclipse debugger.
 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my current code. The CustomerName in the System.out.print statement doesn't compile - it says that CustomerName can not be resolved to a variable. But why is that true? it is declared and assigned a value in the for loop.

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is in this code:


Since "CustomerName" is declared inside the for loop, it is scoped to only be accessible inside the for loop. If you moved the println inside the for loop, it would work.

Also note that in Java, variables begin with a lowercase by convention. So customerName would be a better variable name.
 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had tried to define CustomerName prior to the for loop but when I did so String CustomerName="xx"; I got an error on that. So the question is how to I define CustomerName so that I can use it outside of the for loop.
 
Paul Clapham
Sheriff
Posts: 21576
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're "getting an error" then what you should to is to read the error message. It usually provides some insight into the problem -- at least, it's meant to do that. So telling us only that you got an error is unhelpful. Post the code and the error message.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic