Database SQL: Select 1st record from a table..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 08butoryr
    New Member
    • Sep 2007
    • 16

    #1

    Database SQL: Select 1st record from a table..

    I need to select the first record of a sorted table to move one of the fields of that record to a different table using an sql statement(s) in java. Please help someone?

    Thanks in advance
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Have a look at the Statement.setMa xRows() method and simply fetch the
    first row from the table.

    kind regards,

    Jos

    Comment

    • 08butoryr
      New Member
      • Sep 2007
      • 16

      #3
      Originally posted by JosAH
      Have a look at the Statement.setMa xRows() method and simply fetch the
      first row from the table.

      kind regards,

      Jos
      Excellent, it worked! The program can now select the first row and print it succesfully, but I have a problem inserting the field into the other table. I've been trying to upload a screenshot of the table but it's not working... The table is called GroupA, the fields are: Team, Played, Won, Lost, Draw, GoalsFor, GoalsAgainst and Points. I've sorted the table by Points (Descending) and I need to insert the name of the team (Team) into another table, called SemiFinals. This is the code I tried:

      Code:
      sql = "SELECT TOP 1 Team FROM GroupA";
      rs = set.executeQuery(sql);
      while (rs.next())  {
      String result = rs.getString("Team");
      sql2 = "UPDATE SemiFinals SET TeamA = '" + result + "' WHERE MatchNo = '1'";
      set.executeUpdate(sql2);  }
      This is the output error I received:
      Code:
      java.sql.SQLException: ResultSet is closed
      Thanks again in advance!

      Comment

      • 08butoryr
        New Member
        • Sep 2007
        • 16

        #4
        Never mind, I figured it out; I used the INSERT INTO statement but specififed only 1 field to insert to so it worked out. Thanks a million for your unwavering help, this is a 5-star site!

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Don't use that 'set' Statement twice for two SQL queries; use another Statement
          for your update; executing a Statement again closes a previously opened ResultSet.
          Read the API documentation for the Statement interface.

          kind regards,

          Jos

          Comment

          Working...