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); } } }2.使用簡單的連接池,給出c3p0-config.xml配置文件 1.c3p0-config.xml配置文件: 1 <?xml version="1.0" encoding="UTF-8" ?> 2 - <c3p0-config> 3 - <!-- 這是默認配置信息 4 --> 5 - <default-config> 6 - <!-- 連接四大參數配置 7 --> 8 <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb1</property> 9 <property name="driverClass">com.mysql.jdbc.Driver</property> 10 <property name="user">root</property> 11 <property name="password">123</property> 12 - <!-- 池參數配置 13 --> 14 <property name="acquireIncrement">3</property> 15 <property name="initialPoolSize">10</property> 16 <property name="minPoolSize">2</property> 17 <property name="maxPoolSize">10</property> 18 </default-config> 19 - <!-- 專門為oracle提供的配置信息 20 --> 21 - <named-config name="oracle-config"> 22 <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb1</property> 23 <property name="driverClass">com.mysql.jdbc.Driver</property> 24 <property name="user">root</property> 25 <property name="password">123</property> 26 <property name="acquireIncrement">3</property> 27 <property name="initialPoolSize">10</property> 28 <property name="minPoolSize">2</property> 29 <property name="maxPoolSize">10</property> 30 </named-config> 31 </c3p0-config> View Code
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; } }3.使用連接池,最全面的獲取dataSource,connection以及釋放connection的方法,並提供開啟,提交回滾事務的方法
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(); } }
~~~~~~~~~~~~~~~~~~相互學習~~~~~~~~~~~~~~~~~~~~