本文主要是介绍数据库工具包的使用(commons-dbutils-1.2.jar),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
朋友你还在为框架着急码?不用了,不会持久性框架照样可以开发高效的代码。如果你对Hibernate不是很了解,你的代码效率也会很烂! 如果你对IBatis不是很了解也不想学习,又想开发高效的项目,那么我建议你使用JDBC。如果你对JDBC感觉很,我推荐学习commons-dbutils使用,Common Dbutils是操作数据库的组件,对传统操作数据库的类进行二次封装,可以把结果集转化成List。
主要方法:
DbUtils类:启动类
ResultSetHandler接口:转换类型接口
MapListHandler类:实现类,把记录转化成List
BeanListHandler类:实现类,把记录转化成List,使记录为JavaBean类型的对象
Qrery Runner类:执行SQL语句的类
建立三个Java文件,命名为
BeanListExample.java
Guestbook.java
MapListExample.java
/*** ClassName: BeanListExample.java* created on 2012-4-15* Copyrights 2012 yjde All rights reserved.* site: http://blog.csdn.net/tjcyjd* email: 908599713@qq.com*/
package com.sy;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;/*** Class Description:* * @author yjde*/
public class BeanListExample {public static void main(String[] args) {Connection conn = null;String url = "jdbc:mysql://localhost:3306/people";String jdbcDriver = "com.mysql.jdbc.Driver";String user = "root";String password = "hicc";DbUtils.loadDriver(jdbcDriver);try {conn = DriverManager.getConnection(url, user, password);QueryRunner qr = new QueryRunner();List results = (List) qr.query(conn,"select id,name from guestbook", new BeanListHandler(Guestbook.class));for (int i = 0; i < results.size(); i++) {Guestbook gb = (Guestbook) results.get(i);System.out.println("id:" + gb.getId() + ",name:" + gb.getName());}} catch (SQLException e) {e.printStackTrace();} finally {DbUtils.closeQuietly(conn);}}
}
/*** ClassName: Guestbook.java* created on 2012-4-15* Copyrights 2012 yjde All rights reserved.* site: http://blog.csdn.net/tjcyjd* email: 908599713@qq.com*/
package com.sy;/*** Class Description:* * @author yjde*/
public class Guestbook {private Integer id;private String name;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}
/*** ClassName: MapListExample.java* created on 2012-4-15* Copyrights 2012 yjde All rights reserved.* site: http://blog.csdn.net/tjcyjd* email: 908599713@qq.com*/
package com.sy;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;/*** Class Description:* * @author yjde*/
public class MapListExample {public static void main(String[] args) {Connection conn = null;String url = "jdbc:mysql://localhost:3306/people";String jdbcDriver = "com.mysql.jdbc.Driver";String user = "root";String password = "hicc";DbUtils.loadDriver(jdbcDriver);try {conn = DriverManager.getConnection(url, user, password);QueryRunner qr = new QueryRunner();List results = (List) qr.query(conn,"select id,name from guestmessage", new MapListHandler());for (int i = 0; i < results.size(); i++) {Map map = (Map) results.get(i);System.out.println("id:" + map.get("id") + ",name:"+ map.get("name"));}} catch (SQLException e) {e.printStackTrace();} finally {DbUtils.closeQuietly(conn);}}
}
这篇关于数据库工具包的使用(commons-dbutils-1.2.jar)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!