• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help in JDBC Simple Code execution

 
Shailendra Phadke
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I have created a my first database using Ms-Access having Student table. In that table I entered some records. Fields are RollNo, SName. I entered rollno's randomly (ex: 17,5,2,10,56,1,65,3). When I save and reopen the table, my table sorted in ASC form automatically.

But when i wrote the JDBC code to retreive my first record it is display that record which I entered as a first record in my Student table and that is ROllNo 17. But in my table RollNo 1 is of BOF position. I was thinking that RollNo 1 will displayed on my screen. Why this is happening i don't understand. [My program is working fine but I am not getting the output which I am expecting]

Please help me here.


Thanks
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In an RDBMS rows are not in a particular order unless you write your query so that is has an ORDER BY.
It's possible Access is adding one for you when you view it through Access.

If you want to be able to get your rows back in the order you entered them then give them an incremental ID and order by that when you query the table.
 
Shailendra Phadke
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm... TO execute the first Record I fired ORDER BY clause. But I am not satisfied with this solution. Whichever the record is on the BOF position that display on the screen.
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shailendra Phadke wrote:Hmm... TO execute the first Record I fired ORDER BY clause. But I am not satisfied with this solution. Whichever the record is on the BOF position that display on the screen.


What exactly do you want from the query?
If it's order of entry into the table then you need a column that holds some sort of id that you can then ORDER BY.
A database is well within its rights to reorder the physical order of the rows on the disk.
 
Shailendra Phadke
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, my code is working fine. What my question is if I write Select * from Students and then why it is returning "17" ROllNumbers record? How it understands that record I entered first. That is my question :-)
 
Paul Clapham
Sheriff
Posts: 21551
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shailendra Phadke wrote:What my question is if I write Select * from Students and then why it is returning "17" ROllNumbers record?


What's the "it" which is returning that strange string? Or was that supposed to describe something else? Your question is unclear. I can tell you that the query is going to return all the rows from the Students table; what roll numbers have to do with that I have no idea.

How it understands that record I entered first.


That's unclear too. What is the "it" which you are asking about? The database? The simple SQL query? Even so that sentence doesn't really mean anything.
 
Shailendra Phadke
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK letme clear.

When I create a table I enter the Record Something like this :

17 Amit
19 Raj
1 Ganesh


When I save & close the table and reopen the same table from Ms-Access RollNumber column automatically sorted in ASC order.

After it my table looks like :

1 Ganesh
17 Amit
19 Ganesh

When I write a code for retrieve the first record from the Students table (above) then it returns me "17" Roll Number Record

And I was expecting 1 Ganesh record. After that I use Oder By then it displays 1 Ganesg record.

But my question is if i simply write "Select * from Student" then also 1 Ganesh must display. How it is displaying 17 Amit record.

I hope I clear it.

Thanks


 
Paul Clapham
Sheriff
Posts: 21551
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you've been told twice now that if you don't provide an ORDER BY clause then there is no guarantee that a query will return records in any particular order. It seems to me you ought to pay attention to that. But...

When I write a code for retrieve the first record from the Students table (above) then it returns me "17" Roll Number Record


Let's see that code. Or first, you should remember (since you've been told twice) that the "first" record doesn't mean anything unless your query has an ORDER BY clause. So what's the ORDER BY clause you used to return student number 17 as the first record?
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shailendra Phadke wrote:When I save & close the table and reopen the same table from Ms-Access RollNumber column automatically sorted in ASC order.


That is something the front end of Access is doing.
The driver you are using is unlikely (and in fact doesn't) to do this automatically.

Shailendra Phadke wrote:
When I write a code for retrieve the first record from the Students table (above) then it returns me "17" Roll Number Record


There is no such thing as "the first record in a table". To be more precise, that first record is not fixed and can change by inserts into the table or any number of database tidying up routines that may or may not happen in the background.

What you want is the first record in a result set, which can only be guaranteed if you use an ORDER BY statement.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic