• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

reading in a row from a table one row at a time

I need to read in a row from a table, one row at a time.  so lets say i have 10 rows, i need to be able to read in just one row at a time from one to ten, and sleep every time after it reads in a row if a certain condition is met.
currently i have

rs = stmt.execute("select * from table_name)
if(rs.first())

but this isnt what i want to do obviouslly.  i want to read in one row, then do something then check if tablename2 has something in it, if so then sleep for a period of time, then run again and read in the next row and do the same check as before. and so on and so forth.

read in row
do something
sleep
read in another row
do something
sleep

the api doesnt appear to have anything to read in a row at a time and to make note of every row that has been read in and every row that has to be read in yet.
thanks in advance
0
pronane
Asked:
pronane
  • 8
  • 4
  • 3
  • +1
1 Solution
 
girionisCommented:
Use

while (rs.next())
0
 
pronaneAuthor Commented:
ok so are you saying that this will work for me
sleep for a few seconds and return to the top

public void run()
{
      currentThread.sleep();
Resultset rs = stmt.execute("select * from table2");
if (rs!=null)
{
      currentThread.sleep();
      //whatever amount of seconds
}

Resultset rs1 = stmt1.execute("select * from table1")
      while(rs1.next())
       //process row here
      
      return
}
will this make only one row be read in and then return to the top again?
      will that do
      what i am trying to do
      i.e. return to the top and start all over again?
0
 
girionisCommented:
Put everything in the while loop then:

public void run()
{
    while (true)
    {
   Resultset rs = stmt.execute("select * from table2");
   if (rs!=null)
   {
       currentThread.sleep();
       //whatever amount of seconds
   }


Resultset rs1 = stmt1.execute("select * from table1")
     while(rs1.next())
      //process row here

   }
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mudriCommented:
Can this do the work?

public void Run() {
    while (true)
    {
      ResultSet rs2 = stmt.execute("select * from table2");
      while (rs2.next()) {
        ResultSet rs1 = stmt.execute("select * from table1");
        if (rs1.next()) {
          int paramOne = rs1.getInt(0);
          int paramTwo = rs2.getInt(0);
          // do something with results rs1 and rs2
        } else {
          currentThread.sleep();// some amount of time
        }
      }
    }
0
 
pronaneAuthor Commented:
Resultset rs1 = stmt1.execute("select * from table1")
     while(rs1.next())
      //process row here

   }


what happens here though, cos i want to get to back to the top so that it will check again if there is anything in teh queue and start all over again???  how does it get back to the top?
0
 
pronaneAuthor Commented:
what i mean is i want it to go back to the top and check if there is something in table 2 and if there is i want it to sleep again if not process JUST ONE row and go back to the top.

does while(rs.next())  keep doing it while there is rows in teh table?
0
 
pronaneAuthor Commented:
ya i have to read from if(rs.next())
0
 
mudriCommented:
Little change of previous code to fit multiple results:

public void Run() {
    while (true)
    {
      ResultSet rs2 = stmt.execute("select * from table2");
      while (rs2.next()) {    // only position pointer to new row in result set
        int paramTwo = rs2.getInt(0);    // read from second table

        ResultSet rs1 = stmt.execute("select * from table1");  // search table 2
        while (rs1.next()) {    // in case of multiple results, replace 'if' with 'while'
          int paramOne = rs1.getInt(0);   // read from first result set where pointer is

          // do something with results rs1 and rs2
         int result = paramOne + paramTwo; // for example :)
        } else {
          currentThread.sleep();// some amount of time
        }
      }
    }
0
 
pronaneAuthor Commented:
the problem is though after readin one line in from table 1
how do i get it to go back up to the top to check again if there is something in table2?? and if so sleep again

the priority is if there is something in table 2.  i have tried all them none are fully effective
0
 
pronaneAuthor Commented:

ResultSet rs1 = stmt.executeQuery("select * from table2);
                               while(rs1.next())
                               {
                                     logger.debug("There are rows in the queue, sleeping for " + POLL_INTERVAL/1000/60 + " minutes");
                                    try
                                    {                                  
                                          runner.sleep(POLL_INTERVAL); // sleep
                                    }
                                    catch (InterruptedException ie)
                                    {
                                          ie.printStackTrace();
                                    }
                                    //rs1 = stmt.executeQuery                               }


this just keeps outputing to a log file:

there are rows in teh queue sleeping for 1 minute even though the table2 is empty

can anyone help
0
 
mudriCommented:
Then, what you need is:
while(true) {
   ResultSet rs1 = stmt.executeQuery("select * from table2);
   if (rs1.next())
    {
      logger.debug("There are rows in the queue, sleeping for " + POLL_INTERVAL/1000/60 + " minutes");
    try
    {                                  
    runner.sleep(POLL_INTERVAL); // sleep
    }
    catch (InterruptedException ie)
    {
    ie.printStackTrace();
    }
}
}

0
 
pronaneAuthor Commented:
i really answered my own question here, is it possible to retracct this question?
0
 
girionisCommented:
Ask a question to community support: http://www.experts-exchange.com/Community_Support/
0
 
girionisCommented:
Fine by me.
0
 
pronaneAuthor Commented:
public void run()
{
     Resultset rs = stmt.execute("select * from testtable2");
     Resultset rs1 = statement("select * from testtable1");
while (rs.next)
{
     vector.add("another element added to table");
}

     if(rs1.next())
      {
        break;
      }
     return
}
0
 
moduloCommented:
Closed, 75 points refunded.

modulo
Community Support Moderator
Experts Exchange
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 8
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now