在开源中国上看到站长的博客,说开源中国是用java+mysql写的,没有用庞大的Hibernate,而是用的DbUtils组件,于是对这个组件产生了兴趣,根据“JDBC的轻量级封装DBUtils开源组件的使用(一)”简单地学习了一下。下面是写的一个Demo:
TestDbUtils.java
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.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; /** * @author 张省三 * E-mail:xingsan.zhang@gmail.com * 2013-7-6 下午10:17:32 */ public class TestDbUtils { private static String driverClassName = "com.mysql.jdbc.Driver"; private static String url = "jdbc:mysql://localhost:3306/liuyanban"; private static String user = "root"; private static String password = "fendou"; private static Connection conn = null; //DBCP数据源 /*public static BasicDataSource setupDataSource(String connectURI) { BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName(driverClassName); ds.setUsername(user); ds.setPassword(password); ds.setUrl(connectURI); //ds.setMinIdle(5); return ds; }*/ QueryRunner runner = new QueryRunner(); /** * 得到连接方法 */ public static void getConn(){ if(DbUtils.loadDriver(driverClassName)){//加载驱动 try { conn = DriverManager.getConnection(url, user, password);//得到连接 } catch (SQLException e) { System.out.println("连接失败!"); e.printStackTrace(); } } } /** * 增、删、改操作方法 */ public void update(){ getConn();//得到连接 boolean b = false; String sql = "insert into user (username,password) values(?,?)"; try { if(runner.update(conn, sql, "xiaofeng","123456") > 0){//如果返回结果大于0,则执行成功 System.out.println("插入成功!"); b = true; } } catch (SQLException e) { e.printStackTrace(); } finally{ try { DbUtils.close(conn);//关闭连接 } catch (SQLException e) { e.printStackTrace(); } } } //根据主键值查询,因为查询得到的结果只可能有一条记录,所以这里使用BeanListHandler BeanHandler bh = null; @SuppressWarnings("unchecked") /** * 查询方法,返回一条记录 */ public User select(int userId){ getConn(); User user = null; String sql = "select *from user where userId = ?"; bh = new BeanHandler<User>(User.class);//实例化BeanHandler对象 try { user = runner.query(conn, sql, bh, userId);//查询 System.out.println(user.getUsername()+"---"+user.getPassword()); return user; } catch (SQLException e) { System.out.println("查询出错!"); e.printStackTrace(); } finally{ try { DbUtils.close(conn);//关闭连接 } catch (SQLException e) { e.printStackTrace(); } } return user; } //返回一个集合,用BeanListHandler类 BeanListHandler blh = null; @SuppressWarnings("unchecked") /** * 查询方法,返回一个集合 */ public List select(){ getConn(); List<User> list = null; String sql = "select *from user"; blh = new BeanListHandler<User>(User.class);//实例化BeanListHandler对象 try { list = runner.query(conn, sql, blh);//查询,返回List for(User user : list){ System.out.println(user.getUsername()+"---"+user.getPassword()); } return list; } catch (SQLException e) { System.out.println("查询出错!"); e.printStackTrace(); } finally{ try { DbUtils.close(conn);//关闭连接 } catch (SQLException e) { e.printStackTrace(); } } return list; } public static void main(String[] args) { //new TestDbUtils().update(); //new TestDbUtils().select(1); new TestDbUtils().select(); } }
User.java
/** * @author 张省三 * E-mail:xingsan.zhang@gmail.com * 2013-7-7 上午01:56:49 */ public class User { private String username; private String password; private String email; private int isAdmin; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getIsAdmin() { return isAdmin; } public void setIsAdmin(int isAdmin) { this.isAdmin = isAdmin; } }