0

Reading this official documentation:

For columns that are NOT explicitly named in the query, it is best to use column numbers. If column names are used, the programmer should take care to guarantee that they uniquely refer to the intended columns, which can be assured with the SQL AS clause.

What is a column not explicitly named in the query? What do the they mean with that?

Thanks,

Indeed ItIs

3 Answers 3

1

Columns that are calculated/expressions have arbitrary names, eg

select max(salary) from employee 

if you examined the JDBC column name, it is usually called the same as the expression - ie in the rowset the column may be called literally "max(salary)", but it's database specific and unreliable.

You have two choices:

  1. Use the column number (one-based), eg rowset.get(1)
  2. Name the column in the query,

Ie

select max(salary) as max_salary from employee 

I much prefer the second option - it's much less brittle and easier to understand.

Sign up to request clarification or add additional context in comments.

3 Comments

what if it was SELECT ID, max(salary) from employee. Would it be guaranteed that max(salary would be the second row?
@IndeedItIs (you mean column) yes. The order of columns is exactly as per the query. It's OK to use numbers, but if someone adds a column to the query at the start, your code will break (you'll have to change all the column numbers in JDBC code), but names will not have to be altered. It's also easier to read the code.
cool Bohemian nice point, it's funny that the API I linked suggest to use numbers instead as it's more efficient. I would go wit your insight if I had to choose.
0

It's columns like count(*) or a function result like lower() that are not explicitly named in the query.

Comments

0

If you have

 select count(*) from employees // query 1 

Now you can not use rs.getString(columnLabel) unless you do

select count(*) as total Number from employees

 rs.getString("total Number") unless you do 

So it makes sense to use

 getString(int columnIndex) with query 1 instead of rs.getString(columnLabel) 

With above example below sentence makes complete sense

For columns that are NOT explicitly named in the query, it is best to use column numbers. If column names are used, the programmer should take care to guarantee that they uniquely refer to the intended columns, which can be assured with the SQL AS clause.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.