Using Resultset in Java Program
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
add a comment |
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
add a comment |
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
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
java jdbc resultset
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
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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.
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 isSELECT 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 toprintf
.
– 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
|
show 2 more comments
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);
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
add a comment |
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
}
add a comment |
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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 isSELECT 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 toprintf
.
– 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
|
show 2 more comments
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.
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 isSELECT 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 toprintf
.
– 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
|
show 2 more comments
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.
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.
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 isSELECT 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 toprintf
.
– 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
|
show 2 more comments
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 isSELECT 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 toprintf
.
– 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
|
show 2 more comments
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);
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
add a comment |
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);
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
add a comment |
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);
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);
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
add a comment |
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
add a comment |
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
}
add a comment |
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
}
add a comment |
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
}
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
}
answered Oct 25 '10 at 16:27
mikejmikej
54.5k14130121
54.5k14130121
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Sep 24 '12 at 6:47
SankarSankar
129213
129213
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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