Two Laptop Bag*
The moose likes JDBC 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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "java not passing int to mysql" Watch "java not passing int to mysql" New topic
Author

java not passing int to mysql

peter m hayward
Ranch Hand

Joined: Sep 27, 2012
Posts: 31
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
author
Bartender

Joined: May 14, 2008
Posts: 5812
    
    7

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
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: java not passing int to mysql
 
Similar Threads
cannot write a new customer to the database
Using a prepared statement
What is -Xlint in compiling java program?why this is done?
problem in button ActionListener.
Another "com.mysql.jdbc.Driver" Connection problem