1

i have a program where i have to read data from excel file and store them in a database . I am using LinkedHashmap to read each cell as a string and store them. My excel file contains the data:

ID    Name        Salary
43    paulina     1000
5     christine   2349000
54    laura       12587458
49    jim         45878

In my code I have made the fields for the table by the first row of the excel file and then I have filled the table with the rest of the data using again LinkedHashMap. My question is how in this step I can store the data in the table not the way they are in the file but by ascending IDs. I search that i can do it with TreeMap but how exactly? Could anyone help me?

dedmar
  • 401
  • 3
  • 12
  • 22
  • What do you mean " which database"? – dedmar Apr 29 '13 at 10:55
  • This is what i mean by which database " i have to read data from excel file and store them in a database". See the last word. Is it oracle, sybase, access? – Lokesh Apr 29 '13 at 10:56
  • In a database that i have select in the beginning of my program when i make the connection! – dedmar Apr 29 '13 at 10:57
  • try using a TreeMap instead or create a TreeMap from your HashMap. – ChadNC Apr 29 '13 at 11:04
  • Could you help me how i will create a TreeMap from my LinkedHashMap? – dedmar Apr 29 '13 at 11:09
  • 1
    The answer to the question in this link should point you in the right direction. http://stackoverflow.com/questions/922528/how-to-sort-map-values-by-key-in-java – ChadNC Apr 29 '13 at 11:12

2 Answers2

0

If your question is how to sort for the database, then loki is right: it is unimportant. If your question is how to order it for the program... You can use TreeMap, it's a fast data structure. You need to either make the objects that go in the structure implement Comparable, or you need to construct your TreeMap with a Comparator. Read up on the official Java tutorial on the two - either is ok to use, it's just a slightly different style of programming.

djb
  • 1,635
  • 3
  • 26
  • 49
0

A characteristic of DBMSs (like MySQL, Oracle, etc) is that each row of data stands alone. There is no natural order in which rows are stored in a DBMS.

That is, when you retrieve the rows using a SELECT statement, the order in which they will be presented to you in your result set is formally unpredictable. If you want your rows presented in a predictable order, use an ORDER BY clause in your SELECT statement.

It happens that some DBMS technology presents the illusion that rows are stored in a particular order. But they make no promises about this, and the illusion can break at any time unless you use ORDER BY. I learned this the hard way when an application started malfunctioning when a table got to a certain size.

To order your rows before INSERTing them into MySQL is, therefore, a waste of your time. Keep your insertion software really simple. To order them when you get them out is trivially easy -- use ORDER BY.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • You are right and i know that i can do what i am trying to do with the commands in mysql! But my project is to do it programatically! That's why i am asking! – dedmar Apr 29 '13 at 11:57