/ * Druid连接池的工具类 */ public class JDBCUtils { //1.定义成员变量 private static DataSource ds; static { try { //1.加载配置文件 Properties pro = new Properties(); pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties")); //2.获取DataSource ds = DruidDataSourceFactory.createDataSource(pro); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } / * 获取连接 */ public static Connection getConnection() throws SQLException { return ds.getConnection(); } / * 释放资源 */ public static void close(Statement stmt,Connection conn){ /*if (stmt!=null){ try { stmt.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (conn!=null){ try { conn.close();//归还连接 } catch (SQLException throwables) { throwables.printStackTrace(); } }*/ close(null,stmt,conn); } public static void close(ResultSet rs, Statement stmt, Connection conn){ if (rs!=null){ try { rs.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (stmt!=null){ try { stmt.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (conn!=null){ try { conn.close();//归还连接 } catch (SQLException throwables) { throwables.printStackTrace(); } } } / * 获取连接池方法 */ public static DataSource getDataSource(){ return ds; } }到此这篇druid连接池配置详解(druid连接池工具类)的文章就
介绍到这了,更多相关内容请继续浏览下面的相关
推荐文章,希望大家都能在
编程的领域有一番成就!
版权声明:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权、违法违规、事实不符,请将相关资料发送至xkadmin@xkablog.com进行投诉反馈,一经查实,立即处理!
转载请注明出处,原文链接:https://www.xkablog.com/rfx/54959.html