The example in this article describes how the Java implementation encapsulates the result set into the list. Share to everyone for your reference, specific 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);
Encapsulate the result set into List List = new ArrayList ();
while (Rs.next ()) {//can also be indexed according to column name 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);
Store 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 help you with your Java programming.