1

i am trying to get all the data from a certain table.but it's only giving me only the first row twice a result (as i have two rows in the database)

here is my code

   String data[]=new String[10];
     String[] result;
     Product p= new Product();
     int serial=0;
       try{
            String sql="select * from product";
            rslt=st.executeQuery(sql);  //where private static Statement st, private static ResultSet rslt;
            while(rslt.next()){
                data[1]=rslt.getString("p_code");
                data[2]=rslt.getString("p_name");
               /* data[3]=rslt.getString("description");
                  data[4]=rslt.getString("measurement");
                  data[5]=Integer.toString(p.RemainProduct(data[1]));
                  data[6]=p.getSellPrice(data[1]);
                  serial+=1;
                  data[0]=Integer.toString(serial);
                  DTB.addRow(data); */
                  System.out.println("code :"+data[1]+" "+"Name :"+data[2]);
              }

     }catch(Exception ex){
         System.out.println("ERROR :"+ex);

     } 

my table has two data, here is my database table data

enter image description here

and here is the result after i run the program.

enter image description here

i don't know where is the actual problem. the same code works fine on the other method but why i can't get it here. i am very new to java, please help me fixing this problem

4
  • Your code looks OK to me. Based on title and tags, I assume your DB is MySQL? Which JDBC driver version are you using? Commented Aug 15, 2013 at 16:28
  • mysql jdbc connector from their official site Commented Aug 15, 2013 at 16:41
  • Is this the actual real code your compiling and running? Commented Aug 15, 2013 at 16:46
  • @RahatIslamKhan The official JDBC connector is used without problems in thousands, if not hundreds of thousands, of applications and is unlikely to have a bug when advancing the cursor via ResultSet.next(). The actual error is probably somewhere else in your program. I don't see anything wrong in your example. Commented Aug 15, 2013 at 16:54

1 Answer 1

1

Your code looks correct to me. I think this is a case of "what you are looking at is not what is broken". Perhaps you are not querying the table you think you are querying, or perhaps your table view is cached and out of date. Refresh your table view, and try printing out the PK (id column in the table) in your program output as a double check that the table your are querying is the correct table. Also, double check your JDBC URL and verify you are querying the correct database.

Also, your comments indicate that the same code is giving correct results in a different method. Maybe there is more going on in your actual code that we don't have insight into? Perhaps your code (not shown in your example) is:

  1. Updating the table so that row 1 and 2 have the same data.
  2. Querying the database (as shown in your example), and returning same data in row 1 and 2.
  3. Update the table so that row 1 and 2 now have different data which shows up when you manually query the database.

Finally, make sure some of your other team members or an automated test process isn't updating the table without your knowledge.

Sign up to request clarification or add additional context in comments.

3 Comments

If the JDBC url is wrong then how i am getting result..? i edited the code but not accurate result.still i am confused where is the problem..the same code is giving result to another method...and i have checked the database and table many times..
If you've duplicated your DB for redundancy, your JDBC url might be pointed to a different database or different server with the same table name, but with different records.
no i have no other db like this name and also not this kind of table name..i have checked this query to another table it has two rows but after compiling it is giving me 4 rows (each row double)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.