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
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;
}
it will always 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.
Re: can't get resultset to retrive data
heh you're awsome
i add
Code:
}catch(Exception e){
System.out.println("Bad Connection "+e.getMessage());
}
to the catch and it printed out
"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.
Re: can't get resultset to retrive data
yea thanks i'll keep that in mind
who knew :p
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.