how to cast the result to String From Native Query in Java?
up vote
0
down vote
favorite
i have a query which will return single record with 2 columns from Table
i want to get the result to a List each element hold a column value , but i keep getting ClassCastExceptoion
this is the code :
public List<String> getStatus(Long requestId) {
List result = new ArrayList();
if (requestId != null)
{
StringBuilder querySBuilder = new StringBuilder();
querySBuilder.append(" select R.request_status_id , L.request_status_desc ");
querySBuilder.append(" from Table1 R join Table2 L ");
querySBuilder.append(" on R.request_status_id = L.REQUEST_STATUS_Id ");
querySBuilder.append(" where R.REQUEST_ID = " + requestId);
System.out.print(querySBuilder.toString());
List resultList =
em.createNativeQuery(querySBuilder.toString()).getResultList();
Vector resultVec = (Vector)resultList.get(0);
int id = ((BigDecimal)resultVec.elementAt(0)).intValue();
String statusName = ((String)resultVec.elementAt(0));
System.out.println("id" + id);
System.out.println("name " + statusName);
result.add(id);
result.add(statusName);
if (resultVec == null || resultVec.isEmpty()) {
return new ArrayList<String>();
}
return result;
}
return null;
}
java mysql ejb
add a comment |
up vote
0
down vote
favorite
i have a query which will return single record with 2 columns from Table
i want to get the result to a List each element hold a column value , but i keep getting ClassCastExceptoion
this is the code :
public List<String> getStatus(Long requestId) {
List result = new ArrayList();
if (requestId != null)
{
StringBuilder querySBuilder = new StringBuilder();
querySBuilder.append(" select R.request_status_id , L.request_status_desc ");
querySBuilder.append(" from Table1 R join Table2 L ");
querySBuilder.append(" on R.request_status_id = L.REQUEST_STATUS_Id ");
querySBuilder.append(" where R.REQUEST_ID = " + requestId);
System.out.print(querySBuilder.toString());
List resultList =
em.createNativeQuery(querySBuilder.toString()).getResultList();
Vector resultVec = (Vector)resultList.get(0);
int id = ((BigDecimal)resultVec.elementAt(0)).intValue();
String statusName = ((String)resultVec.elementAt(0));
System.out.println("id" + id);
System.out.println("name " + statusName);
result.add(id);
result.add(statusName);
if (resultVec == null || resultVec.isEmpty()) {
return new ArrayList<String>();
}
return result;
}
return null;
}
java mysql ejb
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
i have a query which will return single record with 2 columns from Table
i want to get the result to a List each element hold a column value , but i keep getting ClassCastExceptoion
this is the code :
public List<String> getStatus(Long requestId) {
List result = new ArrayList();
if (requestId != null)
{
StringBuilder querySBuilder = new StringBuilder();
querySBuilder.append(" select R.request_status_id , L.request_status_desc ");
querySBuilder.append(" from Table1 R join Table2 L ");
querySBuilder.append(" on R.request_status_id = L.REQUEST_STATUS_Id ");
querySBuilder.append(" where R.REQUEST_ID = " + requestId);
System.out.print(querySBuilder.toString());
List resultList =
em.createNativeQuery(querySBuilder.toString()).getResultList();
Vector resultVec = (Vector)resultList.get(0);
int id = ((BigDecimal)resultVec.elementAt(0)).intValue();
String statusName = ((String)resultVec.elementAt(0));
System.out.println("id" + id);
System.out.println("name " + statusName);
result.add(id);
result.add(statusName);
if (resultVec == null || resultVec.isEmpty()) {
return new ArrayList<String>();
}
return result;
}
return null;
}
java mysql ejb
i have a query which will return single record with 2 columns from Table
i want to get the result to a List each element hold a column value , but i keep getting ClassCastExceptoion
this is the code :
public List<String> getStatus(Long requestId) {
List result = new ArrayList();
if (requestId != null)
{
StringBuilder querySBuilder = new StringBuilder();
querySBuilder.append(" select R.request_status_id , L.request_status_desc ");
querySBuilder.append(" from Table1 R join Table2 L ");
querySBuilder.append(" on R.request_status_id = L.REQUEST_STATUS_Id ");
querySBuilder.append(" where R.REQUEST_ID = " + requestId);
System.out.print(querySBuilder.toString());
List resultList =
em.createNativeQuery(querySBuilder.toString()).getResultList();
Vector resultVec = (Vector)resultList.get(0);
int id = ((BigDecimal)resultVec.elementAt(0)).intValue();
String statusName = ((String)resultVec.elementAt(0));
System.out.println("id" + id);
System.out.println("name " + statusName);
result.add(id);
result.add(statusName);
if (resultVec == null || resultVec.isEmpty()) {
return new ArrayList<String>();
}
return result;
}
return null;
}
java mysql ejb
java mysql ejb
asked Nov 19 at 11:12
osfar
2216
2216
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
The pattern I would use would be to phrase the incoming native result set as a List<Object>
, where each object array represents a single record:
Query q = em.createNativeQuery(querySBuilder.toString());
List<Object> result = q.getResultList();
for (Object row : result) {
int id = (Integer)row[0];
String statusName = (String)row[1];
// do something with the id and statusName from above
}
add a comment |
up vote
0
down vote
I think it is a typo.
String statusName = ((String)resultVec.elementAt(0));
The elementAt(1) should be used instead of elementAt(0).
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
The pattern I would use would be to phrase the incoming native result set as a List<Object>
, where each object array represents a single record:
Query q = em.createNativeQuery(querySBuilder.toString());
List<Object> result = q.getResultList();
for (Object row : result) {
int id = (Integer)row[0];
String statusName = (String)row[1];
// do something with the id and statusName from above
}
add a comment |
up vote
1
down vote
accepted
The pattern I would use would be to phrase the incoming native result set as a List<Object>
, where each object array represents a single record:
Query q = em.createNativeQuery(querySBuilder.toString());
List<Object> result = q.getResultList();
for (Object row : result) {
int id = (Integer)row[0];
String statusName = (String)row[1];
// do something with the id and statusName from above
}
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The pattern I would use would be to phrase the incoming native result set as a List<Object>
, where each object array represents a single record:
Query q = em.createNativeQuery(querySBuilder.toString());
List<Object> result = q.getResultList();
for (Object row : result) {
int id = (Integer)row[0];
String statusName = (String)row[1];
// do something with the id and statusName from above
}
The pattern I would use would be to phrase the incoming native result set as a List<Object>
, where each object array represents a single record:
Query q = em.createNativeQuery(querySBuilder.toString());
List<Object> result = q.getResultList();
for (Object row : result) {
int id = (Integer)row[0];
String statusName = (String)row[1];
// do something with the id and statusName from above
}
answered Nov 19 at 11:18
Tim Biegeleisen
211k1382129
211k1382129
add a comment |
add a comment |
up vote
0
down vote
I think it is a typo.
String statusName = ((String)resultVec.elementAt(0));
The elementAt(1) should be used instead of elementAt(0).
add a comment |
up vote
0
down vote
I think it is a typo.
String statusName = ((String)resultVec.elementAt(0));
The elementAt(1) should be used instead of elementAt(0).
add a comment |
up vote
0
down vote
up vote
0
down vote
I think it is a typo.
String statusName = ((String)resultVec.elementAt(0));
The elementAt(1) should be used instead of elementAt(0).
I think it is a typo.
String statusName = ((String)resultVec.elementAt(0));
The elementAt(1) should be used instead of elementAt(0).
answered Nov 19 at 11:16
Swaroop Wakade
11
11
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53373375%2fhow-to-cast-the-result-to-string-from-native-query-in-java%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