wood burning stoves 2.0*
The moose likes JDBC and the fly likes My query is not working!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "My query is not working!!!" Watch "My query is not working!!!" New topic
Author

My query is not working!!!

Elahe Shafie
Ranch Hand

Joined: Dec 12, 2001
Posts: 291
Hi Friends,
I try to get ID from this guery but I have no idea why ID=0 and query is not passing any thing!!!
text has value and has no problem.
Many thanks,
Elahe
----------
stmt = con.createStatement();
PrepStmt =con.prepareStatement("select ID from statusdesc where text =" +text);
System.out.println("ID"+ID);
stmt.close();


Thanks,
Elahe
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
thats now how u excute a query.
u can use wither statement or preparedstatment but u need to excute it.
if u used statement u can do:
ResultSet x=stat.executeQuesry(blah...);
then in x ull have everything u need.
also when using the SQL u need to pass the text variable in ' ' so u need to do the SQL statement like this:
select ID from statusdesc where text ='" +text+"'"
Elahe Shafie
Ranch Hand

Joined: Dec 12, 2001
Posts: 291
Hi Roy,
Thank you so much for your help I added executeQuery but regarding second point I'm not sure my query is correct I put text in ' '
Am I correct?(since still ID=0 and is not returning any thing...)
Many thanks,
Elahe
--------------
stmt = con.createStatement();
PrepStmt =con.prepareStatement("select ID from statusdesc where text ='"+text+"'");
PrepStmt.executeQuery();
System.out.println("ID"+ID);
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
ok here is the code to do that:
ResultSet result;
PrepStmt =con.prepareStatement("select ID from statusdesc where text ='"+text+"'");
result=PrepStmt.executeQuery();
result.next();
(if your ID is int then..)
int id=result.getInt(0);
(if it is String then)
String id=result.getString(0);
System.out.println(id);
Elahe Shafie
Ranch Hand

Joined: Dec 12, 2001
Posts: 291
Roy thanks again for your big help I got that part completely, but I still don't know why ID=0!! I just print out the value of ID right after executeQuery() to make sure it contains something but still doesn't!!!
Many thans,
Elahe
------------
PrepStmt =con.prepareStatement("select ID from statusdesc where text ='"+text+"'");
rs=PrepStmt.executeQuery();
System.out.println("ID"+ID);
Elahe Shafie
Ranch Hand

Joined: Dec 12, 2001
Posts: 291
Got solved.
Thanks,
Elahe
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Got moved. (to JDBC)
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Thanks Mike.
I just discovered this thread in the Servlets forum myself and wondered why it wasn't here instead...
OK, so you've solved the problem. The only thing I'd add at this stage is that if you are searching on text, maybe you should use a PreparedStatement rather than a statement to remove all of the vendor specific problems that will eventually occur.
I love my name when it comes to testing database operations. I wish I had a dollar for every time someone had a problem with the last name O'Meara...
Dave
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: My query is not working!!!