java視頻教程之ResultSetHandler常用結(jié)果集封裝分享
//ArrayHandler:適合取1條記錄。把該條記錄的每列值封裝到一個(gè)數(shù)組中Object[]
//ArrayListHandler:適合取多條記錄。把每條記錄的每列值封裝到一個(gè)數(shù)組中Object[],把數(shù)組封裝到一個(gè)List中
//ColumnListHandler:取某一列的數(shù)據(jù)。封裝到List中。
//KeyedHandler:取多條記錄,每一條記錄封裝到一個(gè)Map中,再把這個(gè)Map封裝到另外一個(gè)Map中,key為指定的字段值。
//MapHandler:適合取1條記錄。把當(dāng)前記錄的列名和列值放到一個(gè)Map中
//MapListHandler:適合取多條記錄。把每條記錄封裝到一個(gè)Map中,再把Map封裝到List
//ScalarHandler:適合取單行單列數(shù)據(jù)
BeanHandler:把每條記錄封裝成對(duì)象,適合取一條記錄
BeanListHandler把每條記錄封裝成對(duì)象,把對(duì)象存儲(chǔ)到List集合中,適合取多條記錄
接下來扣丁學(xué)堂小編針對(duì)Java在線學(xué)習(xí)之ResultSetHandler常用結(jié)果集封裝案例分析,完成針對(duì)結(jié)果集的封裝。
案例代碼
1.MySQL數(shù)據(jù)庫(kù)創(chuàng)建圖書信息表
2.創(chuàng)建Book.javajavabean類-->略
3.建立CSP0連接池-->略
4.根據(jù)需求,使用指定的結(jié)果集獲取數(shù)據(jù)結(jié)果
首先我們先介紹最常用的結(jié)果集:
importjava.sql.ResultSet; importjava.sql.SQLException; importjava.util.ArrayList; importjava.util.List; importorg.apache.commons.dbutils.QueryRunner; importorg.apache.commons.dbutils.ResultSetHandler; importorg.apache.commons.dbutils.handlers.BeanHandler; importorg.apache.commons.dbutils.handlers.BeanListHandler; importorg.junit.Test; importcom.qf.bean.Book; importcom.qf.utils.C3P0Utils; publicclassDemo_Query{ //1.BeanHandler把每條記錄封裝成對(duì)象適合取一條記錄 @Test publicvoidtestQuery2(){ QueryRunnerqr=newQueryRunner(C3P0Utils.getDataSource()); try{ Bookb= qr.query("select*frombookwhereid=?",newBeanHandler<Book>(Book.class),1); System.out.println(b); }catch(SQLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } //2.BeanListHandler把每條記錄封裝成對(duì)象將對(duì)象存儲(chǔ)到List集合中 @Test publicvoidtestQuery3(){ QueryRunnerqr=newQueryRunner(C3P0Utils.getDataSource()); try{ List<Book>list= qr.query("select*frombookwhereid=?orid=?",newBeanListHandler<Book>(Book.class),2,9); System.out.println(list); }catch(SQLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } //3.ArrayHandler適合取一條記錄把該記錄的每一列值存儲(chǔ)到一個(gè)數(shù)組中Object[] @Test publicvoidtestQuery3()throwsSQLException{ QueryRunnerqr=newQueryRunner(C3P0Utils.getDataSource()); Object[]arr=qr.query("select*frombookwhereid=1",newArrayHandler()); for(Objectobj:arr){ System.out.println(obj); } } //4.ArrayListHandler適合取多條記錄把該記錄的每一列值存儲(chǔ)到一個(gè)數(shù)組中Object[]然后把數(shù)組封裝到集合中 @Test publicvoidtestQuery4()throwsSQLException{ QueryRunnerqr=newQueryRunner(C3P0Utils.getDataSource()); List<Object[]>list=qr.query("select*frombook",newArrayListHandler()); for(Object[]obj:list){ for(Objecto:obj){ System.out.print(o+","); } System.out.println(); } } //5.ColumnListHandler獲取某一列的數(shù)據(jù)封裝到List集合中 @Test publicvoidtestQuery5()throwsSQLException{ QueryRunnerqr=newQueryRunner(C3P0Utils.getDataSource()); List<Object>list=qr.query("select*frombook",newColumnListHandler(2));//可以根據(jù)列號(hào)從1開始/列名 for(Objectobj:list){ System.out.println(obj); } } //6.MapHandler適合取一條記錄把當(dāng)前記錄的列名和列值放到一個(gè)Map中 @Test publicvoidtestQuery6()throwsSQLException{ QueryRunnerqr=newQueryRunner(C3P0Utils.getDataSource()); Map<String,Object>map=qr.query("select*frombookwhereid=1",newMapHandler()); for(Map.Entry<String,Object>entry:map.entrySet()){ System.out.println(entry.getKey()+"::"+entry.getValue()); } } //7.KeyedHandler取多條記錄 //每條記錄封裝到Map中再把Map封裝到另一個(gè)Map中 //Map<key,Map<key,value>> //內(nèi)Map:key字段名value:字段值 //外Map:key指定字段的值value:內(nèi)Map //key為指定的字段值 @Test publicvoidtestQuery7()throwsSQLException{ QueryRunnerqr=newQueryRunner(C3P0Utils.getDataSource()); Map<Object,Map<String,Object>>map=qr.query("select*frombook",newKeyedHandler("id")); for(Map.Entry<Object,Map<String,Object>>entry:map.entrySet()){ System.out.println(entry.getKey()); for(Map.Entry<String,Object>e:entry.getValue().entrySet()){ System.out.println(e.getKey()+";;;;"+e.getValue()); } System.out.println("-------------"); } } //8.MapListHandler適合取多條記錄把當(dāng)前記錄封裝到Map中再把Map封裝到List中 @Test publicvoidtestQuery8()throwsSQLException{ QueryRunnerqr=newQueryRunner(C3P0Utils.getDataSource()); List<Map<String,Object>>list=qr.query("select*frombook",newMapListHandler()); for(Map<String,Object>map:list){ for(Stringkey:map.keySet()){ System.out.println(key+";;;;"+map.get(key)); } System.out.println("--------------"); } } //9.ScalarHandler適合取單行單列數(shù)據(jù) @Test publicvoidtestQuery9()throwsSQLException{ QueryRunnerqr=newQueryRunner(C3P0Utils.getDataSource()); Objectobj=qr.query("selectcount(*)frombook",newScalarHandler()); System.out.println(obj); } } 以上代碼是針對(duì)查詢,給出的對(duì)應(yīng)操作。當(dāng)然,我們實(shí)際工作中不只有查詢,還會(huì)涉及到一些增刪改以及批量處理的功能。這時(shí)候我們就要使用QueryRunner類中的另外兩個(gè)方法來完成了。他們分別是: update()用于執(zhí)行insertupdatedelete batch()批處理可執(zhí)行多條語句批量 QueryRunnerupdate()方法的使用案例代碼 packagecom.qf.dbutils; importjava.sql.SQLException; importorg.apache.commons.dbutils.QueryRunner; importorg.junit.Test; importcom.qf.utils.C3P0Utils; //update() publicclassDemo_Update{ @Test publicvoidtestInsert01()throwsSQLException{ //1.獲取QueryRunner對(duì)象 QueryRunnerqr=newQueryRunner(C3P0Utils.getDataSource()); Stringsql="insertintobook(name,price,num)values('html',10,12)"; qr.update(sql); } @Test publicvoidtestInsert02()throwsSQLException{ //1.獲取QueryRunner對(duì)象 QueryRunnerqr=newQueryRunner(C3P0Utils.getDataSource()); Stringsql="insertintobook(name,price,num)values(?,?,?)"; qr.update(sql,"photoshop",23,6); } @Test publicvoidtestUpdate(){ QueryRunnerqr=newQueryRunner(C3P0Utils.getDataSource()); Stringsql="updatebooksetprice=?whereid=1"; try{ qr.update(sql,80); }catch(SQLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } @Test publicvoidtestDelete()throwsSQLException{ //1.獲取QueryRunner對(duì)象 QueryRunnerqr=newQueryRunner(C3P0Utils.getDataSource()); Stringsql="deletefrombookwhereid=13"; qr.update(sql); } @Test publicvoidtestDelete2()throwsSQLException{ //1.獲取QueryRunner對(duì)象 QueryRunnerqr=newQueryRunner(C3P0Utils.getDataSource()); Stringsql="deletefrombookwherenamelike?"; qr.update(sql,"%java%"); } } QueryRunnerbatch()方法的使用案例代碼 packagecom.qf.dbutils; importjava.sql.SQLException; importjava.util.Arrays; importorg.apache.commons.dbutils.QueryRunner; importorg.junit.Test; importcom.qf.utils.C3P0Utils; publicclassDemo_Batch{ /* *batch()方法是批量處理 *所需要的參數(shù)包括一個(gè)二維數(shù)組 *Object[][]params=newObject[5][];高維的數(shù)5決定是執(zhí)行sql語句的次數(shù) *低維數(shù)組的元素就是給sql語句?賦值的 **/ @Test publicvoidtestBatchInsert()throwsSQLException{ QueryRunnerqr=newQueryRunner(C3P0Utils.getDataSource()); //batch方法第二個(gè)參數(shù)是一個(gè)二維數(shù)組所以我們先創(chuàng)建一個(gè)二維數(shù)組 Object[][]params=newObject[5][]; //給二維數(shù)組中每一個(gè)一維數(shù)組的元素進(jìn)行賦值 for(inti=0;i<params.length;i++){ params[i]=newObject[]{"html",22,39}; } int[]arr=qr.batch("insertintobook(name,price,num)values(?,?,?)",params); System.out.println(Arrays.toString(arr)); } @Test publicvoidtestBatchDelete()throwsSQLException{ QueryRunnerqr=newQueryRunner(C3P0Utils.getDataSource()); Object[][]params=newObject[3][]; for(inti=0;i<params.length;i++){ params[i]=newObject[]{i}; } qr.batch("deletefrombookwhereid=?",params); } }
以上就是關(guān)于扣丁學(xué)堂java在線學(xué)習(xí)之ResultSetHandler常用結(jié)果集封裝的詳細(xì)介紹,希望對(duì)小伙伴們有所幫助,想要了解更多內(nèi)容的小伙伴可以登錄扣丁學(xué)堂官網(wǎng)查詢??鄱W(xué)堂是專業(yè)的Java培訓(xùn)機(jī)構(gòu),不僅有專業(yè)的老師和與時(shí)俱進(jìn)的課程體系,還有大量的Java視頻教程供學(xué)員掛看學(xué)習(xí)哦??鄱W(xué)堂java技術(shù)交流群:487098661。微信號(hào):codingbb
*博客內(nèi)容為網(wǎng)友個(gè)人發(fā)布,僅代表博主個(gè)人觀點(diǎn),如有侵權(quán)請(qǐng)聯(lián)系工作人員刪除。