Java takes every piece of data in the database into an array or list

Source: Internet
Author: User

1, how to put the data in the database according to the row (that is, a whole piece of data) out, deposited into the array?

     Public StaticString str =NULL;//Convert stringbuffer to string     Public StaticStringBuffer SB =NewStringBuffer ();//StringBuffer Easy to delete and modify the string     Public Static voidMain (string[] args) {String className="Net.sourceforge.jtds.jdbc.Driver"; String URL="Jdbc:jtds:sqlserver://localhost:3306/test"; String Dbusername="Shujuku";//the name of the databaseString Password ="Mima";//Password//Load Driver        Try{class.forname (className); //Establish a connectionConnection Conn; Try{conn=drivermanager.getconnection (URL, dbusername, password); Statement Stat=conn.createstatement (); String SQL="SELECT * from Test";//read the data from the databaseResultSet rs =stat.executequery (SQL); //Remove data from resultset (if each data in the database has 7 columns of data)                 while(Rs.next ()) {Sb.append (rs.getstring (1));//read out the data for each columnSb.append ("*");//mark after each column of data to make it easier to split in the futureSb.append (Rs.getstring (2)); Sb.append ("*"); Sb.append (Rs.getstring (3)); Sb.append ("*"); Sb.append (Rs.getstring (4)); Sb.append ("*"); Sb.append (Rs.getstring (5)); Sb.append ("*"); Sb.append (Rs.getstring (6)); Sb.append ("*"); Sb.append (Rs.getstring (7)); Sb.append ("%");//mark after each piece of data for easy splitting} str= Sb.tostring ();//Convert data from StringBuffer type to string typeString datas; //divides the total number by the specified character into an array, one for each data groupstring[] params = stringutils.split (str, "%"); //By splitting each piece of data, the Param data holds every piece of data for a single piece of data                for(inti = 0; i < params.length; i++) {string[] param= Stringutils.split ( This. RawData, "*"); //To do what you want to do, I do the work for each data processing, and then into the database in another table}

2, how to save in the list?

 while (Rs.next ()) {        Map<string,object> map=new hashmap<string, object>();        Map.put ("id", rs.getstring (1));        Map.put ("ItemName", rs.getstring (2));        Map.put ("version", Rs.getstring (3));        Map.put ("Contents", Rs.getstring (4));        Map.put ("Account", rs.getstring (5));        Map.put ("PSW", rs.getstring (6));        Map.put ("Tytime", rs.getstring (7));        Listdatas.add (map);                }

So here's the question:

Q: If I save the data in the resultset to the list<map<>>, how can I get the data from the list?

A: You can first for the loop, take out the list of each piece of data (map<> form), put in a map, and then take the data in the map to be stored in the string array

For example:

for (int i=0;i<list.size (); i++) {Map dqmap = (map) list.get (i); String JQH = dqmap.get (' Jqh '); String khh = dqmap.get (' Khh ');}

Well, I can only help here, notice, this is my hand to change the code before the project, the privacy of the data removed, but did not run the code, it should be right, this is my project according to the requirements of their own thinking of the resultset treatment, as for the good is not known, for reference only.

Java takes every piece of data in the database into an array or list

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.