Problem with getString()
Hello ,
I am using MySql with java 1.6 and the connector I am using is mysql-connector-java-5.0.5-bin.jar
The database auth has three fields - email,password & pos (all three being varchars)
Here is a part of the code
PreparedStatement checkDB = con.prepareStatement("SELECT pos FROM auth WHERE email='?' AND password='?' ;");
checkDB.setString(1, Email);
checkDB.setString(2, Upwd);
res = checkDB.executeQuery();
while (res.next() ){
result=true;
who= res.getString("pos");
if(who.equalsIgnoreCase("GM")){
System.out.println("diagnostic: " + pos);
break;
}elseif(who.equalsIgnoreCase("plr")){
System.out.println("diagnostic : " + pos);
break;
}
}
The problem being that with res.getString() i always get a null value , I have tried to use the index as well but there seems to be no change in the output even when there is a valid pos (there are only two possible values for it as of now GM and plr)
Any help will be appreciated
Regards,
Flummoxed
[1636 byte] By [
Flummoxeda] at [2007-11-27 3:28:30]

# 2
> You don't need to quote the values in the
> PreparedStatement. Remove the singlequotes from the
> questionmarks and see if it works.
Yes I tried it without the quotes and even with a Statement (gave values directly),
but the getString() still returns a null value. Is this an issue with getString or is concerned to ResultStatement ?
# 4
No the getString() part did not work with the regular Statement (even after I removed the semicolon at the end )
The query works fine for both PreparedStatement as well as for regualr Statement , it is only with the getString part that there is a problem , getString always seems to return null even when i use column indices ( 1,2 & 3 ) .
Thank You,
Flummoxed
# 6
As BalusC said, remove the semicolon from your select query :
Use this :
PreparedStatement checkDB = con.prepareStatement("SELECT pos FROM auth WHERE email=? AND password=? ");
By The way, are you sure that there is data in your auth table ?
Try the sql select query directly using an sql client.
# 8
Well I don't know what i did right this time around but the program worked !!
I haven't changed anything at all from the original code (just the semicolon has been removed) . I think it was supposed to work only at the 376th run ;)
Thanks for all the help ,
Would just like to know if the efficiency increases if I use a column index instead of the column name if i am accessing the DB in bursts
Example : In a minute i may have say close to 100 access attempts and then there is a period of say more than 30 min without any attempts at all
Regards,
Flummoxed
# 9
> @dcminter
> I meant the function return a null value (there is no
> exception generated)
Assuming you're showing us the right code, that's not possible.
who= res.getString("pos");
if(who.equalsIgnoreCase("GM")){ ...
If who is null, you're trying to call a method (equalsIgnoreCase) on a null pointer. Which will always, always, produce a null pointer exception.
getString might be returning a string containing the text "null", but the pointer itself cannot be null.
Although the program is "working", I strongly recommend that you should spend some time figuring out what actually went wrong. If you don't know how you fixed the problem, you're not going to know how to fix it if it does it again.
I would add that it's quite possible that the variable called "pos" was null all along, but that wasn't acquired from the getString method - you assign that result to the variable called "who". Is this perhaps where your confusion arose?