It's not a secret anymore!
The moose likes JDBC and Relational Databases and the fly likes java not passing int to mysql Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "java not passing int to mysql" Watch "java not passing int to mysql" New topic

java not passing int to mysql

peter m hayward
Ranch Hand

Joined: Sep 27, 2012
Posts: 41
this is my code snippet

in this example the result of the first query is that the author value =35216 if this
is place instead of the variable a then it =works so why is wrong with placing a in the
query that stops it working correctly


Peter Johnson

Joined: May 14, 2008
Posts: 5852

The PHP forum isn't the correct place for help with Java JDBC code, moving to our JDBC forum where you are more likely to get help.

JBoss In Action
Martin Vajsar

Joined: Aug 22, 2010
Posts: 3733

Welcome to the Ranch, Peter!

If I understand you correctly, you expect this line to run a query which will find records where author_id equals to the value stored in local variable a.

It does't work this way. What you need to do is use a PreparedStatement and place a question mark instead of the bits you want to replace with a value. You then use separate method to specify the value of the parameter:
There are still issues to handle: you need to close all resultset, statements and connections in the finally clause (much easier with Java 7 try-with-resources clause), and you should avoid implicit conversions (I'd guess that author_id is an integer, if it is, you should use setInt or setLong, not setString).

I'd suggest reading Oracle's JDBC tutorial, it is a very good resource on JDBC. If there is something unclear in the tutorial, please feel free to ask here.
I agree. Here's the link:
subject: java not passing int to mysql
It's not a secret anymore!