JDBC SUCKS BIG TIME! java.sql.SQLException: Column not found
843854Mar 17 2005 — edited Mar 21 2005Hi fellow programmers,
I'm trying this simple sql query using ACCESS.
String sql = "Select * from EMPLOYEES";
int id , age ;
try
{
Statement stmt = con.createStatement();
res = stmt.executeQuery(sql);
while ( res.next() )
{
id = res.getInt("ID");
age = res.getInt("Age");
}
} catch (Exception q)
{
q.printStackTrace();
return null;
}
Then , I got a ' column not found ' exception for 'Age'
I've checked the column names a million times , it's not it.
I've even copy - paste the column name from the database.
I've tried res.getInt(2) but then I get an 'Invalid descriptor index' exception .
( this table has over 30 columns , so , I don''t think that 2 is invalid... )
I even used the 'proper' order , that is getting the column values in a leftmost to rightmost manner as it is in the Database.
I even used :
String sql = "Select ID,Age from EMPLOYEES";
the same here...
The funny thing is that if I dismiss the while loop then it works OK :
// THIS IS OK !!! IT CAN FIND THE COLUMN NAME NOW.....
try
{
Statement stmt = con.createStatement();
res = stmt.executeQuery(sql);
res.next();
id = res.getInt("ID");
age = res.getInt("Age");
} catch (Exception q)
{
q.printStackTrace();
return null;
}
In another situation where I tried to get about 30 column values from a table ,
I got a 'column not found' exception for the last 5 column names.
I managed to get over it by altering the sequence that I was asking them :
a1 = res.getInt("ID");
a2 = res.getInt("Age");
.......
a25 = res.getInt("Something");
a26 = res.getInt("Something2");
a27 = res.getInt("Something3");
In the above I got a 'column not found' exception for 'Something'
When I :
a25 = res.getInt("Something");
a26 = res.getInt("Something2");
a27 = res.getInt("Something3");
a1 = res.getInt("ID");
a2 = res.getInt("Age");
.......
then , it worked just fine!!!!! :- )
I think this is stupid ...
It 's a fucking shame...