TxQueryRunner????????????
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TxQueryRunner????????????相关的知识,希望对你有一定的参考价值。
?????????utils ?????? als ??????????????? throws ons ?????? param system
TxQueryRunner??????QueryRunner????????????????????????commons-dbutils.jar
* ????????????QueryRunner?????????
* ?????????????????????!??????????????????JdbcUtils??????????????????
??????jdbc??????
* QueryRunner??????????????????
* update() -->insert???update???delete
* query() -->select
* batch() -->?????????
@Test
public void testUpdate() throws SQLException
{
String sql ="insert into t_admin(adminId,adminname,adminpwd) values(?,?,?)";
Object[] params={"1","zouzou","123"};//???sql????????????????????????
QueryRunner qr=new TxQueryRunner();//????????????????????????????????????
qr.update(sql, params);//??????sql???????????????????????????????????????JdbcUtils???????????????
}
??????????????????
@Test
public void testUpdate() throws Exception
{
try{
JdbcUtils.beginTransaction();//????????????
//????????????
String sql="insert into t_admin(adminId,adminname,adminpwd) values(?,?,?)";
QueryRunner qr=new TxQueryRunner();
Object[] params={"2","zouzou1","123"};
qr.update(sql, params);//??????SQL??????
if(false)
{
throw new Exception();
}
params=new Object[]{"3","zouzou2","123"};
qr.update(sql, params);//??????SQL??????
JdbcUtils.commitTransaction();//????????????
}
catch(Exception e){
try{
JdbcUtils.rollbackTransaction();//???????????? ???????????????????????????????????????????????????
} catch (SQLException e1)
{
}
throw e;
}
}
??????????????????
* ????????????JDBC??????????????????ResultSet
* ???QueryRunner????????????????????????ResultSet??????????????????.
* * QueryRunner??????????????????select,??????ResultSet
* * ?????????ResultSet???????????????????????????
* ?????????????????????
* * javaBean:?????????????????????javaBean???
* * Map:?????????????????????Map???
* * ?????????????????????Object?????????????????????????????????
//????????????????????????javaBean???
@Test
public void testQuery1() throws SQLException
{
String sql="select *from t_admin where adminId=?";
QueryRunner qr=new TxQueryRunner();
/**
* ????????????????????????ResultSetHandler???????????????????????????????????????????????????.
*
* BeanHandler --> ??????ResultSetHandler???????????????????????????????????????????????????Person?????????
*/
Person p=qr.query(sql,new BeanHandler<Person>(Person.class),"1");//1???????????????
System.out.println(p);
}
/**
* ??????BeanListHandler
* ???????????????????????????List<Bean>,?????????JavaBean?????????
* ?????????????????????????????????javaBean????????????????????????List<Bean>
* @throws SQLException
*/
@Test
public void testQuery2() throws SQLException
{
String sql="select * from t_admin";
QueryRunner qr=new TxQueryRunner();
/**
* ????????????????????????ResultSetHandler,???????????????????????????????????????????????????
*
* BeanListHandler -->??????ResultSetHandler????????????
* ????????????????????????????????????List<Person>?????????
*/
List<Person> list=qr.query(sql, new BeanListHandler<Person>(Person.class));
System.out.println(list);
}
/**
* ??????MapHandler,???????????????????????????Map?????????
* @throws SQLException
*/
@Test
public void testQuery3() throws SQLException
{
String sql="select * from t_admin where adminId=?";
QueryRunner qr=new TxQueryRunner();
/**
* ????????????????????????ResultSetHandler,???????????????????????????????????????????????????
*
* BeanListHandler -->??????ResultSetHandler???????????????
* ????????????????????????????????????List<Person>?????????
*/
Map<String,Object> map=qr.query(sql, new MapHandler(),"1");
System.out.println(map);
}
/**
* ??????MapListHandler,???????????????????????????List<Map>?????????,?????????Map
* ??????????????????Map,????????????List<Map>
* @throws SQLException
*/
@Test
public void testQuery4() throws SQLException
{
String sql="select * from t_admin";
QueryRunner qr=new TxQueryRunner();
/**
* ????????????????????????ResultSetHandler,???????????????????????????????????????????????????
*
* BeanListHandler --> ??????ResultSetHandler???????????????
* ????????????????????????????????????List<Person>?????????
*/
List<Map<String,Object>> mapList=qr.query(sql, new MapListHandler());
System.out.println(mapList);
}
/**
* ??????ScalarHandler,????????????????????????????????????Object???
* @throws SQLException
*/
@Test
public void testQuery5() throws SQLException
{
String sql="select count(*) from t_admin";//???????????????????????????
QueryRunner qr=new TxQueryRunner();
Object obj=qr.query(sql, new ScalarHandler());
/**
* ????????????select count(1),???????????????????????????
* > Integer
* > Long
* > BigInteger
*
* ?????????????????????????????????
* ?????????????????????????????????Number?????????????????????Number???????????????
*/
Number number =(Number)obj;
int cnt=number.intValue();
System.out.println(cnt);
}
/**
* ??????????????????????????????????????????
* ??????MapHandler?????????
* 1. ?????????????????????map???
* 2. ??????map??????Person??????
* 3. ??????map??????Zou??????
* 4. ?????????????????????????????????
* @throws SQLException
*/
@Test
public void testQuery6() throws SQLException
{
String sql="select * from t_admin p,t_zou a where p.adminId=a.adminId and p.adminId=?";//???????????????????????????
QueryRunner qr=new TxQueryRunner();
/**
* 1.??????Map
*/
Map map=qr.query(sql, new MapHandler(),"1");
/**
* 2.???Map????????????????????????Person???
*/
Person p=CommonUtils.toBean(map, Person.class);
/**
* 3.???Map????????????????????????Zou???
*/
Zou z=CommonUtils.toBean(map, Zou.class);
/**
* 4. ???????????????????????????
*/
p.setZou(z);
System.out.println(p);
}
以上是关于TxQueryRunner????????????的主要内容,如果未能解决你的问题,请参考以下文章