Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

ORDER BY query in MySQL database

 
Greenhorn
Posts: 27
Oracle Java Linux
  • 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.
 
Marshal
Posts: 69856
278
  • 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.
 
Marshal
Posts: 25682
69
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: 27
Oracle Java Linux
  • 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
Marshal
Posts: 25682
69
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.
    Bookmark Topic Watch Topic
  • New Topic