zoukankan      html  css  js  c++  java
  • c3p0连接池和dbutils的使用

    导入包


    关于c3p0配置

    <?xml version="1.0" encoding="UTF-8"?>
    <c3p0-config>
    	<default-config>		
    	 <property name="user">root</property>
    		<property name="password">339446</property>
    		<property name="driverClass">com.mysql.jdbc.Driver</property>
    		<property name="jdbcUrl">jdbc:mysql:///web20?useSSL=false</property>
    	</default-config> 
    </c3p0-config> 
    

    关于封装工具类

    package com.itheima.utils;
    
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import javax.sql.DataSource;
    
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    
    public class DataSourceUtils {
    
    	private static DataSource dataSource = new ComboPooledDataSource();
    
    	private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
    
    	// 直接可以获取一个连接池
    	public static DataSource getDataSource() {
    		return dataSource;
    	}
    	
    	public static Connection getConnection() throws SQLException{
    		return dataSource.getConnection();
    	}
    
    	// 获取连接对象
    	public static Connection getCurrentConnection() throws SQLException {
    
    		Connection con = tl.get();
    		if (con == null) {
    			con = dataSource.getConnection();
    			tl.set(con);
    		}
    		return con;
    	}
    
    	// 开启事务
    	public static void startTransaction() throws SQLException {
    		Connection con = getCurrentConnection();
    		if (con != null) {
    			con.setAutoCommit(false);
    		}
    	}
    
    	// 事务回滚
    	public static void rollback() throws SQLException {
    		Connection con = getCurrentConnection();
    		if (con != null) {
    			con.rollback();
    		}
    	}
    
    	// 提交并且 关闭资源及从ThreadLocall中释放
    	public static void commitAndRelease() throws SQLException {
    		Connection con = getCurrentConnection();
    		if (con != null) {
    			con.commit(); // 事务提交
    			con.close();// 关闭资源
    			tl.remove();// 从线程绑定中移除
    		}
    	}
    
    	// 关闭资源方法
    	public static void closeConnection() throws SQLException {
    		Connection con = getCurrentConnection();
    		if (con != null) {
    			con.close();
    		}
    	}
    
    	public static void closeStatement(Statement st) throws SQLException {
    		if (st != null) {
    			st.close();
    		}
    	}
    
    	public static void closeResultSet(ResultSet rs) throws SQLException {
    		if (rs != null) {
    			rs.close();
    		}
    	}
    
    }
    

    使用案例

    public List<Product> findAllProduct() throws SQLException {
    		return new QueryRunner(DataSourceUtils.getDataSource()).query("select * from product", new BeanListHandler<Product>(Product.class));
    	}


  • 相关阅读:
    VScode 修改中文字体
    missing KW_END at ')' near '<EOF>'
    SQL inner join, join, left join, right join, full outer join
    SQL字符替换函数translater, replace
    SQL COOKBOOK SQL经典实例代码 笔记第一章代码
    sqlcook sql经典实例 emp dept 创建语句
    dateutil 2.5.0 is the minimum required version python
    安装postgresql后找不到服务 postgresql service
    Postgres psql: 致命错误: 角色 "postgres" 不存在
    【西北师大-2108Java】第十六次作业成绩汇总
  • 原文地址:https://www.cnblogs.com/fengnan/p/9311944.html
Copyright © 2011-2022 走看看