Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jdbc DRIVER and MS Sql

 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
A year back I was working with MS sql server.
My query was something like "select name, age from emp"
My code

The above code was throwing an exception but when I read the string first and then the int the code was running fine.



I dont remember the driver which I had used. I want to know if this a driver bug or is this acceptable?
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It worked fine.... I've just tried with my Oracle Database...
Have u ever tried with the names of the columns? The same way as you did.... Try with the second column come first... In my program, it works fine also with the names of the columns
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ko Ko Naing:
It worked fine.... I've just tried with my Oracle Database...
Have u ever tried with the names of the columns? The same way as you did.... Try with the second column come first... In my program, it works fine also with the names of the columns


AS I mentioned I faced the problem with MS SQL database only and unfortunately I dont remember the driver used. I know this works fine for oracle datbase. I am of the understanding that order should not matter when we use indexes for getting values from resultset.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some JDBC drivers are known to be quite picky about the order in which you read the fields. The JDBC Tutoral and API book recommends, if I remember correctly, that for best compatibility you should read all the fields from a ResultSet, in the order in which they occur in the query.
- Peter
[ October 10, 2003: Message edited by: Peter den Haan ]
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter! Is this a problem with the database or the driver?
The JDBC Tutoral and API book recommends, if I remember corectly, that for best compatibility you should read all the fields from a ResultSet, in the order in which they occur in the query.

I strongly feel that this should be mentioned in the API docs or the JDBC spec. I havent come across this when read the api or spec. Did I miss something?
I feel that debugging this will be not so easy, I took some time before I figured it out and the exception messages were not hepful.
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pradeep Bhat:


AS I mentioned I faced the problem with MS SQL database only and unfortunately I dont remember the driver used. I know this works fine for oracle datbase. I am of the understanding that order should not matter when we use indexes for getting values from resultset.

Have u tried using columns' names? I wanna know the results too... Thanx...
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ko Ko Naing:

Have u tried using columns' names? I wanna know the results too... Thanx...


no
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pradeep Bhat:
Thanks Peter! Is this a problem with the database or the driver?
Not sure. Probably the combination of the two -- this all has to do with internal implementation detail (the way the resulting data is being streamed?) and is not part of the JDBC API (AFAIK). If funny restrictions like this are mentioned anywhere, it would probably be in the JDBC driver documentation.
- Peter
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a problem that is specific to MS SQL Server(And maybe Access, too...) but it doen't matter if you use column names or column indices -- if you retreive them not in the order specified in the query, you will get an odd exception. This isn't a Java problem per se; it is a problem with the database driver. (Even if you connect to SQL Server through the JDBC-ODBC Bridge, you get the same results; the problem is on the DB end, not the driver end...)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic