Domanda

If I am running the following piece of code rs.next should return values starting from the first row of the table instead its returning the values from second row.

        String val = "select bl.book_id,bl.branch_id, bl.card_no,bl.date_out, bl.due_date ,concat(b.fname,' ',b.lname) as borrower from book_loans bl inner join borrower b on bl.card_no=b.card_no where bl.book_id like '%"+lm.getBook_id()+"%' and bl.card_no like '%"+lm.getCard()+"%'";
            rs=st.executeQuery(val);
            if(rs.next())
            {
                for(int a=0;rs.next();a++)
                {   
                    System.out.println("hello# "+a);
                    lm.book_ids.add(rs.getString(1));
                    lm.branch_id.add(rs.getInt(2));
                    lm.cards.add(rs.getString(3));
                    lm.date_out.add(rs.getString(4));
                    lm.due_date.add(rs.getString(5));
                }
                for(int a=0;a<lm.branch_id.size();a++)
                {
                    System.out.println(lm.book_ids.get(a));
                    System.out.println(lm.branch_id.get(a));
                    System.out.println(lm.cards.get(a));
                    System.out.println(lm.date_out.get(a));
                    System.out.println(lm.due_date.get(a));
                }
                return 1;
            }
            return 2;

I am running the exact query in MySQL and its giving the proper value but its not in this code. Its showing in sql

Book ID Branch ID Card No Date Out Due Date

72127317 1 9009 2013-11-25 2013-12-09

72127317 3 9009 2013-11-25 2013-12-09

and in here its retuning as follows

hello# 0 72127317 3 9009 2013-11-25 2013-12-09

È stato utile?

Soluzione

I believe your problem is here

if(rs.next()) // get the first record
{
for(int a=0;rs.next();a++) // get the second (and later) records....

You could use a do while loop like this instead -

int a = 0;
do 
{   
  System.out.println("hello# "+a);
  lm.book_ids.add(rs.getString(1));
  lm.branch_id.add(rs.getInt(2));
  lm.cards.add(rs.getString(3));
  lm.date_out.add(rs.getString(4));
  lm.due_date.add(rs.getString(5));
  a++;
} while (rs.next());

Altri suggerimenti

I think your problem is in for clause also . You may try

  int a = 0;
    while (rs.next()){
        System.out.println("hello# "+a);
        lm.book_ids.add(rs.getString(1));
        lm.branch_id.add(rs.getInt(2));
        lm.cards.add(rs.getString(3));
        lm.date_out.add(rs.getString(4));
        lm.due_date.add(rs.getString(5));
        a++; 

  }

do ... while clause may cause nullPoint Exception.ResultSet cursor is initially positioned before the first row. And you need to add order by to you sql.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top