File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Sorting Data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Sorting Data" Watch "Sorting Data" New topic

Sorting Data

zohreh talebi
Ranch Hand

Joined: Sep 01, 2006
Posts: 41
Hi everyone,
I want to sort my database...I know this syntax is true : "SELECT * FROM song ORDER BY nameOfTheSong"

because when I want to sort my data in MySQL it works, but when I call this method it doesn't work. May be I have

used a wrong object or method in my code ... I'm not sure.

public void sortByNameOfTheSong()
Statement stmt = connect.createStatement(ResultSet.TYPE_SCROLL_INSEVSITIVE,
stmt.executeQuery("SELECT * FROM song ORDER BY nameOfTheSong");
catch(SQLException sq)
{"The table could not be sorted");

Thanks in advance,
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Did you log the exception to see what it said?

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3753

Change the error message to sq.printStackTrace() to see the error. My guess is the error has to do with the query, not the fact that you are trying to sort.

Btw, performing a sorting query DOES NOT sort the database, it just returns you a sorted set of data. Everytime the query is called the database sorting operation is re-performed. You should look into adding an index to the column so that the data can be accessed consistently in a sorted order. This would make calls to retrieve data on this index much (possibly 10000 times) faster.

[OCA 8 Book] [Blog]
Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226
Do you even have an exception? This method could work and you would never know, as you don't do anything with the results of the query. As someone else said that query doesn't permanently sort the table by that column.
zohreh talebi
Ranch Hand

Joined: Sep 01, 2006
Posts: 41
Dear freinds,

thanks for your anwsers. I think understand my incurrect point.
I had sorted the resultset but it didn't relate to database or my table.Am I true?

I agree. Here's the link:
subject: Sorting Data
It's not a secret anymore!