Solved

problem with ResultSet.getString() ?

Posted on 2004-08-18
53
430 Views
Last Modified: 2008-01-09
Hi fellows,
IS there a specific reason why the following code would return a wrong value ?:
testStatement =
testConn.prepareStatement(
" SELECT distinct blob " +
" FROM blobtable where  blob2=

testStatement.setString(1, globalVariable1);
resultset = testStatement.executeQuery();

while(resultset.next())
{
   globalVariable2 = rs.getString(1);
}

// when I debug for globalVariable2, i get something like: CorrectString   IncorrectString
i.e. BBALL   LE
where the IncorrectString is a certain portion of globalVariable1


any thoughts? is there an unknown bug in here or is it a synch. issue?

appreciate any help you guys can provide.

mrb
0
Comment
Question by:mrb121212
  • 25
  • 19
  • 2
  • +4
53 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 11833577
You shouldn't have any problems. What happens if you run the code in a SQL client? Do you get the expected result?
0
 

Author Comment

by:mrb121212
ID: 11833636
Yes , i do get the expected result. there seems to be a problem with the statement resultset.getString(1); it returns a faulty string in Java.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11833771
u r saying that it is dintict blob in select statement.

s that blob object or string??
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11833807
if it is bob object
follow the example below

try {
        Statement stmt = connection.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT col_blob FROM mysql_all_table");
   
        if (rs.next()) {
            // Get the BLOB from the result set
            Blob blob = rs.getBlob("col_blob");
   
            // Get the number bytes in the BLOB
            long blobLength = blob.length();
   
            // Get bytes from the BLOB in a byte array
            int pos = 1;   // position is 1-based
            int len = 10;
            byte[] bytes = blob.getBytes(pos, len);
   
            // Get bytes from the BLOB using a stream
            InputStream is = blob.getBinaryStream();
            int b = is.read();
        }
    } catch (IOException e) {
    } catch (SQLException e) {
    }

Regards
Sudhakar
0
 

Author Comment

by:mrb121212
ID: 11834300
Oh sorry, i didn't think of BLOBs; no, it is JUST a String. not BLOB object. any old String.
So column blob is a char(8) column. hence resultset.getString(1).
Thanks  for bringing that up.

0
 
LVL 35

Expert Comment

by:girionis
ID: 11834405
Can you give us a bit more info?

What value do you pass as globalVariable1?
What is the expected output?
What is the value of globalVariable2 after you read the result set?

I am a bit confused with all these IncorrectString and CorrectString.
0
 
LVL 2

Expert Comment

by:beermequik
ID: 11834600
Is there possibly a null byte embedded in your db column?

That may cause it to prematurley shorten the expected value when it is a String type.
0
 

Author Comment

by:mrb121212
ID: 11834627
beermequik,
thanks for the suggestion however,
it doesn't prematurely shorten the expected value. It gives me exactly what I need plus part of some other resultset.getstring.
0
 

Author Comment

by:mrb121212
ID: 11834657
girionis,
globalVariable1 value i pass is a string say "abcdef"
the output of the resultset.getstring(1) would be not  anything like the "abcdef" that globalVariable1 is.
after I read the result set in java, globalVariable2 is like  "blob    ef"
                 if globalVariable1 was "12345"
                    globalVariable2 would have been like "try   5"
Does that make more sense?
I realize it is a stupid error to have.. but it happens...
0
 

Author Comment

by:mrb121212
ID: 11834668
oops forgot  to say : expectedoutput for first example should be "blob"
second example should be "try"
0
 
LVL 35

Expert Comment

by:girionis
ID: 11834684
> if globalVariable1 was "12345"
>                     globalVariable2 would have been like "try   5"

and what is it instead?
0
 

Author Comment

by:mrb121212
ID: 11834719
read previous comment, i forgot to add it. but i entered it right after i posted, sorry.
"try    5" is still the wrong output. correct output is "try"
0
 
LVL 35

Expert Comment

by:girionis
ID: 11834750
Ok what happens if you try to get the string by using the column's name?

resultset.getString(<column's name that holds the result>);
0
 

Author Comment

by:mrb121212
ID: 11834792
Same incorrect thing.... i get "try    5" kind of result, instead of "try"
0
 
LVL 35

Expert Comment

by:girionis
ID: 11834804
Weird... Can you post a bit more code? Also can you make sure that you read the correct value?
0
 

Author Comment

by:mrb121212
ID: 11834847
Unfortunately i can't post any more code because it is part of a very complex system with a lot of connections to the database and alot of object classes.
I do read the correct value from the database as I triple checked the query in the dbms-provided-command prompt for accessing the database. ... it is frustrating. the dmbs itself returns the correct value. it seems like the driver is not returning the correct value when resultset.getstring is used in this case...
all i can say about the code is that it is pretty much how long the method is and  resultset variable is a local var. this method gets called from another class to generate a result to be used by another class.
I appreciate the help you provided in trying to resolve the issue.
0
 
LVL 2

Expert Comment

by:beermequik
ID: 11834860
What DB are you using.....(Might be a silly question, but I catch myself on this one every once in a while.....Are you trying to read an uncommitted change?   Looking at thr right database in your code?)

0
 
LVL 35

Expert Comment

by:girionis
ID: 11834866
Can you tell us what database and driver you are using?
0
 

Author Comment

by:mrb121212
ID: 11834885
database is db2 , driver jdbc driver for db2 i believe.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11834898
Is it possible to try another driver and see if you get the same results? ot sure if thsi wil lhelp but it will be good to eliminate the "driver's fault" option.
0
 

Author Comment

by:mrb121212
ID: 11834907
beermequik, yes I double and triple checked the database, i m looking at the correct one. the thing that bothers me is that the incorrect part of the output value is part of a previous result set. which is also the globalVariable1.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11834938
> the thing that bothers me is that the incorrect part of the output
> value is part of a previous result set. which is also the globalVariable1.

Can you elaborate on that?
0
 

Author Comment

by:mrb121212
ID: 11835114
>> the thing that bothers me is that the incorrect part of the output
>> value is part of a previous result set. which is also the globalVariable1.

>Can you elaborate on that?
globalVariable1 = "12345"
globalVariable2 incorrectly is "try   5" instead of just "try" globalVariable1 gets set via interaction with the DB before this function is called. in that previous function globalVariable1 is assigned with a result of another resultset.getstring(x) x being whatever number it maybe.
if globalVariable1 were ="abcdef"
then globalVariable2 is incorrectly "try    ef" instead of just "try".
if globalVariable1 were ="shoot"
then globalVariable2 is incorrectly "try    t" instead of just "try"
0
 
LVL 35

Expert Comment

by:girionis
ID: 11835222
This is weird. Does the value "try   5" exist somewhere in the database at all or is it just thrown there somehow?
0
 

Author Comment

by:mrb121212
ID: 11835266
"try    5" does NOT exist in database anywhere ... maybe just in the metatable that the result set is creating. I dont know if thats in the actual database or in memory.
yup, this is a very stupid error to have...
0
 
LVL 35

Expert Comment

by:girionis
ID: 11835321
I am really stumped... Have you tried using a different driver?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 35

Expert Comment

by:girionis
ID: 11835355
Is there any manual that says what is the recommended corresponding java data type for char(n) db2 datat type? This link says it is a String indeed :http://publib.boulder.ibm.com/infocenter/db2v8luw/index.jsp?topic=/com.ibm.db2.udb.doc/ad/rjvjdata.htm but better check it again.
0
 
LVL 3

Expert Comment

by:kumvjuec
ID: 11835387
hi mrb121212,

Apparently the new values overwrite the older values. So your new string is at the same memory location as your older one. The one space in between indicates a terminating character over there.

Are you sure that you are closing any open resultsets and open preparedstatements before you use new ones???
Try closing everything(Even the connection if above doesn't work, although it should) before you again query thru jdbc.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11835427
Strings are immutable they shouldn't be overriding other values.

> The one space in between indicates a terminating character over there.

I noticed that too but there isn't just one space but several of them.
0
 
LVL 1

Expert Comment

by:vishalbhando
ID: 11835467
Hope you are not using native code with some pointers.. and if there are some native libs' please make sure your ptrs' are well allocated and deallocated.

Which Java version are you using ?
0
 

Author Comment

by:mrb121212
ID: 11835492
kumvjuec, i have been tending to think the same thing since this morning when I realized the problem. I looked and looked but didnt see any explanation of how getString and setString work memory-wise.
girionis, i think he means in the memory not in reality. if the space the setString is used , and 2 lines after another method is called by rule of locality, same or a close memory location will be allocated for the new routine's use.
as far as driver's are concerned, i don't think i can use a different driver. the whole system, over 1TB of data, has been working normally before we added this new entries of rows into the system.
0
 

Author Comment

by:mrb121212
ID: 11835502
vishalbhando,
using java sdk 1.3.1
0
 

Author Comment

by:mrb121212
ID: 11835506
>> Are you sure that you are closing any open resultsets and open preparedstatements before you use new ones???

I am going thru all the 2000+ result sets to make sure; that is one of my other concerns.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11835514
> f the space the setString is used , and 2 lines after another method
> is called by rule of locality, same or a close memory location will be
> allocated for the new routine's use.

So this is a good point to look at. Can you take a look and see if you set the same String in the same prepared statement a few lines below the code (or anywhere actually if you do not close the prepared statement)? I do not think this is the problem but we loose nothing to check it.
0
 

Author Comment

by:mrb121212
ID: 11841209
we close the result set promptly after.
0
 

Author Comment

by:mrb121212
ID: 11841224
i do check the rs.getString right before the result set is closed and it returns the incorrect string still in the debugging.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11841253
Well I am pretty much stumped. I'd say change driver and try again.
0
 
LVL 3

Expert Comment

by:kumvjuec
ID: 11841291
well, it would be much better if you post some code before and after your troubling code..
0
 

Author Comment

by:mrb121212
ID: 11841298
alright, thanks for trying to help though. i really appreciate it.
0
 

Author Comment

by:mrb121212
ID: 11841458
kumvjuec,
that is the essence of the method that has trouble. i do not know where it gets called from , that is basically the whole code in 1 method ...
0
 
LVL 35

Expert Comment

by:girionis
ID: 11842050
It shouldbe called from somewhere. Debug it and you will see who is calling it. Also can you psot gaain the code that fails? I cannot make loads of things from your original code since it is missing a few things.
0
 

Author Comment

by:mrb121212
ID: 11842216
oh yea, the code is missing the rest of the query for some reason from my question statement up top.
// method begin
// initialize local vars
// resultset, etc
// ...
         testStatement =
         testConn.prepareStatement(
         " SELECT distinct col1 " +
         " FROM table1 where col2 = ?");
         testStatement.setString(1, globalVar1);
         tempString=new String("");
         resultset = testStatement.executeQuery();

         tempString = rs.getString(1).trim();
         isFound = resultset.next();
//       debug code to file
         if (isFound)
         {
            tempString = resultset.getString(1);
            setGlobalVar1(tempString);
         }
//     debug code to file
         resultset.close();
         testStatement.close();
//endof method.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11842299
Can you tel lus the output of this:

testStatement = testConn.prepareStatement(" SELECT distinct col1 " + " FROM table1 where col2 = ?");
System.out.println("globalVar1: " + globalVar1);
testStatement.setString(1, globalVar1);
tempString=new String("");
System.out.println("1st tempString: " + tempString);
resultset = testStatement.executeQuery();

         tempString = rs.getString(1).trim();
System.out.println("2nd tempString: " + tempString);
         isFound = resultset.next();
//       debug code to file
         if (isFound)
         {
            tempString = resultset.getString(1);
            System.out.println("3rd tempString: " + tempString);
            setGlobalVar1(tempString);
         }
//     debug code to file
         resultset.close();
         testStatement.close();
0
 

Author Comment

by:mrb121212
ID: 11842733
please ignore the line above System.out.println("2nd tempString: " + tempString);
that is a mistake, i was shuffling around the code, forgot to take that out. good ol' runtime error made me take it out in a hurry.
so ignoring that one :
1st tempString: ""  -obviously, i m putting the "" s in.
3rd tempString: "try    5"
the culprit is rs.getString(1) - as i believe. cuz i print out rs.getString(1) to our log file and it is "try    5".
0
 
LVL 35

Assisted Solution

by:girionis
girionis earned 200 total points
ID: 11842834
Damn this is weird... I guess the globalVar1 is 12345? What happens if you do tempString = ""; instead of tempString = new String(""); or even tempString = null;
0
 

Author Comment

by:mrb121212
ID: 11842850
same thing... it IS weird.
0
 
LVL 1

Expert Comment

by:oytun12
ID: 11842887
Hi mrb121212,
Why don't you try to use a Statement and not a PreparedStatement? It seems like you are setting the ? to a global variable that you don't change. This would eliminate the problem with principle of locality if indeed that is the case with the .setString(1) and .getString(1).
0
 

Author Comment

by:mrb121212
ID: 11844291
please elaborate.
0
 
LVL 1

Accepted Solution

by:
oytun12 earned 300 total points
ID: 11844430
e.g. blob2 = '"+globalVar1+"'); instead of ?");
0
 

Author Comment

by:mrb121212
ID: 11844614
thats a working work around for the issue. i ll increase points and split it between girionis & oytun12. still no clue why it happens though. thanks guys ,esp. girionis who has been in this since yesterday morning.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11844777
Finally!!! That was a weird problem, glad it is solved :) (and I was always being told to use a PreparedStatement instead of Statement hehe).
0
 

Author Comment

by:mrb121212
ID: 11844993
yeah it is probably a WEIRD bug in the driver code that only occurs when weird scenarios are created.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11849707
:)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now