The JDBC Connection database query data returns the resultset result set at all times. Key value pairs are stored in the resultset. We can first get the key value, and then we get the value we want with the key value. So how do we get the key value? Look at the following code:
try {list<map<string, string>> List = new arraylist<map& Lt String, string>> (); rs = getstmt (). executeQuery (SQL); Get the structure information of result set resultset, such as field number, field name, etc. resultsetmetadata RSMD = Rs.getmetadata (); Gets the number of columns in the dataset int columncount = Rsmd.getcolumncount (); while (Rs.next ()) {map<string, string> Map = new hashmap<string, string> (); for (int i = 0; i < ColumnCount; i++) {String key = Rsmd.getcolumnlabel (i+1); String key = Rsmd.getcolumnname (i + 1) string value = Rs.getstring (key); Map.put (key, value); } list.add (map); } return list; } catch (Exception e) {e.printstacktrace (); return null; } finally {Colse (); }
For SQL Server and Oracle databases, we can useString key = rsmd.getColumnName(i + 1)来获取Key值,但是对于Mysql数据库,当我们使用聚合函数时如:select count(*) as stu_count from student,我们会发现这时取得的key值为空(‘‘),要想取得key值就需要使用String key = rsmd.getColumnLabel(i + 1)方法。