天天看點

tomcat連接配接池和dbutils使用

1.    配置tomcat下的conf下的context.xml檔案,在<context> </context>之間添加連接配接池配置: 

           <context>

 <resource name="jdbc/lhy"     <--對應web.xml     <res-ref-name> -->

            auth="container" 

        type="javax.sql.datasource"    <--對應web.xml  

<res-type>  -->

            maxactive="20" 

            maxidel="10"

            maxwait="1000" 

            username="root" 

            password="123456" 

   driverclassname="com.mysql.jdbc.driver" 

   url="jdbc:mysql://127.0.0.1:3306/lhy"  > 

</resource> 

</context>

在項目的webroot->web-inf下的web.xml中<web-app>

之間配置

<web-app>

<resource-ref>

  <description>dbconnection</description>

  <res-ref-name>jdbc/lhy</res-ref-name>

  <res-type>javax.sql.datasource</res-type>

  <res-auth>container</res-auth>

  </resource-ref>

</web-app>

配置dbutils

注意empbean裡面定義的屬性要和資料庫表中的名字一樣,

這樣才會自動把資料庫中的列比對給相應的bean屬性

tomcat連接配接池和dbutils使用

資料庫

tomcat連接配接池和dbutils使用

實體類empbean

package bean;

public

class empbean {

      private

int id;

      private string

name;

int age;

    public

int getid() {

        return

id;

    }

void setid(int id) {

        this.id = id;

    public string getname() {

voidsetname(string name) {

        this.name = name;

int getage(){

age;

void setage(int age) {

        this.age = age;

}

封裝update,query方法代碼類dbhelper

package until;

importjava.sql.sqlexception;

importjavax.naming.context;

importjavax.naming.initialcontext;

importjavax.naming.namingexception;

importjavax.sql.datasource;

importorg.apache.commons.dbutils.queryrunner;

importorg.apache.commons.dbutils.resultsethandler;

class dbhelper{

    public queryrunnergetqueryrunner() {

        queryrunner qr = null;

        try {

            context context = newinitialcontext();

            datasource ds = (datasource) context

                    .lookup("java:/comp/env/jdbc/lhy");

            qr = newqueryrunner(ds);

            context.close();

        } catch(namingexception e) {

            e.printstacktrace();

        }

        return qr;

intupdate(string sql) {

        int result =0;

        queryrunner qr = this.getqueryrunner();

            result = qr.update(sql);

        } catch(sqlexception e) {

        return result;

intupdate(string sql,object [] params) {

            result = qr.update(sql, params);

    public object query(stringsql,resultsethandler rsh) {

        object list = null;

            list = qr.query(sql, rsh);

        } catch (sqlexceptione) {

        return list;

    public object query(stringsql,object [] params ,resultsethandler rsh) {

            list = qr.query(sql,params, rsh);

資料庫操作empdao

packagedao;

importjava.util.list;

importorg.apache.commons.dbutils.handlers.beanlisthandler;

importuntil.dbhelper;

importbean.empbean;

publicclass empdao {

       string sel_sql="select * from empwhere id=? and age=?";

       string ins_sql="insert into emp(name,age) values(?,?)";

       dbhelper db=new dbhelper();

       public list <empbean> sel(intid,int age)

       {

              object params[] = {id,age};

              list<empbean>list=(list<empbean>) db.query(sel_sql, params, newbeanlisthandler(empbean.class));

              return list;

       }

       public int insertemp(empbean emp)

              object params[] ={emp.getname(),emp.getage()};

              return db.update(ins_sql, params);

測試類test

importdao.empdao;

publicclass test {

       public static void main(string[] args) {

              empdao emp=new empdao();

//            emp.sel(1, 20);

              empbean eb=new empbean();

              eb.setage(20);

              eb.setname("劉會要");

              int i=emp.insertemp(eb);

              if(i>0)

                     system.out.println("添加成功");

              else

                     system.out.println("添加失敗");