日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

jdbc結(jié)果集轉(zhuǎn)換成對象列表

系統(tǒng) 2249 0

將jdbc結(jié)果集轉(zhuǎn)換成對象列表
估計hibernate就是用得這種方式進行轉(zhuǎn)換的。
實體對象

Java代碼 復(fù)制代碼
  1. package ?test; ??
  2. //實體對象,該對象的屬性與數(shù)據(jù)庫中的字段相同,當(dāng)然可以改變具體看需求 ??
  3. public ? class ?Person?{ ??
  4. ???? private ? int ?id; ??
  5. ???? private ? int ?age; ??
  6. ???? private ?String?name; ??
  7. ???? public ? int ?getId()?{ ??
  8. ???????? return ?id; ??
  9. ????} ??
  10. ???? public ? void ?setId( int ?id)?{ ??
  11. ???????? this .id?=?id; ??
  12. ????} ??
  13. ???? public ? int ?getAge()?{ ??
  14. ???????? return ?age; ??
  15. ????} ??
  16. ???? public ? void ?setAge( int ?age)?{ ??
  17. ???????? this .age?=?age; ??
  18. ????} ??
  19. ???? public ?String?getName()?{ ??
  20. ???????? return ?name; ??
  21. ????} ??
  22. ???? public ? void ?setName(String?name)?{ ??
  23. ???????? this .name?=?name; ??
  24. ????} ??
  25. ???? ??
  26. }??

?

Java代碼 復(fù)制代碼
  1. package ?test; ??
  2. ??
  3. import ?java.lang.reflect.Field; ??
  4. import ?java.sql.Connection; ??
  5. import ?java.sql.PreparedStatement; ??
  6. import ?java.sql.ResultSet; ??
  7. import ?java.sql.SQLException; ??
  8. import ?java.util.List; ??
  9. ??
  10. public ? class ?Main?{ ??
  11. ???? //用于測試的方法 ??
  12. ???? public ? static ? void ?main(String[]?args)? throws ?InstantiationException,?IllegalAccessException,?IllegalArgumentException,?ClassNotFoundException?{ ??
  13. ????????Connection?conn?=?DbUtils.getConn(); ??
  14. ????????ResultSet?rs?=? null ; ??
  15. ????????PreparedStatement?psmt?=? null ; ??
  16. ????????System.out.println(conn); ??
  17. ???????? try ?{ ??
  18. ????????????psmt?=?conn.prepareStatement( "select?*?from?person" ); ??
  19. ????????????rs?=?psmt.executeQuery(); ??
  20. ????????????List?list?=?DbUtils.populate(rs,?Person. class ); ??
  21. ???????????? for ( int ?i?=? 0 ?;?i<list.size()?;?i++){ ??
  22. ????????????????Person?per?=?(Person)?list.get(i); ??
  23. ????????????????System.out.println( "person?:?id?=?" +per.getId()+ "?name?=?" +per.getName()+ "?age?=?" +per.getAge()); ??
  24. ????????????} ??
  25. ????????}? catch ?(SQLException?e)?{ ??
  26. ????????????e.printStackTrace(); ??
  27. ????????} finally { ??
  28. ???????????? if (rs!= null ){ ??
  29. ???????????????? try ?{ ??
  30. ????????????????????rs.close(); ??
  31. ????????????????}? catch ?(SQLException?e)?{ ??
  32. ????????????????????e.printStackTrace(); ??
  33. ????????????????} ??
  34. ????????????????rs= null ; ??
  35. ????????????} ??
  36. ???????????? if (psmt!= null ){ ??
  37. ???????????????? try ?{ ??
  38. ????????????????????psmt.close(); ??
  39. ????????????????}? catch ?(SQLException?e)?{ ??
  40. ????????????????????e.printStackTrace(); ??
  41. ????????????????} ??
  42. ????????????????psmt= null ; ??
  43. ????????????} ??
  44. ???????????? if (conn!= null ){ ??
  45. ???????????????? try ?{ ??
  46. ????????????????????conn.close(); ??
  47. ????????????????}? catch ?(SQLException?e)?{ ??
  48. ????????????????????e.printStackTrace(); ??
  49. ????????????????} ??
  50. ????????????????conn= null ; ??
  51. ????????????} ??
  52. ????????} ??
  53. ???????? ??
  54. ????} ??
  55. ??
  56. }??
Java代碼 復(fù)制代碼
  1. package ?test; ??
  2. ??
  3. import ?java.lang.reflect.Field; ??
  4. import ?java.sql.Connection; ??
  5. import ?java.sql.DriverManager; ??
  6. import ?java.sql.ResultSet; ??
  7. import ?java.sql.ResultSetMetaData; ??
  8. import ?java.sql.SQLException; ??
  9. import ?java.util.ArrayList; ??
  10. import ?java.util.List; ??
  11. ??
  12. public ? class ?DbUtils?{ ??
  13. ???? private ? static ?String?url?=? "jdbc:mysql://localhost:3306/test" ; ??
  14. ???? private ? static ?String?username?=? "root" ; ??
  15. ???? private ? static ?String?password?=? "" ; ??
  16. ???? private ? static ?String?driverClass?=? "com.mysql.jdbc.Driver" ; ??
  17. ???? //沒什么好說的,獲取數(shù)據(jù)庫連接 ??
  18. ???? public ? static ?Connection?getConn(){ ??
  19. ????????Connection?conn?=? null ; ??
  20. ???????? try ?{ ??
  21. ????????????Class.forName(driverClass); ??
  22. ????????????conn?=?DriverManager.getConnection(url,username,password); ??
  23. ????????}? catch ?(ClassNotFoundException?e)?{ ??
  24. ????????????e.printStackTrace(); ??
  25. ????????}? catch ?(SQLException?e)?{ ??
  26. ????????????e.printStackTrace(); ??
  27. ????????} ??
  28. ???????? ??
  29. ???????? return ?conn; ??
  30. ????} ??
  31. ???? /* ?
  32. ?????*?將rs結(jié)果轉(zhuǎn)換成對象列表 ?
  33. ?????*?@param?rs?jdbc結(jié)果集 ?
  34. ?????*?@param?clazz?對象的映射類 ?
  35. ?????*?return?封裝了對象的結(jié)果列表 ?
  36. ?????*/ ??
  37. ???? public ? static ?List?populate(ResultSet?rs?,?Class?clazz)? throws ?SQLException,?InstantiationException,?IllegalAccessException{ ??
  38. ???????? //結(jié)果集的元素對象? ??
  39. ????????ResultSetMetaData?rsmd?=?rs.getMetaData(); ??
  40. ???????? //獲取結(jié)果集的元素個數(shù) ??
  41. ????????? int ?colCount?=?rsmd.getColumnCount(); ??
  42. //???????System.out.println("#"); ??
  43. //???????for(int?i?=?1;i<=colCount;i++){ ??
  44. //???????????System.out.println(rsmd.getColumnName(i)); ??
  45. //???????????System.out.println(rsmd.getColumnClassName(i)); ??
  46. //???????????System.out.println(rsmd.getColumnClassLabel(i)); ??
  47. //???????} ??
  48. ????????? //返回結(jié)果的列表集合 ??
  49. ?????????List?list?=? new ?ArrayList(); ??
  50. ????????? //業(yè)務(wù)對象的屬性數(shù)組 ??
  51. ?????????Field[]?fields?=?clazz.getDeclaredFields(); ??
  52. ????????? while (rs.next()){ //對每一條記錄進行操作 ??
  53. ?????????????Object?obj?=?clazz.newInstance(); //構(gòu)造業(yè)務(wù)對象實體 ??
  54. ????????????? //將每一個字段取出進行賦值 ??
  55. ????????????? for ( int ?i?=? 1 ;i<=colCount;i++){ ??
  56. ?????????????????Object?value?=?rs.getObject(i); ??
  57. ????????????????? //尋找該列對應(yīng)的對象屬性 ??
  58. ????????????????? for ( int ?j= 0 ;j<fields.length;j++){ ??
  59. ?????????????????????Field?f?=?fields[j]; ??
  60. ????????????????????? //如果匹配進行賦值 ??
  61. ????????????????????? if (f.getName().equalsIgnoreCase(rsmd.getColumnName(i))){ ??
  62. ????????????????????????? boolean ?flag?=?f.isAccessible(); ??
  63. ?????????????????????????f.setAccessible( true ); ??
  64. ?????????????????????????f.set(obj,?value); ??
  65. ?????????????????????????f.setAccessible(flag); ??
  66. ?????????????????????} ??
  67. ?????????????????} ??
  68. ?????????????} ??
  69. ?????????????list.add(obj); ??
  70. ?????????} ??
  71. ???????? return ?list; ??
  72. ????} ??
  73. }??

jdbc結(jié)果集轉(zhuǎn)換成對象列表


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 扶风县| 三门峡市| 南昌市| 左贡县| 黑龙江省| 兖州市| 河间市| 固安县| 岳阳市| 衡东县| 东海县| 平山县| 大足县| 高安市| 高雄市| 兴安县| 迁安市| 遂川县| 旬邑县| 清原| 仙居县| 平利县| 武清区| 滨州市| 缙云县| 福州市| 英山县| 盐津县| 青海省| 平陆县| 镇沅| 永济市| 塘沽区| 西宁市| 虞城县| 瑞金市| 穆棱市| 于田县| 白河县| 交口县| 西平县|