Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / February 2006

Tip: Looking for answers? Try searching our database.

variable in sql query

Thread view: 
Anna Kubiak - 23 Feb 2006 03:35 GMT
Hi, excuse me, can anybody help?

how to write variable from another void to string containing sql query?

for example

String qry ="select * from table where name_of_field like variable";

thanks in advance,
anna kubiak
John O'Conner - 23 Feb 2006 04:29 GMT
> Hi, excuse me, can anybody help?
>
[quoted text clipped - 6 lines]
> thanks in advance,
> anna kubiak

String qry = "select * from table where name_of_field like ?";
PreparedStatement stmt = dbConnection.prepareStatement(qry);
stmt.setString(1, variable);
ResultSet result = stmt.executeQuery();

or something like that...

Regards,
John O'Conner
Anna Kubiak - 23 Feb 2006 22:19 GMT
>> Hi, excuse me, can anybody help?
>>
[quoted text clipped - 13 lines]
>
> or something like that...

thanks i tried , maybe it works or not, because when i use command String
name_variable =rs.getString(1) where rs is declared as record set. i wrote
prepareStatement(qry) where qry is "Select * from table where name_of_field
like ?" When I am testing rs is not empty, but still it seems getString
doesn't work (the types are not mixed for sure).

what's maybe the reason?
Thanks, Anna
John O'Conner - 23 Feb 2006 22:45 GMT
> thanks i tried , maybe it works or not, because when i use command String
> name_variable =rs.getString(1) where rs is declared as record set. i wrote
[quoted text clipped - 4 lines]
> what's maybe the reason?
> Thanks, Anna

The ResultSet "pointer" starts before the first returned record.
Typically one uses the following to iterate through the result set:

while(rs.next()) {
    String name_variable = rs.getString(1);
    // do something else
        // ...
}

So, just try rs.next() to get to the first record.

--
John O'Conner
IchBin - 24 Feb 2006 02:40 GMT
>> thanks i tried , maybe it works or not, because when i use command
>> String name_variable =rs.getString(1) where rs is declared as record
[quoted text clipped - 15 lines]
>
> So, just try rs.next() to get to the first record.

Here is a link for "Using Prepared Statements" in Sun' tutorial :
http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html

Also, here is a link to "The Java Developers Almanac 1.4" or code
samples for SQL Class:
http://javaalmanac.com/egs/java.sql/pkg.html

There is an example for "Inserting a Row into a Database Table Using a
Prepared Statement" did not look for one that does retrieve. There are
plenty of examples that show you how to loop thru a ResultSet.

Just remember the the order that you select the columns will be the
order they are returned in the ResultSet. Sounds like you are not doing
the correct order for calling rs.getString(1) or rs.getInt(i)...
BlabBlabBlab.

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)
Alexey Shevchenko - 27 Feb 2006 15:50 GMT
Ann, please do not use enumeration as "*". It's bad practice. Always use
only ovious values when you programming. Try to enumerate such columns you
have interested.
There is no waranty of order and count of the columns you have recieved in
your case.
What rs.getstring(1) should return if the first column is integer? Some time
it is work some time not
Good luck!

>>> Hi, excuse me, can anybody help?
>>>
[quoted text clipped - 22 lines]
> what's maybe the reason?
> Thanks, Anna
Alexey Shevchenko - 27 Feb 2006 15:53 GMT
You can use also  :)

String fieldName ="New York";
String qry ="select * from table where name_of_field like " + fieldName;

> Hi, excuse me, can anybody help?
>
[quoted text clipped - 6 lines]
> thanks in advance,
> anna kubiak


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.