Result returned by query execution

Say i do a select * from some_table, then from the result set i get my results by saying getString(1),getString(2) etc...now I add a column in some_table. Now can my application break. I mean since I am doing getString(1) etc, is it possible that in the resultset i can get any column as 1,2,3 etc....?

[309 byte] By [javanewbie80a] at [2007-11-27 9:12:17]
# 1
hi yes , can,, but your first column is String type , then no problem..because you use rs.getString(1) before altering (adding ) column in table,so after adding column also no problem to get like this...
drvijayy2k2a at 2007-7-12 21:58:37 > top of Java-index,Java Essentials,Java Programming...
# 2

If your table has columns some_column and another_column, I consider it much better practice to use getString("some_column"), getString("another_column"), etc. This is slightly less efficient, but it won't break if the columns are rearranged or a new column is inserted before some of the existing ones, or the query is changed to, say, select another_column, some_column from some_table (which would also be better practice, and more efficient if you only use some of the columns from the table).

I'm not sure it answered your question? I hope you can use it anyway.

OleVVa at 2007-7-12 21:58:37 > top of Java-index,Java Essentials,Java Programming...
# 3
so does this mean that in resultset the sequence of columns which are returned can be random. i.e. its not sure that columns number 1,2,3 will always come as 1,2,3....it could come as 2,1,3 also..right?
javanewbie80a at 2007-7-12 21:58:37 > top of Java-index,Java Essentials,Java Programming...
# 4

No, I definitely don't think so.

I have seen many programmers write getString(1), getDate(4), etc. (and it hurts a little bit every time), and I have heard nobody complain that results got randomly shuffled. After all, since getString(int) is in the API, I take it that it's supposed to give you a predictable (deterministic) result. It's just poorly suited for the inevitable changes to the query and the database during maintenance of your system. And more difficult to read.

OleVVa at 2007-7-12 21:58:37 > top of Java-index,Java Essentials,Java Programming...
# 5

see, i know one thing, that if you write select firstname,lastname,address from employee kind of query is there then if you do getString(1) etc, you will always get firstname as the value of getString(1). But my question is that if i write select * from employee , then in this case, if i do getString(1), will i always get firstname or is it possible that i might get address or lastname as well ?

javanewbie80a at 2007-7-12 21:58:37 > top of Java-index,Java Essentials,Java Programming...
# 6

The true answer is I don't know.

My best guess is it depends on how your RDBMS (be it Oracle, MySQL, Informix, Postgres, Ingres, Access, MS SQL Server or whatever) interprets select * from ...

The relational theory I've learned says that columns can be shuffled at random, or put the other and more correct way, they have no defined order. Probably the SQL standard says something about this, I haven't checked, and if it does, I would expect it to say that columns *have* an order and cannot be shuffled. And I would definitely expect the RDBMSs I mentioned and all commercial RDBMSs to maintain column order.

So I can find no simple answer. If you insist on one anyway, choose from:

(1) Check the documentation of your RDBMS.

(2) My guess is that getString(1) will always return the same column in practice.

OleVVa at 2007-7-12 21:58:37 > top of Java-index,Java Essentials,Java Programming...
# 7
if u use select * from ( table-name) qry also return the ordered result set column.likegetString(1)return always firstname.If u have any doubt put this qry in ur qry analyzer and check.what is the order of the columnEver with Bright kathir
kathir_brighta at 2007-7-12 21:58:37 > top of Java-index,Java Essentials,Java Programming...
# 8
yea thx
javanewbie80a at 2007-7-12 21:58:37 > top of Java-index,Java Essentials,Java Programming...
# 9
HINTuse rs.getString(i, "field_name")
pbulgarellia at 2007-7-12 21:58:37 > top of Java-index,Java Essentials,Java Programming...
# 10
ANOTHER HINTdon't use select * from xxx, always enumerate the columns you need.
dwga at 2007-7-12 21:58:37 > top of Java-index,Java Essentials,Java Programming...
# 11
> HINT> use rs.getString(i, "field_name")Err, what?In the ResultSet doc on http://java.sun.com/j2se/1.5.0/docs/api/index.html, I can find getString(int columnIndex) and getString(String columnName), but no two-argument getString().
OleVVa at 2007-7-12 21:58:37 > top of Java-index,Java Essentials,Java Programming...
# 12

Okay, since there seems to be a lot of guessing, misleading information and other general chaos, I'll put my 2 eurocents in it (which should be more valuable than some of the comments here).

First of all, I'd be very surprised if there was a performance hit between getXXX(int) and getXXX(String). At least compared to the actual performance hit that you get from hitting the database.

Number b) it's a lot more readable to use the getXXX(String) methods and indeed it doesn't break the program if a column gets added (then again, that shouldn't happen very often in a properly designed system).

3. Designing and using databases is tricky business, for your homegrown starter applications you're bound to make some weird "solutions" (like having a denormalized database thus no reference integrity).

You'll learn in time.

-Kayaman-a at 2007-7-12 21:58:37 > top of Java-index,Java Essentials,Java Programming...