Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java not passing int to mysql

 
peter m hayward
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic