ResultSet brings unordered results on SELECT * query

Hello,

I am querying Oracle tables and I would like the resultSet will bring the columns at the same order they were defined at the database.

I must use SELECT * on these tables for internal reasons and cannot specify the columns names instead.

Now, I am using the following standard code for that:

String query = "SELECT * FROM MYTABLE";

Connection conn = ds.getConnection();

stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery(query);

The problem is that if I go over the ResultSet object, like this:

for (int i=1 ; i<=rs.getMetaData().getColumnCount(); i++)

{

String col= rs.getMetaData().getColumnName(i);

System.out.println(col);

}

I don't get the columns in the same order as defined in the database.

Any idea why and how can I force it to?

[859 byte] By [Roy.Ca] at [2007-11-27 10:56:41]
# 1

select column1, column2, column3 from ....

maybe?

Select * is not normally considered a good practice. Especially when your code then depends on the columns being returned in a specific order. A dbadmin makes a change and your code is screwed.

masijade.a at 2007-7-29 12:04:28 > top of Java-index,Java Essentials,Java Programming...
# 2

Hi,

As I mentioned above: I MUST use SELECT * on these tables for internal reasons and cannot specify the columns names instead. I am getting these tables on runtime and I don't know the columns names ahead.

In this case no dbadmin will ever change these tables.

Roy

Message was edited by:

Roy.C

Roy.Ca at 2007-7-29 12:04:28 > top of Java-index,Java Essentials,Java Programming...
# 3

Then use

rs.getString("columnName");

rather than

rs.getString(index);

masijade.a at 2007-7-29 12:04:28 > top of Java-index,Java Essentials,Java Programming...
# 4

Again, I don't know the columnName when I do that.

Isn't there any way to force * to bring the columns in the same order as in the DB itself?

Roy

Roy.Ca at 2007-7-29 12:04:28 > top of Java-index,Java Essentials,Java Programming...
# 5

Don't know. Never needed it and should never need it, and wouldn't rely on it if I found one. Read the API, and ask the DB company about any Driver specific stuff.

But, it sounds to me, as though a minor redesign is in order.

masijade.a at 2007-7-29 12:04:28 > top of Java-index,Java Essentials,Java Programming...
# 6

I agree with what you're saying but bare in mind that I am trying to adapt myself to a certain design that was like that before I arrived. It can't be changed although I agree it should.

Roy.Ca at 2007-7-29 12:04:28 > top of Java-index,Java Essentials,Java Programming...
# 7

You could get meta data from the result set, which will tell you the order of column names and their data types.

If you don't know about the table you're reading, how can the column order bother you?

malcolmmca at 2007-7-29 12:04:28 > top of Java-index,Java Essentials,Java Programming...
# 8

I need to exports these results to excel and it get uploaded to a different DB but it has to have the same order.

But Never mind, now I see I do get the same order it's just the SAP GUI that presents these Oracle fields differently.

Thanks guys,

Roy

Roy.Ca at 2007-7-29 12:04:28 > top of Java-index,Java Essentials,Java Programming...