从Java类中检索SQL数据

huangapple go评论85阅读模式
英文:

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",所有三种情况的结果都保持不变。这个问题的原因是什么?

从Java类中检索SQL数据

英文:
SELECT REQUEST_ID,LOGIN_USER,PRICE,STATUS from TABLE ORDER BY REQUEST_ID DESC 

JAVA CODE:

public class DBConnection {
	public HashMap&lt;Object, List&lt;Dashboard&gt;&gt;  getStoreResult() {
		ArrayList&lt;Dashboard&gt; dashRec=new ArrayList&lt;Dashboard&gt;();
		HashMap&lt;Object, List&lt;Dashboard&gt;&gt; map = new HashMap&lt;&gt;();
		try{
	        Class.forName(&quot;&quot;);
			Connection con=DriverManager.getConnection(&quot;&quot;);
			Statement st=con.createStatement();
			ResultSet rs=st.executeQuery(&quot;SELECT REQUEST_ID,LOGIN_USER,PRICE,STATUS from TABLE ORDER BY REQUEST_ID DESC &quot;);
			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?

从Java类中检索SQL数据

答案1

得分: 1

我不确定尝试这种方式,LinkedHashMap 会保持顺序。

LinkedHashMap<Object,List<Dashboard>> map = new LinkedHashMap<>();

英文:

I am not sure try to this way LinkedHashMap takes the order.

LinkedHashMap&lt;Object, List&lt;Dashboard&gt;&gt; map = new LinkedHashMap&lt;&gt;();

huangapple
  • 本文由 发表于 2020年5月19日 19:15:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/61889693.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定