Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Problem in Fetching Values

  Asked By: Grant    Date: Oct 10    Category: Java    Views: 582
  

->Below is the method in a Java Program with few parameters.


->In the method parameters there may be values or may not be we dont
know.


->The parameters name are same as the attributes name in one table of
database.


->I want to perform a query with only not null values only.


->Supp in the below list of parameters there are values only in
rxm_number, facility and upload_date then the query should be like
this :


select * from tablename where rxm_number='' and facility='' and
upload_date='';


it should not have attributes mentioned in the query which has null
values.


->Problem is i am not able to get the attribute name which has values
init.


Help me to do this.......


----Method-------------


public SearchQuery[] getSearchResult(String rxm_number, String
facility, String upload_date, String comments, String document_desc,
String archive, String document,String document_seq) {


}

Share: 

 

10 Answers Found

 
Answer #1    Answered By: Bian Nguyen     Answered On: Oct 10

Why not try passing your parameters  in an XML document? Maybe have a simple XML
schema for your parameter passing, perhaps something like this:

<query>
<rxm_number>...</rxm_number>
<facility>...</facility>
...
</query>

You get the idea. It involves a more complicated parameter passing structure.
But it can be useful. You could simply pass the XML string  and then decode the
string into a document  to get the parameters for the query. You could also add
things like a type attribute  to a node to indicate that it's a date or a number,
among other things.

 
Answer #2    Answered By: Daniel Jones     Answered On: Oct 10

sorry to jump in here BUT DO NOT USE + concatenate strings!!!!
Use the StringBuffer!!
StringBuffer buffer = new StringBuffer();
buffer.append("xxx");
buffer.append("yyy");
buffer.toString();

I don't know why still so many developers just do not get it. One of the
very basics.

Sorry, when I see things like that I get a bit upset sometimes.

 
Answer #3    Answered By: Mercedes Andrews     Answered On: Oct 10

Probably because when you are learning Java it is seen as something that
isn't worth getting angry about as there are other more important things
in a developing Java programmers life (like learning how to refference
Objects :P)

Then when they start getting Intermediate level coders nobody is
checking small things like that, they are after more complex ideas, and
eventually nobody else looks at their code.

I would agree that everybody try and use StringBuffer as much as
possible, but I know when I am just Hacking together something for
myself I often find the append nature of StringBuffer can be harder to
read then than concatenating strings together :)

 
Answer #4    Answered By: Fuzairah Neeman     Answered On: Oct 10

Sorry? Why? To quote Sun ...

"In the java  programming language, you can use + to concatenate Strings
together"

and goes on to say "... behind the scenes the compiler uses StringBuffers to
implement concatenation ..."

Source
java.sun.com/.../stringsAndJavac.html

Why would you use long-hand coding like your example when the compiler is
quite capable of handling the much friendlier concatenation operator?

Quoting further from Sun, their example ...

String cat = "cat";
System.out.println("con" + cat + "enation");

apparently compiles to

String cat = "cat";
System.out.println(new StringBuffer().append("con").
append(cat).append("enation").toString());

which would probably be more efficient than the multiple statement way you
have described.

In an overwhelming proportion of cases, the actual efficiency of code is
immaterial anyway, and the readability/maintainability of the code is much
more important.

 
Answer #5    Answered By: Brandi Ramirez     Answered On: Oct 10

You are right that the java  compiler will do this job for you.
But this no reason for me why you shouldn't do it yourself.
Anyway.....
The compiler will instantiate a new StringBuffer everytime you use the "+".
You can see it when you run applications that are written in this manner
using "+" and watch the memory consumption.
This is explained in very details in "Java essentials" from sun. A book that
I can
recommend for every java developer.

 
Answer #6    Answered By: Archie Parker     Answered On: Oct 10

Well, sorry, but it should be. That's why we have compilers. To allow
programmers to write readable, maintainable, code at the expense of a bit of
run-time (in)efficiency.

It really doesn't matter whether the compiler creates new temporary
StringBuffer objects to hold the individual parts of the equation - it will
discard them when they're no longer needed. I'm surprised to hear you say
that it will, however, as that is certainly not what Sun said would happen
(in the example I quoted).

There may be the odd occasion when the efficiency of hand-coding
StringBuffer concatenations is needed. But, in the main, there is no point
and it is a very bad habit to get into. Keep the program  readable (by
others as well as yourself).

If you want to program with maximum efficiency and minimum memory, you'd be
better off in Assembler. :-)

 
Answer #7    Answered By: Hamdan Younis     Answered On: Oct 10

Actually I was willing to keep my mouth shut but you are right.
I have made the experience once and I have seen that this "+" approach
was eating up the memory (using JDK1.3). I had to rewrite a whole bunch
of sql statements in java  (the former developer had used this "+" heavily)
to get rid of this problem. We had the problem  of running out of memory
when too many users were online and using the application.

But since David said that it is no problem and even suns says it is no
problem I was about to keep calm.
Let them run into the pitfalls and they will learn. It is always the best
way to learn from your own experiences rather than others.

BTW: What happened to the weekly "Which IDE is the best" discussion? I am
already missing it.....

 
Answer #8    Answered By: Laaibah Malik     Answered On: Oct 10

I'm not sure why this is such a big deal, but I agree with David that using a
StringBuffer everywhere is a waste of time. If you're so worried about using up
memory (which is gc'd anyway) and saving every little clock cycle, then I'm
afraid you've chosen the wrong programming language. StringBuffers make sense
in areas that are used heavily and perform  a lot of string  concatenation or in
loops where string concatenation takes place in the loop. I doubt the
application that the original poster was talking about fits into either of those
categories.

 
Answer #9    Answered By: Daw Boonliang     Answered On: Oct 10

Just to clarify this a bit. I didn't say it was not a problem. I quoted Sun
saying it wasn't a problem.

I also said that there might be occasions where hand-coding was needed.

If Sun is talking rubbish, then I'd like to know. So hopefully would they.

 
Answer #10    Answered By: Christie Bradley     Answered On: Oct 10

You can concatenate the Query string  based on the condition whether the
parameter is null  or not


public SearchQuery[] getSearchResult(String rxm_number, String
facility, String upload_date, String comments, String document_desc,
String archive, String document,String document_seq) {

String strQuery = "select * from tablename where ";


if (rxm_number !="")
{
strQuery = strQuery + " rxm_number = '" + rxm_number +"' and";
}

if (facility!="")
{
strQuery = strQuery + " facility = '" + facility + "' and";
}
//like that u can add dynamically based on the condition..
//Take care on 'and '

}

 
Didn't find what you were looking for? Find more on Problem in Fetching Values Or get search suggestion and latest updates.




Tagged: