java commons-dbutils使用

     其实很多小型项目,我们没必要去死套三大框架,封装jdbc足以搞定,但我们不能每次做项目都去封装啊,自己封装的又怕效率不高,我推荐一个国外的jdbc封装,有源码的,希望大家喜欢。

     首先导入三个包

        commons-dbutils-1.3.jar  (核心包)

        commons-dbutils-1.3-sources.jar(辅助包)

        commons-dbutils-1.3-javadoc.jar(辅助包)

     然后导入数据库包,我用的是mysql

       mysql-connector-java-5.1.16-bin.jar

      自己写了个DBHepler代码:

package com.dal;

import java.sql.*;

import java.util.List;

import org.apache.commons.dbutils.*;

import org.apache.commons.dbutils.QueryRunner;

import org.apache.commons.dbutils.handlers.BeanListHandler;

public class DBHepler {

	   /**

     * 获取数据库连接

     * @return

     */

    public static Connection getConnection() {

        Connection conn = null;  

        String jdbcURL = "jdbc:mysql://127.0.0.1:3306/hslog";

        String jdbcDriver = "com.mysql.jdbc.Driver";

        String user = "root";

        String password = "123456";

        try {

            DbUtils.loadDriver(jdbcDriver);

            conn = DriverManager.getConnection(jdbcURL, user, password);

        } catch (SQLException e) {

            // handle the exception

            e.printStackTrace();

        } finally {

            //DbUtils.closeQuietly(conn);

        }

        return conn;

    }

 

    /**

     * 查找多个对象

     * @param sqlString 

     * @param clazz

     * @return

     */

    public static List query(String sqlString, Class clazz) {

        List beans = null;

        Connection conn = null;

        try {

            conn = getConnection();

            QueryRunner qRunner = new QueryRunner();

            beans =

                (List) qRunner.query(

                    conn,

                    sqlString,

                    new BeanListHandler(clazz));

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            DbUtils.closeQuietly(conn);

        }

        return beans;

    }

 

    public static void QueryArray(String[] sqlString)

    {

    	

    }

    

    /**

     * 查找对象

     * @param sqlString

     * @param clazz

     * @return

     */

    public static Object get(String sqlString, Class clazz) {

        List beans = null;

        Object obj = null;

        Connection conn = null;

        try {

            conn = getConnection();

            QueryRunner qRunner = new QueryRunner();

            beans =

                (List) qRunner.query(

                    conn,

                    sqlString,

                    new BeanListHandler(clazz));

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            DbUtils.closeQuietly(conn);

        }

        if(beans!=null && !beans.isEmpty()){ //注意这里

             obj=beans.get(0);

        }

        return obj;

    }

 

    /**

     * 执行更新的sql语句,插入,修改,删除

     * @param sqlString

     * @return

     */

    public static boolean update(String sqlString) {

        Connection conn = null;

        boolean flag = false;

        try {

            conn = getConnection();

            QueryRunner qRunner = new QueryRunner();

            int i = qRunner.update(conn,sqlString);

            if (i > 0) {

                flag = true;

            }

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            DbUtils.closeQuietly(conn);

        }

        return flag;

    }  

}



     我们先来创建数据库(编号,姓名,年龄) 

DROP TABLE IF EXISTS `myemp`;

CREATE TABLE `myemp` (

  `id` int(20) NOT NULL auto_increment,

  `name` varchar(100) default NULL,

  `age` int(20) default NULL,

  PRIMARY KEY  (`id`)

) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=gb2312;

      我再来建设一个model类  MyEmp类

package com;



public class MyEmp {

   private int id;

   private String name;

   private int age;

	public int getId() {

		return id;

	}

	public void setId(int id) {

		this.id = id;

	}

	public String getName() {

		return name;

	}

	public void setName(String name) {

		this.name = name;

	}

	public int getAge() {

		return age;

	}

	public void setAge(int age) {

		this.age = age;

	}

}

写个测试类  test

package com;

import java.sql.Connection;

import java.util.List;



import org.apache.commons.dbutils.QueryRunner;

public class test {

   public static void main(String[] args) {

	  List<MyEmp> list=(List<MyEmp>)DBHepler.query("select * from myemp",MyEmp.class);

	  for (MyEmp myemp : list) {

		System.out.println("编号:"+myemp.getId()+"  姓名:"+myemp.getName()+" 年龄"+myemp.getAge());

	}

}

}

运行一下:

 结果:编号:1  姓名:chenjie 年龄18
返回集合我会了,我想其余的增删改查,就会很简单了。大家再也不会因为关闭了数据库连接而记录集不能用的情况了

 源代码及包下载地址:http://download.csdn.net/source/3426136

你可能感兴趣的:(commons)