Results 1 to 6 of 6
  1. #1
    smacker is offline Member
    Join Date
    Jan 2011
    Posts
    46
    Rep Power
    0

    Default 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
    Java 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    smacker is offline Member
    Join Date
    Jan 2011
    Posts
    46
    Rep Power
    0

    Default Re: can't get resultset to retrive data

    heh you're awsome
    i add
    Java 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

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  5. #5
    smacker is offline Member
    Join Date
    Jan 2011
    Posts
    46
    Rep Power
    0

    Default Re: can't get resultset to retrive data

    yea thanks i'll keep that in mind
    who knew :p

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default 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.

    ** This space for rent **

Similar Threads

  1. Replies: 2
    Last Post: 08-09-2011, 07:52 PM
  2. cannot retrive data from mysql
    By tmukmkd in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 10-24-2010, 08:26 AM
  3. How to retrive a string array data from a session
    By shandi in forum New To Java
    Replies: 1
    Last Post: 09-16-2010, 02:02 PM
  4. Retrive from DB
    By Dileep in forum JDBC
    Replies: 12
    Last Post: 09-25-2009, 09:58 AM
  5. Collecting ResultSet data and Comparing
    By casid in forum New To Java
    Replies: 4
    Last Post: 06-08-2009, 08:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •