你的位置:首页 > Java教程

[Java教程]最全面的jdbcUtils,总有一种适合你


附加jar包,TxQueryRunner.java文件,dbconfig.properties配置文件(点击链接下载):
  http://files.cnblogs.com/files/xiaoming0601/jdbcUtils%E7%9B%B8%E5%85%B3jar%E5%8C%85.zip
 
1.普通的连接数据库,读取dbconfig.properties配置文件
  
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.
  1.c3p0-config.
 1  <??>  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.  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();  }}

~~~~~~~~~~~~~~~~~~相互学习~~~~~~~~~~~~~~~~~~~~