Results 1 to 6 of 6
- 10-07-2012, 04:03 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 39
- Rep Power
- 0
can't get resultset to retrive data
hello
i have a table of books in there the fields id , name , ammount
i want to retrive if a book is available
so i did
it will always return -1Java Code:try{ Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","",""); Statement stmt= con.createStatement(); ResultSet rs=stmt.executeQuery("SELECT ammount FROM lib.BOOKS WHERE name='"+name+"'"); if(rs.getInt("ammount")>0) return 1; if(rs.getInt("ammount")==0) return 0; return -1; }catch(Exception e){} return -1; }
i debuged it and it connects to the database and does the statement
but on the third row of (ResultSet rs=stmt.executeQuery)
it jumps to the last row
something wrong with that syntacs?
thanks to all the helpers
-
Re: can't get resultset to retrive data
For one, you should never have an empty catch block, since it's like driving a car blind. At least print out the stacktrace to be sure that no exceptions are being thrown.
- 10-07-2012, 05:01 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 39
- Rep Power
- 0
Re: can't get resultset to retrive data
heh you're awsome
i add
to the catch and it printed outJava Code:}catch(Exception e){ System.out.println("Bad Connection "+e.getMessage()); }
"Bad Connection Before start of result set"
so i add rs.next(); before the two ifs
thanks a lot my man
-
Re: can't get resultset to retrive data
You're welcome. As a general rule, you want to avoid empty catch blocks for this very reason. I can only think of few times I actually leave mine empty. Thread.sleep(...) and where I specifically want to ignore a certain exception comes to mind, but that's about it.
- 10-07-2012, 07:10 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 39
- Rep Power
- 0
Re: can't get resultset to retrive data
yea thanks i'll keep that in mind
who knew :p
- 10-08-2012, 11:06 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: can't get resultset to retrive data
You also don't want to get the same piece of data from the result set more than once.
Some drivers react badly to that.
Get it once and store it in a local variable then do your comparisons on that variable.
Also, for the exception, you want to at the minimum do e.printStackTrace(), otherwise you won't know the exact line that caused the error.Please do not ask for code as refusal often offends.
Similar Threads
-
Need help finding tutorials on populating GUI with SQL resultset data
By ravl13 in forum AWT / SwingReplies: 2Last Post: 08-09-2011, 07:52 PM -
cannot retrive data from mysql
By tmukmkd in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 10-24-2010, 08:26 AM -
How to retrive a string array data from a session
By shandi in forum New To JavaReplies: 1Last Post: 09-16-2010, 02:02 PM -
Retrive from DB
By Dileep in forum JDBCReplies: 12Last Post: 09-25-2009, 09:58 AM -
Collecting ResultSet data and Comparing
By casid in forum New To JavaReplies: 4Last Post: 06-08-2009, 08:43 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks