Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Prepared Statement vs. select

 
Faiz Abdelhafid
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello folks,

I am trying to use a prepared statement to select a particular row or a set of rows (does not matter ! ). I could use prepared statement to update rows but not to retrieve them from a MySQL Database.
I used parametric statements in other languages like php and asp, it must be a solution with JDBC, to get a result from a parametric query.




The code runs without errors but also without results !

waiting for your comments

Thanks !
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that looks like invalid SQL to me. I don't know what it's supposed to return, I can't even figure out what the column names in the resulting table would be.

But besides that, you do need to set the parameters for the PreparedStatement before you execute the query.
 
Faiz Abdelhafid
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

My query is something like



parameter is an integer value like 2093, 2039, etc.

to clarify more my request:
If I would re-write it in another language it would be like the following:



what do you suggest ?

Thank you
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you set the parameters for the PreparedStatement before you execute the query. In fact I believe I already suggested that.
 
Faiz Abdelhafid
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Yes in fact you mentioned that before, but you also wrote that my sql query is invalid.

besides the parameters where is the problem ?

Thanks
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Faiz Abdelhafid wrote:
Yes in fact you mentioned that before, but you also wrote that my sql query is invalid.
besides the parameters where is the problem ?

Hi,

here is your code:

so your query is like this:


I am not an SQL expert, but as I know, comparison (logical) operator '=' is allowed in expressions after a WHERE/HAVING keywords,
but is not allowed after a SELECT keyword.
I tried to run this query on Oracle and got an error: "FROM keyword not found where expected"
 
Faiz Abdelhafid
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I already found the correct syntax. First, I thought that Java parametric Queries are just like any other language, but it's a little bit different from PHP and ASP.



Best Regards,
 
Jinglong Wang
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be you code is wrong..
I think it's like this:

remember try catch the exception and throw out so that you can know where the wrong..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic