It's not a secret anymore!
The moose likes JDBC and Relational Databases and the fly likes getString() in ResultSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "getString() in ResultSet" Watch "getString() in ResultSet" New topic

getString() in ResultSet

Kelly Adams

Joined: Feb 15, 2003
Posts: 13
I am working on a code that uses JDBC to retrieve data from Oracle database. e,g,
ResultSet rt = statement.executeQuery(sql);
String s = rt.getString("User_ID") // it's 'N/A'.
if( s.equals("N/A")
It is not equal! How come? It doesn show as N/A! Is this a data type conversion thing?
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

do you move the result set forward one first?

Kelly Adams

Joined: Feb 15, 2003
Posts: 13
Yes. I did. I used

String a = rt.getString("user_ID");....
And when I print out a, it did show up as N/A. But it deson't equal to N/A. When I used the compraeTo() method in String, it should a positive integer 97. Any idea?
Suresh Selvaraj
Ranch Hand

Joined: Nov 14, 2000
Posts: 104
I tried your example and it works fine.
Here are the SQLs:
insert into temp(User_Name)
I used JDBC to retrieve the User_Name and I get the String "N/A".
Suresh Selvaraj

Suresh Selvaraj, (author of JQuiz)<br />SCJP2<br /><a href="" target="_blank" rel="nofollow"></a>
Lu Battist
Ranch Hand

Joined: Feb 17, 2003
Posts: 104
Maybe the problem is extra spaces.
String s = null;
while( {
s = rt.getString("user_ID");
if (s != null) {
s = (s.trim()).toUpperCase();
if ( s.equals("N/A") ) {
} else {
System.out.println("NotEqual s='"+s+"'");
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
If your field is varchar(N), then equals("N/A") should return true (provided that the field actually contains that string, that your query is right, etc). However, if your field is char(N), N<>3, then it won't as it'll be space-padded.
- Peter
kundi kx

Joined: Feb 03, 2003
Posts: 10
I think Battist may be right.
Print out the string fetched and decide your action from there.
System.out.println("User_ID = '"+rt.getString("User_ID") +"'");
[ February 19, 2003: Message edited by: kundi kx ]
I agree. Here's the link:
subject: getString() in ResultSet
It's not a secret anymore!