dbutils开源项目用法

<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><!-- [if !mso]> <object classid="clsid:38481807-CA0E-42D2-BF39-B33AF135CC4D" id=ieooui> </object> <style> st1\:*{behavior:url(#ieooui) } </style> <![endif]--><!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]-->

dbutils 开源项目用法

 

 

dbutils 开源项目用法:

项目地址: http://commons.apache.org/dbutils/

最新版本为 1.1

英文文档中这样写到:

DbUtils is a very small library of classes so it won't take long to go through the javadocs for each class.

The core classes/interfaces in DbUtils are QueryRunner and ResultSetHandler.

You don't need to know about any other DbUtils classes to benefit from using the library.

大概意思是: DbUtils 是一个非常小的类库 因此你不需要花费太长的时间去研究 javadocs

它的核心接口是 QueryRunner ResultSetHandler

你不需要关心其他 DbUtils 的类如何使用

-------------------------------------------------------------------------------------------------------------------------

QueryRunner

构造 QueryRunner

QueryRunner()



QueryRunner(javax.sql.DataSource ds)



有两种方法来构造 QueryRunner 一种是无参数的 一种是有 DataSource 类型参数的构造方法

这里使用 DataSource 来构造它 使用连接池来构造它的好处就是 我们无需管 Connection 对象的创建与关闭了

后面调用 QueryRunner 对象的方法也无需传入 Connection 对象了

那我们就要用到另外一个开源数据库连接池组件 dbcp

以下为 dbcp 的用法:

/**

*

*

* dbcp
连接池的用法

*/

public class DBUtils {

private static Properties properties;

private static DataSource dataSource;

private static Connection conn;

static {

try {

properties = new Properties();

//
载入 dbcp 的配置文件

properties.load(DBUtils.class.

.getResourceAsStream("/dbcpconfig.properties"));

//
创建个 BasicDataSourceFactory 对象用于创建连接池对象

BasicDataSourceFactory b = new BasicDataSourceFactory();

//
properties 对象设置给 BasicDataSourceFactory

dataSource = b.createDataSource(properties);

} catch (Exception e) {

e.printStackTrace();

}

}

public static DataSource getdataSource() {

return dataSource;

}

}

注意:需要导入 dbcp 连接池的 jar commons-dbcp-1.2.1.jar 和所依赖的 commons-pool-1.2.jar

commons-collections-3.1.jar

//
这样我们就可以构造个 queryRunner 对象了

QueryRunner queryRunner = new QueryRunner(DBUtils.getdataSource());



QueryRunner
主要方法

//
更新一列

queryRunner.update("update Users set username=?,password=? where id = ?", new String[]{"11","22","1"});

//
删除一列

       queryRunner.update("delete from Users where id =?", 1);

--------------------------------------------------------------------------------------------------------------

如果要 使用 QueryRunner 的查询方法 也就是 query() 方法的话 那么就需要有对象来接收查询出来的 ResultSet

用什么来接收呢? 要用到 实现 ResultSetHandler 接口的类

1. BeanHandler
返回个 entitybean 对象

//
指定返回哪个 entitybean

ResultSetHandler rsh = new BeanHandler(Users.class);

QueryRunner queryRunner = new QueryRunner(DBUtils.getdataSource());

Users users = (Users) queryRunner.query("select * from Users where id=?",2, rsh);

System.out.println(users.getUsername());

--------------------------------------------------------------------------------------------------------------

2. BeanListHandler
返回个 List List 里装的是 entitybean 对象

QueryRunner queryRunner = new QueryRunner(DBUtils.getdataSource());

ResultSetHandler rsh = new BeanListHandler(Users.class);

List<Users> list = (List) queryRunner.query(

"select * from Users ", rsh);

for(Users user:list){

System.out.println(user.getUsername());

}

--------------------------------------------------------------------------------------------------------------

3. ArrayListHandler
返回 List 对象 list 里装的为 Object 数组

QueryRunner queryRunner = new QueryRunner(DBUtils.getdataSource());

//
使用 ArrayListHandler 实现类来存放结果会返回 ArrayList 对象

ResultSetHandler rsh = new ArrayListHandler();

//list
里装的是对象数组

List<Object[]> list = (List) queryRunner.query(

"select * from Users ", rsh);

for(Object[] user:list) {

for(Object obj:user)

{

System.out.println(obj);

}

}

-------------------------------------------------------------------------------------------------------------

4. ColumnListHandler
返回 List 对象



注意 : 构造 ColumnListHandler() 对象时候如果 是无参数的话



像这样 ResultSetHandler rsh = new ColumnListHandler();



查询出来的 List 里放的类型为主键的类型 值为主键的值 ;



ResultSetHandler rsh = new ColumnListHandler();

List<Integer> list = (List) queryRunner.query(

"select * from Users ", rsh);

//System.out.println(list.get(0).getClass().getSimpleName());

for(Integer in:list){

System.out.println(in);

}

构造 ColumnListHandler() 对象时候如果 是有参数的话



像这样 ResultSetHandler rsh = new ColumnListHandler( 主键值 );



那么查询出来的 List 里放的是就是这个主键对应的一行的记录 ;



ResultSetHandler rsh = new ColumnListHandler(2);

List<String> list = (List) queryRunner.query(

"select * from Users ", rsh);

for(String in:list){

System.out.println(in);

}

-------------------------------------------------------------------------------------------------------------

5. KeyedHandler
根据键返回 Map



ResultSetHandler rsh = new KeyedHandler();

//map
里装的 key 为主键的值 value 为主键对应的那行记录名称和记录

Map map = (Map) queryRunner.query(

"select id,username,password from Users ", rsh);

//
根据主键值出去那一列

// Map t = (Map) map.get(2);

System.out.println(map.get(2));

---------------------------------------------------------------------------------------------------------------

6. MapHandler
根据键返回 Map

QueryRunner queryRunner = new QueryRunner(DBUtils.getdataSource());

ResultSetHandler rsh = new MapHandler();

Map map = (Map) queryRunner.query(

"select * from Users ", rsh);

System.out.println(map);

---------------------------------------------------------------------------------------------------------------

7. ScalarHandler
根据键返回第一行记录

QueryRunner queryRunner = new QueryRunner(DBUtils.getdataSource());

//
这样就会返回第一行记录名字为 username 的值

ResultSetHandler rsh = new ScalarHandler("username");

  // 这的类型根据表里数据的类型而定 如果上面 ResultSetHandler rsh = new ScalarHandler("username");

//
取出来的类型为 int 的话 那么这里也必须由 int 来接收

String result = (String) queryRunner.query("select * from Users where id=99 ",

rsh);

System.out.println(result);

你可能感兴趣的:(apache,sql,xml,Go)