The JDBC Connection database returns the key value of the result set

Source: Internet
Author: User

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)方法。

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.