aspose file tools*
The moose likes JDBC and the fly likes Prepared Statement vs. select Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Prepared Statement vs. select" Watch "Prepared Statement vs. select" New topic
Author

Prepared Statement vs. select

Faiz Abdelhafid
Ranch Hand

Joined: Nov 21, 2009
Posts: 32
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
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

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

Joined: Nov 21, 2009
Posts: 32
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
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

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

Joined: Nov 21, 2009
Posts: 32
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

Joined: Jun 21, 2008
Posts: 423
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

Joined: Nov 21, 2009
Posts: 32
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

Joined: Mar 05, 2010
Posts: 12
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..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Prepared Statement vs. select