store data in table by ascending IDs

跟風遠走 提交于 2019-12-11 06:42:39

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/16276686/store-data-in-table-by-ascending-ids

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!