• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Single value SQL Select statement into a variable

 
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.
 
Bartender
Posts: 7306
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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.

 
Marshal
Posts: 67451
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.

 
author & internet detective
Posts: 40200
816
Eclipse IDE VI Editor Java
  • 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.
 
Marshal
Posts: 25830
69
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.
 
Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic