最全面的jdbcUtils,总有一种适合你
package cn.itcast.jdbcUtils; import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties; public class JdbcUtils {
private static final String dbconfig = "dbconfig.properties";
private static Properties prop = new Properties();
static {
try {
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(dbconfig);
prop.load(in);
Class.forName(prop.getProperty("driverClassName"));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Connection getConnection(){
try {
return DriverManager.getConnection(prop.getProperty("url"),prop.getProperty("username"),prop.getProperty("password"));
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
<?xml version="1.0" encoding="UTF-8" ?>
- <c3p0-config>
- <!-- 这是默认配置信息
-->
- <default-config>
- <!-- 连接四大参数配置
-->
<property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb1</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">root</property>
<property name="password">123</property>
- <!-- 池参数配置
-->
<property name="acquireIncrement">3</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">2</property>
<property name="maxPoolSize">10</property>
</default-config>
- <!-- 专门为oracle提供的配置信息
-->
- <named-config name="oracle-config">
<property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb1</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">root</property>
<property name="password">123</property>
<property name="acquireIncrement">3</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">2</property>
<property name="maxPoolSize">10</property>
</named-config>
</c3p0-config>
2.代码
package cn.itcast.jdbcUtils; import java.sql.Connection;
import java.sql.SQLException; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; public class JdbcUtils2 {
// 配置文件的默认配置!要求你必须给出c3p0-config.xml!!!
private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
public static DataSource getDataSource() {
return dataSource;
}
}
package cn.itcast.jdbcUtils; import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource; public class JdbcUtils3 {
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{
Connection conn = tl.get();
if(conn==null){
return dataSource.getConnection();
}
return conn;
}
//开启事物
public static void beginTransaction() throws SQLException{
Connection conn = tl.get();
if(conn!=null){
throw new SQLException("您已经开启了事务,在没有结束当前事务时,不能再开启事务");
}
conn = dataSource.getConnection();
conn.setAutoCommit(false);
tl.set(conn);//把当前线程的连接保存起来!
}
//提交事务
public static void commitTransaction() throws SQLException{
Connection conn = tl.get();
if(conn==null){
throw new SQLException("当前没有事务,所以不能提交");
}
conn.commit();
conn.close();
tl.remove();
}
//回滚事务
public static void rollbackTransaction() throws SQLException{
Connection conn = tl.get();
if(conn==null){
throw new SQLException("当前没有事务,所以不能回滚");
}
conn.rollback();
conn.close();
tl.remove();
}
//释放连接
public static void releaseConnection(Connection connection) throws SQLException {
Connection con = tl.get();
/*
* 判断它是不是事务专用,如果是,就不关闭!
* 如果不是事务专用,那么就要关闭!
*/
// 如果con == null,说明现在没有事务,那么connection一定不是事务专用的!
if(con == null) connection.close();
// 如果con != null,说明有事务,那么需要判断参数连接是否与con相等,若不等,说明参数连接不是事务专用连接
if(con != connection) connection.close();
}
}
~~~~~~~~~~~~~~~~~~相互学习~~~~~~~~~~~~~~~~~~~~
最全面的jdbcUtils,总有一种适合你的更多相关文章
- App渠道统计方法全面解析 总有一种适合你
一.App渠道统计对于App推广运营的重要性 (理解App渠道统计重要性的老司机,请直接移步到第二部分) App服务的竞争重点已经由功能竞争转向市场和运营的竞争,而App的推广与运营离不开App渠道统 ...
- [置顶]
echarts x轴文字显示不全(xAxis文字倾斜比较全面的3种做法值得推荐)
echarts x轴标签文字过多导致显示不全 如图: 解决办法1:xAxis.axisLabel 属性 axisLabel的类型是object ,主要作用是:坐标轴刻度标签的相关设置.(当然yAxis ...
- Node初学者入门,一本全面的NodeJS教程(转载)
分类 JS学习 发布 ourjs 2013-12-02 注意 转载须保留原文链接,译文链接,作者译者等信息. 作者: Manuel Kiessling 翻译: goddyzhao &a ...
- 大数据-将MP3保存到数据库并读取出来《黑马程序员_超全面的JavaWeb视频教程vedio》day17
黑马程序员_超全面的JavaWeb视频教程vedio\黑马程序员_超全面的JavaWeb教程-源码笔记\JavaWeb视频教程_day17-资料源码\day17_code\day17_1\ 大数据 目 ...
- [转载]Node入门 » 一本全面的Node.js教程
http://www.nodebeginner.org/index-zh-cn.html 作者: Manuel Kiessling 翻译: goddyzhao & GrayZhang & ...
- Node初学者入门,一本全面的NodeJS教程
作者: Manuel Kiessling 翻译: goddyzhao & GrayZhang & MondayChen 关于 本书致力于教会你如何用Node.js来开发应用,过程中会 ...
- 【Linux开发】全面的framebuffer详解
全面的framebuffer详解 一.FrameBuffer的原理 FrameBuffer 是出现在 2.2.xx 内核当中的一种驱动程序接口. Linux是工作在保护模式下,所以用户态进程是无法象D ...
- 超全面的.NET GDI+图形图像编程教程
本篇主题内容是.NET GDI+图形图像编程系列的教程,不要被这个滚动条吓到,为了查找方便,我没有分开写,上面加了目录了,而且很多都是源码和图片~ (*^_^*) 本人也为了学习深刻,另一方面也是为了 ...
- 最全面的 C++ 资源、框架大全
转载自 http://www.codeceo.com/article/cpp-resource-framework.html#0-tsina-1-99850-397232819ff9a47a7b7 ...
随机推荐
- UIRefreshControl的使用
注意: 1.需要在ios6.0之后的版本中使用 2.UIRefreshControl目前只能用于UITableViewController,如果用在其他ViewController中,运行时会错误(即 ...
- [leetode]Binary Search Tree Iterator
用个stack模拟递归即可 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * Tr ...
- [转载]JavaScript 中小数和大整数的精度丢失
标题: JavaScript 中小数和大整数的精度丢失作者: Demon链接: http://demon.tw/copy-paste/javascript-precision.html版权: 本博客的 ...
- C primer plus 练习题 第六章
16. #include <stdio.h> int main() { double remain = 100.0; ; ) { remain = remain * 0.08 + rema ...
- 【Xamarin报错】libpng warning : iCCP: Not recognizing known sRGB profile that has been edited
报错: Xamarin Android 编译时发生以下错误: libpng warning : iCCP: Not recognizing known sRGB profile that has be ...
- MFC窗口和控件大小等比例变化
第一步:OnInitDialog里保存对话框及其所有子窗体的Rect区域 CRect rect; GetWindowRect(&rect); listRect.AddTail(rect);// ...
- Apache配置多个网站的方法
Apache的虚拟主机是一种允许在同一台机器上,运行超过一个网站的解决方案.虚拟主机有两种,一种叫基于IP的(IP-based),另一种叫基于名字的(name-based).虚拟主机的存在,对用户来说 ...
- 同时使用Twitter nlp 和stanford parser的解决方法
因为Twitter nlp中使用了较老版本的stanford parser,导致不能同时使用 解决方法是使用未集成其它jar包的Twitter nlp,关于这点Stanford FAQ中也有说明(在F ...
- ubuntu 12.04亮度无法调节和无法保存屏幕亮度解决办法(echo_brightness)
经过多次更改失败重装后终于在官网的answers找到了解决办法:原文链接 http://askubuntu.com/questions/3841/desktop-doesnt-remember-bri ...
- [LeetCode] Longest Valid Parentheses 动态规划
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...