Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

My query is not working!!!

 
Elahe Shafie
Ranch Hand
Posts: 291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got solved.
Thanks,
Elahe
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got moved. (to JDBC)
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic