Generics ——— list<> in Java

Source: Internet
Author: User
Public list<bank> Getkhxxinfos (Connection con) {
  
  String sql = ' Select dh,dz,khbh,zjhm,xm from T_yw_khxxb ';
  list<bank> list = new arraylist<bank> ();
  
  try {
     PreparedStatement PS = con.preparestatement (sql);

     ResultSet rs = Ps.executequery ();
   
     while (Rs.next ()) {
       Bank BK = new Bank ();
    
       BK.SETDH (rs.getstring ("DH"));
       Bk.setdz (rs.getstring ("DZ"));
       Bk.setkubh (Rs.getint ("Kubh"));
       BK.SETXM (Rs.getstring ("XM"));
       BK.SETZJLX (rs.getstring ("ZJLX"));
    
       List.add (BK);
    }
  catch (SQLException e) {
      e.printstacktrace ();
    }
  
    return list;
}

List<?> what's in it. It depends on what type you have in the list. Just like the one above you. The data you query is placed in the Bank bean. Then put the bean in the list. Then if you want to use the generic type, then List<bank > banklist = new arraylist<bank> (); If I query now for string sql = "Select Zjhm from T_yw_khxxb", this query comes with a column. If this column is a character type . You can write list<string> list = new arraylist<string> (); List.add (rs.getstring ("ZJHM"));

The bank is a class that contains SETDH (), setd (), Setkubh (), SETXM (), SETZJLX () method, so you can invoke these methods when you instantiate the bank. And the list is stored in the bank type of lists, and list is a Java collection, it is an abstract interface, to achieve its arraylist,linkedlist and so on.

Generics are the types that are restricted during compilation. The list<bank> in your collection is the type of object you define for yourself, such as list<string> is a String list.

According to your program, it should be the bank object, generics, the simple point is to control the contents of your container, the advantage is that when you parse or traverse the container, do not need a strong transfer type, no boxing and unboxing process, efficiency than without the generic to quickly, code recognition is also easy to write.

Generic collection!! Specifies the type of list to store;

Program Analysis: This program is to query the database T_YW_KHXXB table operation;

1. Define the entity bank corresponding to the T_YW_KHXXB table each of these attributes corresponds to the field one by one in the T_YW_KHXXB table

2. Declare SQL query statement String sql = "Select Dh,dz,khbh,zjhm,xm from T_yw_khxxb";

3.list<bank> List = new arraylist<bank> (); Define a collection store Bank entity object

4.PreparedStatement PS = con.preparestatement (SQL); ResultSet rs = Ps.executequery (); Execute SQL statement and return ResultSet object Set (Result set)

5. Read the result set and save it in the List collection object, then return to the List collection object


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.