zoukankan      html  css  js  c++  java
  • DbUtils的使用

    Commons DbUtils是Apache组织提供的一个对JDBC进行简单封装的开源工具类库,使用它能够简化JDBC应用程序的开发,同时也不会影响程序的性能

    导入jar包

    1. c3p0-0.9.1.2-jdk1.3.jar
    2. c3p0-0.9.1.2.jar
    3. c3p0-oracle-thin-extras-0.9.1.2.jar
    4. commons-dbutils-1.4.jar
    5. mysql-connector-java-5.1.7-bin.jar

    增删改

    public class TestDbUtils {
    
    	@Test
    	public void test() throws SQLException {
    		ComboPooledDataSource ds = new ComboPooledDataSource();
    		QueryRunner runner = new QueryRunner(ds);
    		
    		//增加
    		/*String sql = "insert into person values(null,?,?,?,null)";
    		runner.update(sql,"qf",21,new Date(0));*/
    		
    		//删除
    		/*String sql = "delete from person where id>?";
    		runner.update(sql,18);*/
    				
    		//修改
    		String sql = "update person set name=? where id=?";
    		runner.update(sql,"smile",18);
    	}
    }

    查询 

    查询一个

     1 public class TestDbUtils {
     2 
     3     @Test
     4     /*
     5      * 匿名内部类实现
     6      */
     7     public void test() throws SQLException {
     8         ComboPooledDataSource ds = new ComboPooledDataSource();
     9         QueryRunner runner = new QueryRunner(ds);
    10         
    11         Person person = runner.query("select * from person where id=?", new ResultSetHandler<Person>() {
    12 
    13             @Override
    14             public Person handle(ResultSet rs) throws SQLException {
    15                 Person p = null;
    16                 while(rs.next()) {
    17                     String name = rs.getString("name");
    18                     int age = rs.getInt("age");
    19                     Date time = rs.getDate("time");
    20                     String address = rs.getString("address");
    21                     p = new Person(name, age, time, address);
    22                 }
    23                 return p;
    24             }
    25         }, 2);
    26         System.out.println(person);
    27     }
    28     
    29     @Test
    30     /*
    31      * 使用ResultSetHandler接口的实现类
    32      */
    33     public void test2() throws SQLException {
    34         ComboPooledDataSource ds = new ComboPooledDataSource();
    35         QueryRunner runner = new QueryRunner(ds);
    36         
    37         Person person = runner.query("select * from person where id=?", new BeanHandler<Person>(Person.class), 2);
    38         System.out.println(person);
    39     }
    40 }

    查询多个

     1 public class TestDbUtils {
     2 
     3     /*
     4      * 使用ResultSetHandler接口的实现类
     5      */
     6     @Test
     7     public void test2() throws SQLException {
     8         ComboPooledDataSource ds = new ComboPooledDataSource();
     9         QueryRunner runner = new QueryRunner(ds);
    10         
    11         List<Person> list = runner.query("select * from person ", new BeanListHandler<Person>(Person.class));
    12         for (Person person : list) {
    13             System.out.println(person);
    14         }
    15     }
    16 }

    输出

    Person [name=smile, age=12, time=2018-03-06 19:32:35.0, address=null]
    Person [name=wxf, age=13, time=2018-03-07 19:33:37.0, address=null]
    Person [name=smile, age=24, time=1970-01-01 00:00:00.0, address=null]
  • 相关阅读:
    数字音乐均衡器
    移植x264到vs2008之二
    无线连接频繁掉线,解决方法之telnet命令突破ddwrt端口最大数连接限制分析
    最新开发的消费平台开发过程 持续更新(二)
    .net 4.0 下请求验证模式变化 应对方法
    DDWRT无线参数解读
    利用 Application_Error 捕获所有异常
    location.reload() 和 location.replace()的区别和应用
    纯CSS 实现组织架构图,学习
    Syslog架设windows日志服务器
  • 原文地址:https://www.cnblogs.com/qf123/p/10102190.html
Copyright © 2011-2022 走看看