Results 1 to 5 of 5
  1. #1
    mike_ledis is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default Having problem on retriving results from a query

    Hello suppose that i have the following sql table

    name clients
    field 1 c_id varchar(8)
    field 2 remaining_amount smallint(6)
    i need to execute the following statement from a java program and take the results
    SELECT remaining_amount FROM clients WHERE c_id='something'
    i tried the following code but i keep having an exception
    Java Code:
        public static boolean checkAmountAndWithdraw (String id,int amount) throws SQLException
        {
        	 
    
            String chId = null;
        	Statement stmt ; 
            ResultSet rs ; 
            PreparedStatement pstmt = dbConnection.prepareStatement("SELECT remaining_amount FROM clients WHERE c_id='"+id+"'");
         
            rs=pstmt.executeQuery();
            Boolean exists=false;
                if(rs.getInt(2)==0){
            	System.out.println ("is") ;
            	exists=true;
                
            }
            System.out.println (exists) ;
          rs.close () ;
            return exists;
        }
    }
    [

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Do you not think it might be an idea to actually tell us what the exception is (full stack trace and all)?

    Your statement isn't actually incorrect...but it is a waste of a PreparedStatement (even an abuse). Reading the link Petr supplied will show you how to use it properly. The way you have it it may as well be a Statement.

    However, the error is probably:
    Java Code:
                if(rs.getInt([B]2[/B])==0){
    You are only selecting one column in your query, whereas that line above is trying to find the third column you've requested...which doesn't exist.

  4. #4
    razmyasdfg is offline Member
    Join Date
    Jul 2008
    Location
    London (Kingsbury)
    Posts
    41
    Rep Power
    0

    Default

    Yeah Petr is right. you have a problem in Prepared Statement
    put it in this way

    PreparedStatement pstmt = dbConnection.prepareStatement("SELECT remaining_amount FROM clients WHERE c_id= ? id = ?);

    pstmt.setString (1,"pass the c_id");
    pstmt.setInt (2,pass the id)

    while (rs.next()){ write the condition witch you want to check

    }

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Which doesn't actually fix what is likely to have been the problem.
    Which is that they were trying to get data from column 2, and only had one column in their query.

    Of course they didn't give the exception or where it came from...

    And this was over 2 weeks ago.

Similar Threads

  1. problem in retriving string data using jsp
    By Manas Das in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 02-02-2009, 08:53 AM
  2. how to take query results to a jlist!!
    By themburu in forum JDBC
    Replies: 1
    Last Post: 06-07-2008, 10:51 AM
  3. how to take query results to a jlist!!
    By themburu in forum New To Java
    Replies: 3
    Last Post: 06-07-2008, 10:45 AM
  4. Retriving image from database
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-09-2008, 08:41 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
  •