Java encapsulates the database result set into map (Java obtains database fields)

Source: Internet
Author: User

This sectionCodeIs to encapsulate the result set into map, the field is key, record is value. Then add each map to the list.

If you have any suggestions or ideas, please leave me a message.

 

Import Java. SQL. connection; import Java. SQL. drivermanager; import Java. SQL. resultset; import Java. SQL. resultsetmetadata; import Java. SQL. sqlexception; import Java. SQL. statement; import Java. util. arraylist; import Java. util. hashmap; public class dbhelper {public static void main (string [] ARGs) throws classnotfoundexception, sqlexception {class. forname ("com. mySQL. JDBC. driver "); string url =" JDBC: mysql: // 127. 0.0.1: 3306/db123 "; string user =" root "; string Password =" root "; connection conn = drivermanager. getconnection (URL, user, password); statement stmt = Conn. createstatement ();/* For (char letter = 'a'; letter <= 'Z'; letter ++) {int id = letter-97; string name = ""; string sex = (ID & 1 )! = 0? "Male": "female"; string state = "Y"; string SQL = "insert into person (ID, name, sex, state) values (" + ID + ", "+ name +", "+ sex +", "+ state +") "cmdps.exe cute (SQL );} */resultset rs = stmt.exe cutequery ("select * From person P"); resultsetmetadata data Data = Rs. getmetadata (); arraylist 

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.