JDBC 封裝ResultSet返回List
阿新 • • 發佈:2018-11-08
package top.cglzwz.Test1; import java.sql.ResultSet; import java.util.List; import java.util.Map; /** * * ResultSet返回List * * @author chgl16 * @Date 2018.10.11 * */ public interface ResultSetToListService { /** * SELECT * FROM websites * 查詢所有記錄,以List返回 * list物件的每一個元素都是一條記錄 * 每條記錄儲存在Map<String, Object>裡面,String型別指欄位名字,Object對應欄位值 * * @param rs * @return List<Map<String, Object>> */ public List<Map<String, Object>> selectAll(ResultSet rs); }
package top.cglzwz.Test1; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; /** * * ResultSet返回List * * @author chgl16 * @Date 2018.10.11 * */ public class ResultSetToListServiceImp implements ResultSetToListService { /** * SELECT * FROM websites * 查詢所有記錄,以List返回 * list物件的每一個元素都是一條記錄 * 每條記錄儲存在Map<String, Object>裡面,String型別指欄位名字,Object對應欄位值 * * @param rs * @return List<Map<String, Object>> */ public List<Map<String, Object>> selectAll(ResultSet rs) { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); try { // 獲取結果集結構(元素據) ResultSetMetaData rmd = rs.getMetaData(); // 獲取欄位數(即每條記錄有多少個欄位) int columnCount = rmd.getColumnCount(); while (rs.next()) { // 儲存記錄中的每個<欄位名-欄位值> Map<String, Object> rowData = new HashMap<String, Object>(); for (int i = 1; i <= columnCount; ++i) { // <欄位名-欄位值> rowData.put(rmd.getColumnName(i), rs.getObject(i)); } // 獲取到了一條記錄,放入list list.add(rowData); } } catch (Exception ex) { ex.printStackTrace(); } return list; } }
package top.cglzwz.Test1; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.Iterator; import java.util.List; import java.util.Map; public class App1 { public static void main(String[] args) throws Exception { Connection cnn = null; Statement stm = null; ResultSet rs = null; String sql = " SELECT * FROM websites"; Class.forName("com.mysql.cj.jdbc.Driver");//載入資料庫驅動 cnn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test1?serverTimezone=UTC","root","123"); stm = cnn.createStatement(); rs = stm.executeQuery(sql); ResultSetToListService rstls = new ResultSetToListServiceImp(); List<Map<String, Object>> list = rstls.selectAll(rs); // 列印list看看 for (Map<String, Object> map : list) { // 每一條記錄被包含在Map裡面,迭代器列印 Iterator it = map.keySet().iterator(); while (it.hasNext()) { // key就是欄位名, value是欄位值 String key = (String) it.next(); System.out.print(String.format("%1$10s",map.get(key).toString()) + " | "); } System.out.println("\n----------------------------------------------------------"); } } }
USA | Google | 1 | 1 | https://www.google.cm/ |
----------------------------------------------------------
CN | 淘寶 | 13 | 2 | https://www.taobao.com/ |
----------------------------------------------------------
USA | 菜鳥教程 | 1000 | 3 | http://www.runoob.com |
----------------------------------------------------------
CN | 微博 | 20 | 4 | http://weibo.com/ |
----------------------------------------------------------
USA | Facebook | 3 | 5 | https://www.facebook.com/ |
----------------------------------------------------------
CN | 波波宇宙 | 13 | 7 | bobo.io |
----------------------------------------------------------
CN | 波波zwz | 13 | 10 | bobo.io |
----------------------------------------------------------