apache的dbUtils的使用
来源:互联网 发布:sql数据库 分组查询 编辑:程序博客网 时间:2024/05/29 11:32
1.引入相关的jar:
2.MapListHandler的使用,将结果集转换为List<Map<String,Object>>
@Beforepublic void init() throws SQLException, ClassNotFoundException{String driverClass="oracle.jdbc.driver.OracleDriver";String url="jdbc:oracle:thin:@192.168.233.134:1521:orcl";String username="cnmef";String password="cnmef";Class.forName(driverClass); connection=DriverManager.getConnection(url,username,password);}/** * 测试apache中dbUtils的mapListHandler的使用 * @throws SQLException */@Testpublic void MapListHandlerTest() throws SQLException{String sql="select " +"t1.id as \"id\","+" t1.nofficeid as nofficeid, "+" t1.ncurrencyid as \"ncurrencyid\", "+" t1.transaction_num as \"transactionNum\", "+" t1.interest_start as \"interestStart\", "+" t1.interest_end as \"interestEnd\","+" t1.margin_amount as \"marginAmount\", "+" t1.abstract as \"summary\","+" t1.ncontractid as \"ncontractId\","+" t1.in_account as \"inAccount\","+" t1.out_account as \"outAccount\" ,"+" t1.inputuserid as \"inputuserid\","+" t1.checkuserid as \"checkuserid\","+" t1.checkdate as \"checkdate\","+" (select t2.id from sett_subaccount t2 where t2.naccountid=t1.in_account and rownum=1 ) as \"subInAccountId\","+" (select t3.id from sett_subaccount t3 where t3.naccountid=t1.out_account and rownum=1 ) as \"subOutAccountId\","+" t1.margin_amount as \"marginAmount\" "+" from sett_depositbatchreview t1 " ;QueryRunner queryRunner=new QueryRunner();List<Map<String,Object>> map=queryRunner.query(connection, sql, new MapListHandler());for(Map<String,Object> temp:map){System.out.println(temp);}} @After public void close(){ try{ if(connection!=null){ connection.close(); } }catch(Exception e){ e.printStackTrace(); } }
3.BeanListHandler的使用,返回List<Bean>:
@Beforepublic void init() throws SQLException, ClassNotFoundException{String driverClass="oracle.jdbc.driver.OracleDriver";String url="jdbc:oracle:thin:@192.168.233.134:1521:orcl";String username="cnmef";String password="cnmef";Class.forName(driverClass); connection=DriverManager.getConnection(url,username,password);}/** * 测试apache中dbUtils的mapListHandler的使用 * @throws SQLException */ @Test public void BeanListHandlerTest() throws SQLException{ String sql="select " + "t1.id as \"id\","+ " t1.nofficeid as nofficeid, "+ " t1.ncurrencyid as \"ncurrencyid\", "+ " t1.transaction_num as \"transactionNum\", "+ " t1.interest_start as \"interestStart\", "+ " t1.interest_end as \"interestEnd\","+ " t1.margin_amount as \"marginAmount\", "+ " t1.abstract as \"summary\","+ " t1.ncontractid as \"ncontractId\","+ " t1.in_account as \"inAccount\","+ " t1.out_account as \"outAccount\" ,"+ " t1.inputuserid as \"inputuserid\","+ " t1.checkuserid as \"checkuserid\","+ " t1.checkdate as \"checkdate\","+ " (select t2.id from sett_subaccount t2 where t2.naccountid=t1.in_account and rownum=1 ) as \"subInAccountId\","+ " (select t3.id from sett_subaccount t3 where t3.naccountid=t1.out_account and rownum=1 ) as \"subOutAccountId\","+ " t1.margin_amount as \"marginAmount\" "+ " from sett_depositbatchreview t1 "; QueryRunner queryRunner=new QueryRunner(); List<MarginAccountVo> map=queryRunner.query(connection, sql, new BeanListHandler<MarginAccountVo>(MarginAccountVo.class)); for(MarginAccountVo temp:map){ System.out.println(temp); } } @After public void close(){ try{ if(connection!=null){ connection.close(); } }catch(Exception e){ e.printStackTrace(); } }
4.测试MapHandler:返回Map<String,Object>
@Beforepublic void init() throws SQLException, ClassNotFoundException{String driverClass="oracle.jdbc.driver.OracleDriver";String url="jdbc:oracle:thin:@192.168.233.134:1521:orcl";String username="cnmef";String password="cnmef";Class.forName(driverClass); connection=DriverManager.getConnection(url,username,password);}/** * 测试apache中dbUtils的mapListHandler的使用 * @throws SQLException */ @Test public void MapHandlerTest() throws SQLException{ String sql="select " + "t1.id as \"id\","+ " t1.nofficeid as nofficeid, "+ " t1.ncurrencyid as \"ncurrencyid\", "+ " t1.transaction_num as \"transactionNum\", "+ " t1.interest_start as \"interestStart\", "+ " t1.interest_end as \"interestEnd\","+ " t1.margin_amount as \"marginAmount\", "+ " t1.abstract as \"summary\","+ " t1.ncontractid as \"ncontractId\","+ " t1.in_account as \"inAccount\","+ " t1.out_account as \"outAccount\" ,"+ " t1.inputuserid as \"inputuserid\","+ " t1.checkuserid as \"checkuserid\","+ " t1.checkdate as \"checkdate\","+ " (select t2.id from sett_subaccount t2 where t2.naccountid=t1.in_account and rownum=1 ) as \"subInAccountId\","+ " (select t3.id from sett_subaccount t3 where t3.naccountid=t1.out_account and rownum=1 ) as \"subOutAccountId\","+ " t1.margin_amount as \"marginAmount\" "+ " from sett_depositbatchreview t1 where t1.id=? "; QueryRunner queryRunner=new QueryRunner(); /* * queryRunner.query(connection,sql,ResultSetHandler<T>,params); */ Map<String,Object> map=queryRunner.query(connection, sql, new MapHandler(),1); System.out.println(map); } @After public void close(){ try{ if(connection!=null){ connection.close(); } }catch(Exception e){ e.printStackTrace(); } }
5.测试BeanHandler:返回javaBean
@Beforepublic void init() throws SQLException, ClassNotFoundException{String driverClass="oracle.jdbc.driver.OracleDriver";String url="jdbc:oracle:thin:@192.168.233.134:1521:orcl";String username="cnmef";String password="cnmef";Class.forName(driverClass); connection=DriverManager.getConnection(url,username,password);}/** * 测试apache中dbUtils的mapListHandler的使用 * @throws SQLException */ @Test public void BeanHandlerTest() throws SQLException{ String sql="select " + "t1.id as \"id\","+ " t1.nofficeid as nofficeid, "+ " t1.ncurrencyid as \"ncurrencyid\", "+ " t1.transaction_num as \"transactionNum\", "+ " t1.interest_start as \"interestStart\", "+ " t1.interest_end as \"interestEnd\","+ " t1.margin_amount as \"marginAmount\", "+ " t1.abstract as \"summary\","+ " t1.ncontractid as \"ncontractId\","+ " t1.in_account as \"inAccount\","+ " t1.out_account as \"outAccount\" ,"+ " t1.inputuserid as \"inputuserid\","+ " t1.checkuserid as \"checkuserid\","+ " t1.checkdate as \"checkdate\","+ " (select t2.id from sett_subaccount t2 where t2.naccountid=t1.in_account and rownum=1 ) as \"subInAccountId\","+ " (select t3.id from sett_subaccount t3 where t3.naccountid=t1.out_account and rownum=1 ) as \"subOutAccountId\","+ " t1.margin_amount as \"marginAmount\" "+ " from sett_depositbatchreview t1 where t1.id=1 "; QueryRunner queryRunner=new QueryRunner(); MarginAccountVo bean=queryRunner.query(connection, sql, new BeanHandler<MarginAccountVo>(MarginAccountVo.class)); System.out.println(bean); } @After public void close(){ try{ if(connection!=null){ connection.close(); } }catch(Exception e){ e.printStackTrace(); } }
1 0
- apache--dbutils的使用
- apache的dbUtils的使用
- apache common-dbutils 的使用
- JDBC:Apache DBUtils的使用
- Apache的DBUtils使用详解
- Apache之DBUtils的使用
- Apache的DBUtils使用详解
- Apache的DBUtils框架学习
- Apache的DBUtils框架学习
- Apache的DBUtils框架学习
- Apache的DBUtils框架学习
- Apache的DBUtils框架学习
- Apache的DBUtils框架学习
- Apache的DBUtils框架学习
- Apache的DBUtils框架学习
- Apache的DbUtils组件学习
- Apache的DBUtils框架学习
- Apache的DBUtils框架学习
- 设置/修改centos上的swap交换分区的方法(转帖)
- WIN7如何设置默认登陆帐户,不输密码,实现自动登陆-转
- CentOS-Base.repo
- 3650M4机器安装redhat5.8
- 2008-11-10 12:48 为centos添加EPEL软件仓库
- apache的dbUtils的使用
- vnc
- linux查看CPU信息、机器型号等硬件信息
- 【KVM2】xauth:(stdin):1:bad display name \"xxx:1\" in \"add\" command解决
- php 遍历目录文件方法
- 记小问题:linux下mysql的表名默认区分大小写
- Android studio 中gson混淆,及注意的地方
- html5解决大文件断点续传
- Linux more 命令用法详解