Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ORDER BY query in MySQL database

 
Adrian Cordoba
Greenhorn
Posts: 26
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, there!
For two days, I'm struggling with this problem:
I'm developing a Java application.
The program shows an ordered list of objects in a Swing JList. These objects are obtained with a SQL query (SELECT.....ORDER BY...) to a MySQL database.
The user can add an object. This object is inserted on database by mean of "INSERT INTO....".
After the object is inserted in database, the JList is cleared, and the same "SELECT....ORDER BY...." query is executed in order to populate the JList again.
But the added object appears at the end of the list, instead it appear in order.
After I restart the application, the added object appears in correct order.
I can't imagine why this happens.
I don't know SQL and MySQL very well. May be I'm omitting some statements for persists the changes in database... I don't know.

Can sombody help me with this problem?

Thank you, in advance.
 
Campbell Ritchie
Sheriff
Posts: 51447
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you run that query from the MySQL command line? Have a look at the Java® Tutorials about databases.
 
Paul Clapham
Sheriff
Posts: 21581
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest doing some debugging to observe what order the ORDER-BY query returns rows in. My wild guess, substantiated by looking at no code, is that you are populating the JList from something other than the database query the second time.
 
Adrian Cordoba
Greenhorn
Posts: 26
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, for reply.
I use the same method to populate the JList from database in both cases: before and after the insertion.
I could make some debug:
I just print the order of the objects from the ResultSet I got from stored procedure: after the insertion, the added object appears in the last position.
But I executed the stored procedure from a console, and the object is in the correct position.

May be I have a remark: I'm using the same SQL Connection to execute both stored procedures: insertion and select.
Could this be a problem?
 
Paul Clapham
Sheriff
Posts: 21581
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adrian Cordoba wrote:I just print the order of the objects from the ResultSet I got from stored procedure: after the insertion, the added object appears in the last position.


A stored procedure??? Your original post said you were using an SQL query. Evidently it's actually more complicated than that. Pity you didn't mention the difference originally.

I'm assuming now (based on seeing no code at all) that the stored procedure does more than just an SQL query. (Otherwise why would you need a stored procedure at all?) So consider the difference.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic