连接池

package demo02;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;

public class DBUtils {
    public static final String DRIVER = "com.mysql.jdbc.Driver";
    public static final String URL = "jdbc:mysql://localhost:3306/goods?characterEncoding=utf-8";
    public static final String USERNAME = "root";
    public static final String PASSWORD = "123456";
    /*
     * 创建连接池BasicDataSource
     */
    public static BasicDataSource dataSource = new BasicDataSource();
    //静态代码块
    static {
        //对连接池对象 进行基本的配置
        dataSource.setDriverClassName(DRIVER); // 这是要连接的数据库的驱动
        dataSource.setUrl(URL); //指定要连接的数据库地址
        dataSource.setUsername(USERNAME); //指定要连接数据的用户名
        dataSource.setPassword(PASSWORD); //指定要连接数据的密码
    }
    /*
     * 返回连接池对象
     */
    public static DataSource getDataSource(){
        return dataSource;
    }
}
package demo02;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;

import demo01.JDBCUtils;

public class demo01 {
    public static void main(String[] args) throws SQLException {
        insert();
    }
    public static  void insert() throws SQLException{
        QueryRunner qr =new QueryRunner(DBUtils.getDataSource());
        String sql ="insert into sort values(?,?,?)";        
        Object[] obj ={9,"橡皮擦","好吃又大补"};        
        int row= qr.update(sql,obj);        
        System.out.println(row);
    }
}

 

posted @ 2018-07-12 16:15  淅沥沥丶下雨  阅读(100)  评论(0编辑  收藏  举报