数据库开源框架DBUtils的用法

1.DBUtils的jar包

数据库开源框架DButils

2.<!-- default-config 默认的配置,  -->

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>

	<!-- default-config 默认的配置,  -->
  <default-config>
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost/bank</property>
    <property name="user">root</property>
    <property name="password">123456</property>
    
    
    <property name="initialPoolSize">10</property>
    <property name="maxIdleTime">30</property>
    <property name="maxPoolSize">100</property>
    <property name="minPoolSize">10</property>
    <property name="maxStatements">200</property>
  </default-config>
  
   <!-- This app is massive! -->
  <named-config name="oracle"> 
    <property name="acquireIncrement">50</property>
    <property name="initialPoolSize">100</property>
    <property name="minPoolSize">50</property>
    <property name="maxPoolSize">1000</property>

    <!-- intergalactoApp adopts a different approach to configuring statement caching -->
    <property name="maxStatements">0</property> 
    <property name="maxStatementsPerConnection">5</property>

    <!-- he's important, but there's only one of him -->
    <user-overrides user="master-of-the-universe"> 
      <property name="acquireIncrement">1</property>
      <property name="initialPoolSize">1</property>
      <property name="minPoolSize">1</property>
      <property name="maxPoolSize">5</property>
      <property name="maxStatementsPerConnection">50</property>
    </user-overrides>
  </named-config>

 
</c3p0-config>
	

默认文件名c3p-config.xml  系统默认执行 无需自动连接

数据库版本在4及以上可以不用启动,底层系统自动启动 

 

数据库开源框架DButils

数据库开源框架DButils配置好 可以直接写 只需俩行代码即可实现

package com.itheima.uitl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.junit.Test;

import com.itheima.domain.Account;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class TestDemo {

	@Test
	public void test() throws SQLException{
		QueryRunner qRunner=new QueryRunner(new ComboPooledDataSource() );
		//插入
		qRunner.update("insert into account values (28,?,?)","eee",2220);
		//删除
		qRunner.update("delete  from account where id=?",28);
		//修改
		qRunner.update("update account set money =? where id=?",60000,4);
		//单个查询
		Account account1 = qRunner.query("select * from account where id=?",new BeanHandler<Account>(Account.class),4);
		System.out.println(account1);
		//多个查询
		Account query = qRunner.query("select * from account where id=?", new BeanHandler<Account>(Account.class),4);
		System.out.println(query);
		List<Account> list = qRunner.query("select * from account",new BeanListHandler<>(Account.class));
		for (Account account : list) {
			System.out.println(account);
		}
	}
	
	
}

具体配置文件已经上传   DButils模板可以下载  仿写 

相关文章: