英文:
Retrieving data from sql in java class
问题
SELECT REQUEST_ID, LOGIN_USER, PRICE, STATUS FROM TABLE ORDER BY REQUEST_ID DESC
JAVA CODE:
public class DBConnection {
public HashMap<Object, List<Dashboard>> getStoreResult() {
ArrayList<Dashboard> dashRec = new ArrayList<Dashboard>();
HashMap<Object, List<Dashboard>> map = new HashMap<>();
try {
Class.forName("");
Connection con = DriverManager.getConnection("");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT REQUEST_ID, LOGIN_USER, PRICE, STATUS FROM TABLE ORDER BY REQUEST_ID DESC");
while (rs.next()) {
}
return map;
}
我在Java中使用SQL查询来检索数据,并将其存储在HashMap中。HashMap的键是一个包含请求ID、用户名、一组中的最低状态的对象。这里的“组”是指具有相同ID的所有行。例如,请求ID为123的情况下,有7行数据,所以最低状态号为1。在HashMap的值中,我使用ArrayList,其中包含特定请求ID的所有行对象。当我在SQL中执行查询时,根据查询中是否更改了"ORDER BY REQUEST_ID DESC" 或 "ORDER BY REQUEST_ID ASC",结果会有所变化。但是,当我在Java类中执行相同操作时,无论是"ORDER BY REQUEST_ID DESC"、"ORDER BY REQUEST_ID ASC" 还是 "ORDER BY REQUEST_ID",所有三种情况的结果都保持不变。这个问题的原因是什么?
英文:
SELECT REQUEST_ID,LOGIN_USER,PRICE,STATUS from TABLE ORDER BY REQUEST_ID DESC
JAVA CODE:
public class DBConnection {
public HashMap<Object, List<Dashboard>> getStoreResult() {
ArrayList<Dashboard> dashRec=new ArrayList<Dashboard>();
HashMap<Object, List<Dashboard>> map = new HashMap<>();
try{
Class.forName("");
Connection con=DriverManager.getConnection("");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("SELECT REQUEST_ID,LOGIN_USER,PRICE,STATUS from TABLE ORDER BY REQUEST_ID DESC ");
while (rs.next()) {
}
return map;
I am using sql query to retrieve data in hashmap in java.The hashmap has key as object which consists of reqid,name,lowest status of a set.Here set refers to all rows of a unique id.For instance,reqid 123 has 7 rows ,so lowest status no is 1.In hashmap value,I have arraylist which has all rows as objects of a particuler reqid.When I execute the query in sql, the results are as my changes in query.I mean changes take place in Order by reqid desc or Order by reqid asc.But when I do the same in java class the results for all three remains same i.e. Order by reqid desc or Order by reqid asc or Order by reqid
.What can be the cause of the error?
答案1
得分: 1
我不确定尝试这种方式,LinkedHashMap 会保持顺序。
LinkedHashMap<Object,List<Dashboard>> map = new LinkedHashMap<>();
英文:
I am not sure try to this way LinkedHashMap takes the order.
LinkedHashMap<Object, List<Dashboard>> map = new LinkedHashMap<>();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论