This article describes the Java implementation method of encapsulating the result set into a List. Share it for your reference, as follows:
import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.List;public class TestResultSet { public static List query(){ Connection conn = new ConnectionUtil().openConnection(); try { Statement stmt = conn.createStatement(); String sql = "select id,name,email from customertbl"; ResultSet rs = stmt.executeQuery(sql); //Embroidize the result set into List List list = new ArrayList(); while(rs.next()){ // You can either refer to the column name or the column index int id = rs.getInt(1); String name = rs.getString("name"); String email = rs.getString("email"); System.out.println(id+":"+name+":"+email); Customer c = new Customer(); c.setId(id); c.setName(name); c.setEmail(email); //Storing the object in the list container list.add(c); } return list; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try { conn.close(); } catch (SQLException e) { conn = null; e.printStackTrace(); } } return null; }}I hope this article will be helpful to everyone's Java programming.