Using Resultset in Java Program












8















Resultset rs=stmt.executeQuery("select count(*) from feedsca group by score order by score");


Using the above java code above, am retrieving the counts of rows from the table named feedsCA.



While trying to retrieving the counts using rs.getInt(1),rs.getInt(2),rs.getInt(3), I end with an error saying as below,



Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The result set has no current row.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.verifyResultSetHasCurrentRow(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getterGetColumn(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getInt(Unknown Source)
at SimpleMail.main(SimpleMail.java:151)


UPDATE:



The above exception has been resolved.



But I get the following exception, for which I dont know the reason. Please advise.



Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The index 2 is out of range.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.verifyValidColumnIndex(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getterGetColumn(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getInt(Unknown Source)
at SimpleMail.main(SimpleMail.java:152)


This is how I have updated my program. Find me a logical way as I can understand well that the loop below will not work as required.



rs=stmt.executeQuery("select count(*) from feedsca group by score order by score");
while(rs.next()){
pw.printf(rowFormat, rs.getLong(1),"0",rs.getLong(2),rs.getLong(3));}









share|improve this question





























    8















    Resultset rs=stmt.executeQuery("select count(*) from feedsca group by score order by score");


    Using the above java code above, am retrieving the counts of rows from the table named feedsCA.



    While trying to retrieving the counts using rs.getInt(1),rs.getInt(2),rs.getInt(3), I end with an error saying as below,



    Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The result set has no current row.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.verifyResultSetHasCurrentRow(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getterGetColumn(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getInt(Unknown Source)
    at SimpleMail.main(SimpleMail.java:151)


    UPDATE:



    The above exception has been resolved.



    But I get the following exception, for which I dont know the reason. Please advise.



    Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The index 2 is out of range.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.verifyValidColumnIndex(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getterGetColumn(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getInt(Unknown Source)
    at SimpleMail.main(SimpleMail.java:152)


    This is how I have updated my program. Find me a logical way as I can understand well that the loop below will not work as required.



    rs=stmt.executeQuery("select count(*) from feedsca group by score order by score");
    while(rs.next()){
    pw.printf(rowFormat, rs.getLong(1),"0",rs.getLong(2),rs.getLong(3));}









    share|improve this question



























      8












      8








      8


      2






      Resultset rs=stmt.executeQuery("select count(*) from feedsca group by score order by score");


      Using the above java code above, am retrieving the counts of rows from the table named feedsCA.



      While trying to retrieving the counts using rs.getInt(1),rs.getInt(2),rs.getInt(3), I end with an error saying as below,



      Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The result set has no current row.
      at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
      at com.microsoft.sqlserver.jdbc.SQLServerResultSet.verifyResultSetHasCurrentRow(Unknown Source)
      at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getterGetColumn(Unknown Source)
      at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getInt(Unknown Source)
      at SimpleMail.main(SimpleMail.java:151)


      UPDATE:



      The above exception has been resolved.



      But I get the following exception, for which I dont know the reason. Please advise.



      Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The index 2 is out of range.
      at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
      at com.microsoft.sqlserver.jdbc.SQLServerResultSet.verifyValidColumnIndex(Unknown Source)
      at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getterGetColumn(Unknown Source)
      at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getInt(Unknown Source)
      at SimpleMail.main(SimpleMail.java:152)


      This is how I have updated my program. Find me a logical way as I can understand well that the loop below will not work as required.



      rs=stmt.executeQuery("select count(*) from feedsca group by score order by score");
      while(rs.next()){
      pw.printf(rowFormat, rs.getLong(1),"0",rs.getLong(2),rs.getLong(3));}









      share|improve this question
















      Resultset rs=stmt.executeQuery("select count(*) from feedsca group by score order by score");


      Using the above java code above, am retrieving the counts of rows from the table named feedsCA.



      While trying to retrieving the counts using rs.getInt(1),rs.getInt(2),rs.getInt(3), I end with an error saying as below,



      Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The result set has no current row.
      at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
      at com.microsoft.sqlserver.jdbc.SQLServerResultSet.verifyResultSetHasCurrentRow(Unknown Source)
      at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getterGetColumn(Unknown Source)
      at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getInt(Unknown Source)
      at SimpleMail.main(SimpleMail.java:151)


      UPDATE:



      The above exception has been resolved.



      But I get the following exception, for which I dont know the reason. Please advise.



      Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The index 2 is out of range.
      at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
      at com.microsoft.sqlserver.jdbc.SQLServerResultSet.verifyValidColumnIndex(Unknown Source)
      at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getterGetColumn(Unknown Source)
      at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getInt(Unknown Source)
      at SimpleMail.main(SimpleMail.java:152)


      This is how I have updated my program. Find me a logical way as I can understand well that the loop below will not work as required.



      rs=stmt.executeQuery("select count(*) from feedsca group by score order by score");
      while(rs.next()){
      pw.printf(rowFormat, rs.getLong(1),"0",rs.getLong(2),rs.getLong(3));}






      java jdbc resultset






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 18 '18 at 9:15









      Mark Rotteveel

      60.3k1477121




      60.3k1477121










      asked Oct 25 '10 at 16:26









      LGAPLGAP

      1,218134366




      1,218134366
























          4 Answers
          4






          active

          oldest

          votes


















          12














          You have to move the cursor of the result set to a row - either by resultSet.first() or by resultSet.next(). Initially the cursor is pointing before the first row, hence your exception.



          When you want to iterate the ResultSet:



          while(rs.next()) {
          ...
          }


          Update: For your second problem - (as noted by Casablanca) your query seems to return only one column, and you are asking for a 2nd and 3rd - and they are not found. Note that in rs.getX(idx) idx is the column, not the row.






          share|improve this answer


























          • Thanks Bozho! Kindly update me on my recent exception too. See my updated question

            – LGAP
            Oct 25 '10 at 16:34






          • 3





            @LGAP: Your query is SELECT COUNT(*) ..., which returns only 1 value. Hence the only valid index is 1, that's why it complains that index 2 is out of range.

            – casablanca
            Oct 25 '10 at 16:35











          • @casablanca & Bozho Please find my updated question...

            – LGAP
            Oct 25 '10 at 16:40











          • @LGAP: How do you know how many rows will be returned? It's not a good idea to hard-code the number of parameters to printf.

            – casablanca
            Oct 25 '10 at 16:43











          • @Bozho.. My query actually returns three rows in one column. I need those three values to be retrieved. What can be done?

            – LGAP
            Oct 25 '10 at 16:48



















          5














          You need to call rs.next() before accessing the first row.



          Typically, you will iterate over the result set like this:



          ResultSet rs = ...;
          while (rs.next()) {
          ...
          }


          Update: Note that SELECT COUNT(*) ... returns only one field per row, which is the count. You may have several rows, but each row will have only one field, which has index 1. You need to iterate through the rows to get all the counts:



          while (rs.next()) {
          System.out.println(rs.getInt(1));
          }


          Yet another update: It's bad to assume that your query will always return only 3 rows. However, if you are absolutely sure of this, then you can just call next 3 times manually:



          long l1, l2, l3;
          rs.next();
          l1 = rs.getLong(1);
          rs.next();
          l2 = rs.getLong(1);
          rs.next();
          l3 = rs.getLong(1);
          pw.printf(rowFormat, l1,"0",l2,l3);





          share|improve this answer


























          • Thanks casablanca! In this case, am pretty sure that the query will return three rows only. I will go for an alternate method for any other queries with the similar scenario.

            – LGAP
            Oct 25 '10 at 17:02



















          2














          You need to use one of the methods to move the ResultSet cursor to a row before using the getxxx methods. i.e. rs.next(), rs.first() or rs.last(). These methods return true if a valid row has been located so a typical pattern is



          if (rs.first()) {
          int field1 = rs.getInt(1);
          // other columns
          }


          or for a query that returns multiple rows:



          while (rs.next()) {
          int field1 = rs.getInt(1);
          // other columns
          }





          share|improve this answer































            0














            As far as my knowledge, your query will only get one row and column i.e., the total number of rows that your query returns.



            Say for example :



            Select count(*) from emp;
            Generally this query will return a value 14.



            so your java code



            if(rs.next())
            rs.getInt(1);


            will return only one value i.e., 14



            So, How can you access rs.getString(2). This will automatically throws an exception which you got in the second case.






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4016715%2fusing-resultset-in-java-program%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              12














              You have to move the cursor of the result set to a row - either by resultSet.first() or by resultSet.next(). Initially the cursor is pointing before the first row, hence your exception.



              When you want to iterate the ResultSet:



              while(rs.next()) {
              ...
              }


              Update: For your second problem - (as noted by Casablanca) your query seems to return only one column, and you are asking for a 2nd and 3rd - and they are not found. Note that in rs.getX(idx) idx is the column, not the row.






              share|improve this answer


























              • Thanks Bozho! Kindly update me on my recent exception too. See my updated question

                – LGAP
                Oct 25 '10 at 16:34






              • 3





                @LGAP: Your query is SELECT COUNT(*) ..., which returns only 1 value. Hence the only valid index is 1, that's why it complains that index 2 is out of range.

                – casablanca
                Oct 25 '10 at 16:35











              • @casablanca & Bozho Please find my updated question...

                – LGAP
                Oct 25 '10 at 16:40











              • @LGAP: How do you know how many rows will be returned? It's not a good idea to hard-code the number of parameters to printf.

                – casablanca
                Oct 25 '10 at 16:43











              • @Bozho.. My query actually returns three rows in one column. I need those three values to be retrieved. What can be done?

                – LGAP
                Oct 25 '10 at 16:48
















              12














              You have to move the cursor of the result set to a row - either by resultSet.first() or by resultSet.next(). Initially the cursor is pointing before the first row, hence your exception.



              When you want to iterate the ResultSet:



              while(rs.next()) {
              ...
              }


              Update: For your second problem - (as noted by Casablanca) your query seems to return only one column, and you are asking for a 2nd and 3rd - and they are not found. Note that in rs.getX(idx) idx is the column, not the row.






              share|improve this answer


























              • Thanks Bozho! Kindly update me on my recent exception too. See my updated question

                – LGAP
                Oct 25 '10 at 16:34






              • 3





                @LGAP: Your query is SELECT COUNT(*) ..., which returns only 1 value. Hence the only valid index is 1, that's why it complains that index 2 is out of range.

                – casablanca
                Oct 25 '10 at 16:35











              • @casablanca & Bozho Please find my updated question...

                – LGAP
                Oct 25 '10 at 16:40











              • @LGAP: How do you know how many rows will be returned? It's not a good idea to hard-code the number of parameters to printf.

                – casablanca
                Oct 25 '10 at 16:43











              • @Bozho.. My query actually returns three rows in one column. I need those three values to be retrieved. What can be done?

                – LGAP
                Oct 25 '10 at 16:48














              12












              12








              12







              You have to move the cursor of the result set to a row - either by resultSet.first() or by resultSet.next(). Initially the cursor is pointing before the first row, hence your exception.



              When you want to iterate the ResultSet:



              while(rs.next()) {
              ...
              }


              Update: For your second problem - (as noted by Casablanca) your query seems to return only one column, and you are asking for a 2nd and 3rd - and they are not found. Note that in rs.getX(idx) idx is the column, not the row.






              share|improve this answer















              You have to move the cursor of the result set to a row - either by resultSet.first() or by resultSet.next(). Initially the cursor is pointing before the first row, hence your exception.



              When you want to iterate the ResultSet:



              while(rs.next()) {
              ...
              }


              Update: For your second problem - (as noted by Casablanca) your query seems to return only one column, and you are asking for a 2nd and 3rd - and they are not found. Note that in rs.getX(idx) idx is the column, not the row.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Oct 25 '10 at 16:51

























              answered Oct 25 '10 at 16:27









              BozhoBozho

              485k1089521064




              485k1089521064













              • Thanks Bozho! Kindly update me on my recent exception too. See my updated question

                – LGAP
                Oct 25 '10 at 16:34






              • 3





                @LGAP: Your query is SELECT COUNT(*) ..., which returns only 1 value. Hence the only valid index is 1, that's why it complains that index 2 is out of range.

                – casablanca
                Oct 25 '10 at 16:35











              • @casablanca & Bozho Please find my updated question...

                – LGAP
                Oct 25 '10 at 16:40











              • @LGAP: How do you know how many rows will be returned? It's not a good idea to hard-code the number of parameters to printf.

                – casablanca
                Oct 25 '10 at 16:43











              • @Bozho.. My query actually returns three rows in one column. I need those three values to be retrieved. What can be done?

                – LGAP
                Oct 25 '10 at 16:48



















              • Thanks Bozho! Kindly update me on my recent exception too. See my updated question

                – LGAP
                Oct 25 '10 at 16:34






              • 3





                @LGAP: Your query is SELECT COUNT(*) ..., which returns only 1 value. Hence the only valid index is 1, that's why it complains that index 2 is out of range.

                – casablanca
                Oct 25 '10 at 16:35











              • @casablanca & Bozho Please find my updated question...

                – LGAP
                Oct 25 '10 at 16:40











              • @LGAP: How do you know how many rows will be returned? It's not a good idea to hard-code the number of parameters to printf.

                – casablanca
                Oct 25 '10 at 16:43











              • @Bozho.. My query actually returns three rows in one column. I need those three values to be retrieved. What can be done?

                – LGAP
                Oct 25 '10 at 16:48

















              Thanks Bozho! Kindly update me on my recent exception too. See my updated question

              – LGAP
              Oct 25 '10 at 16:34





              Thanks Bozho! Kindly update me on my recent exception too. See my updated question

              – LGAP
              Oct 25 '10 at 16:34




              3




              3





              @LGAP: Your query is SELECT COUNT(*) ..., which returns only 1 value. Hence the only valid index is 1, that's why it complains that index 2 is out of range.

              – casablanca
              Oct 25 '10 at 16:35





              @LGAP: Your query is SELECT COUNT(*) ..., which returns only 1 value. Hence the only valid index is 1, that's why it complains that index 2 is out of range.

              – casablanca
              Oct 25 '10 at 16:35













              @casablanca & Bozho Please find my updated question...

              – LGAP
              Oct 25 '10 at 16:40





              @casablanca & Bozho Please find my updated question...

              – LGAP
              Oct 25 '10 at 16:40













              @LGAP: How do you know how many rows will be returned? It's not a good idea to hard-code the number of parameters to printf.

              – casablanca
              Oct 25 '10 at 16:43





              @LGAP: How do you know how many rows will be returned? It's not a good idea to hard-code the number of parameters to printf.

              – casablanca
              Oct 25 '10 at 16:43













              @Bozho.. My query actually returns three rows in one column. I need those three values to be retrieved. What can be done?

              – LGAP
              Oct 25 '10 at 16:48





              @Bozho.. My query actually returns three rows in one column. I need those three values to be retrieved. What can be done?

              – LGAP
              Oct 25 '10 at 16:48













              5














              You need to call rs.next() before accessing the first row.



              Typically, you will iterate over the result set like this:



              ResultSet rs = ...;
              while (rs.next()) {
              ...
              }


              Update: Note that SELECT COUNT(*) ... returns only one field per row, which is the count. You may have several rows, but each row will have only one field, which has index 1. You need to iterate through the rows to get all the counts:



              while (rs.next()) {
              System.out.println(rs.getInt(1));
              }


              Yet another update: It's bad to assume that your query will always return only 3 rows. However, if you are absolutely sure of this, then you can just call next 3 times manually:



              long l1, l2, l3;
              rs.next();
              l1 = rs.getLong(1);
              rs.next();
              l2 = rs.getLong(1);
              rs.next();
              l3 = rs.getLong(1);
              pw.printf(rowFormat, l1,"0",l2,l3);





              share|improve this answer


























              • Thanks casablanca! In this case, am pretty sure that the query will return three rows only. I will go for an alternate method for any other queries with the similar scenario.

                – LGAP
                Oct 25 '10 at 17:02
















              5














              You need to call rs.next() before accessing the first row.



              Typically, you will iterate over the result set like this:



              ResultSet rs = ...;
              while (rs.next()) {
              ...
              }


              Update: Note that SELECT COUNT(*) ... returns only one field per row, which is the count. You may have several rows, but each row will have only one field, which has index 1. You need to iterate through the rows to get all the counts:



              while (rs.next()) {
              System.out.println(rs.getInt(1));
              }


              Yet another update: It's bad to assume that your query will always return only 3 rows. However, if you are absolutely sure of this, then you can just call next 3 times manually:



              long l1, l2, l3;
              rs.next();
              l1 = rs.getLong(1);
              rs.next();
              l2 = rs.getLong(1);
              rs.next();
              l3 = rs.getLong(1);
              pw.printf(rowFormat, l1,"0",l2,l3);





              share|improve this answer


























              • Thanks casablanca! In this case, am pretty sure that the query will return three rows only. I will go for an alternate method for any other queries with the similar scenario.

                – LGAP
                Oct 25 '10 at 17:02














              5












              5








              5







              You need to call rs.next() before accessing the first row.



              Typically, you will iterate over the result set like this:



              ResultSet rs = ...;
              while (rs.next()) {
              ...
              }


              Update: Note that SELECT COUNT(*) ... returns only one field per row, which is the count. You may have several rows, but each row will have only one field, which has index 1. You need to iterate through the rows to get all the counts:



              while (rs.next()) {
              System.out.println(rs.getInt(1));
              }


              Yet another update: It's bad to assume that your query will always return only 3 rows. However, if you are absolutely sure of this, then you can just call next 3 times manually:



              long l1, l2, l3;
              rs.next();
              l1 = rs.getLong(1);
              rs.next();
              l2 = rs.getLong(1);
              rs.next();
              l3 = rs.getLong(1);
              pw.printf(rowFormat, l1,"0",l2,l3);





              share|improve this answer















              You need to call rs.next() before accessing the first row.



              Typically, you will iterate over the result set like this:



              ResultSet rs = ...;
              while (rs.next()) {
              ...
              }


              Update: Note that SELECT COUNT(*) ... returns only one field per row, which is the count. You may have several rows, but each row will have only one field, which has index 1. You need to iterate through the rows to get all the counts:



              while (rs.next()) {
              System.out.println(rs.getInt(1));
              }


              Yet another update: It's bad to assume that your query will always return only 3 rows. However, if you are absolutely sure of this, then you can just call next 3 times manually:



              long l1, l2, l3;
              rs.next();
              l1 = rs.getLong(1);
              rs.next();
              l2 = rs.getLong(1);
              rs.next();
              l3 = rs.getLong(1);
              pw.printf(rowFormat, l1,"0",l2,l3);






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Oct 25 '10 at 16:52

























              answered Oct 25 '10 at 16:27









              casablancacasablanca

              59.7k5108139




              59.7k5108139













              • Thanks casablanca! In this case, am pretty sure that the query will return three rows only. I will go for an alternate method for any other queries with the similar scenario.

                – LGAP
                Oct 25 '10 at 17:02



















              • Thanks casablanca! In this case, am pretty sure that the query will return three rows only. I will go for an alternate method for any other queries with the similar scenario.

                – LGAP
                Oct 25 '10 at 17:02

















              Thanks casablanca! In this case, am pretty sure that the query will return three rows only. I will go for an alternate method for any other queries with the similar scenario.

              – LGAP
              Oct 25 '10 at 17:02





              Thanks casablanca! In this case, am pretty sure that the query will return three rows only. I will go for an alternate method for any other queries with the similar scenario.

              – LGAP
              Oct 25 '10 at 17:02











              2














              You need to use one of the methods to move the ResultSet cursor to a row before using the getxxx methods. i.e. rs.next(), rs.first() or rs.last(). These methods return true if a valid row has been located so a typical pattern is



              if (rs.first()) {
              int field1 = rs.getInt(1);
              // other columns
              }


              or for a query that returns multiple rows:



              while (rs.next()) {
              int field1 = rs.getInt(1);
              // other columns
              }





              share|improve this answer




























                2














                You need to use one of the methods to move the ResultSet cursor to a row before using the getxxx methods. i.e. rs.next(), rs.first() or rs.last(). These methods return true if a valid row has been located so a typical pattern is



                if (rs.first()) {
                int field1 = rs.getInt(1);
                // other columns
                }


                or for a query that returns multiple rows:



                while (rs.next()) {
                int field1 = rs.getInt(1);
                // other columns
                }





                share|improve this answer


























                  2












                  2








                  2







                  You need to use one of the methods to move the ResultSet cursor to a row before using the getxxx methods. i.e. rs.next(), rs.first() or rs.last(). These methods return true if a valid row has been located so a typical pattern is



                  if (rs.first()) {
                  int field1 = rs.getInt(1);
                  // other columns
                  }


                  or for a query that returns multiple rows:



                  while (rs.next()) {
                  int field1 = rs.getInt(1);
                  // other columns
                  }





                  share|improve this answer













                  You need to use one of the methods to move the ResultSet cursor to a row before using the getxxx methods. i.e. rs.next(), rs.first() or rs.last(). These methods return true if a valid row has been located so a typical pattern is



                  if (rs.first()) {
                  int field1 = rs.getInt(1);
                  // other columns
                  }


                  or for a query that returns multiple rows:



                  while (rs.next()) {
                  int field1 = rs.getInt(1);
                  // other columns
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 25 '10 at 16:27









                  mikejmikej

                  54.5k14130121




                  54.5k14130121























                      0














                      As far as my knowledge, your query will only get one row and column i.e., the total number of rows that your query returns.



                      Say for example :



                      Select count(*) from emp;
                      Generally this query will return a value 14.



                      so your java code



                      if(rs.next())
                      rs.getInt(1);


                      will return only one value i.e., 14



                      So, How can you access rs.getString(2). This will automatically throws an exception which you got in the second case.






                      share|improve this answer




























                        0














                        As far as my knowledge, your query will only get one row and column i.e., the total number of rows that your query returns.



                        Say for example :



                        Select count(*) from emp;
                        Generally this query will return a value 14.



                        so your java code



                        if(rs.next())
                        rs.getInt(1);


                        will return only one value i.e., 14



                        So, How can you access rs.getString(2). This will automatically throws an exception which you got in the second case.






                        share|improve this answer


























                          0












                          0








                          0







                          As far as my knowledge, your query will only get one row and column i.e., the total number of rows that your query returns.



                          Say for example :



                          Select count(*) from emp;
                          Generally this query will return a value 14.



                          so your java code



                          if(rs.next())
                          rs.getInt(1);


                          will return only one value i.e., 14



                          So, How can you access rs.getString(2). This will automatically throws an exception which you got in the second case.






                          share|improve this answer













                          As far as my knowledge, your query will only get one row and column i.e., the total number of rows that your query returns.



                          Say for example :



                          Select count(*) from emp;
                          Generally this query will return a value 14.



                          so your java code



                          if(rs.next())
                          rs.getInt(1);


                          will return only one value i.e., 14



                          So, How can you access rs.getString(2). This will automatically throws an exception which you got in the second case.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Sep 24 '12 at 6:47









                          SankarSankar

                          129213




                          129213






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4016715%2fusing-resultset-in-java-program%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              這個網誌中的熱門文章

                              Tangent Lines Diagram Along Smooth Curve

                              Yusuf al-Mu'taman ibn Hud

                              Zucchini