Create connection

From , 4 Years ago, written in Java, viewed 54 times.
URL https://pastebin.vip/view/3a20f62a
  1. package Util;
  2.  
  3. import java.io.IOException;
  4. import java.sql.Connection;
  5. import java.sql.SQLException;
  6. import java.util.Properties;
  7.  
  8. import org.apache.commons.dbcp.BasicDataSource;
  9.  
  10. public class DBUtil {
  11.        
  12.         private static BasicDataSource ds;
  13.        
  14.         static {
  15.                 Properties p = new Properties();
  16.                 try {
  17.                         //读取参数
  18.                         p.load(DBUtil.class.getClassLoader()
  19.                                 .getResourceAsStream("db.properties"));
  20.                         String driver = p.getProperty("driver");
  21.                         String url = p.getProperty("url");
  22.                         String user = p.getProperty("user");
  23.                         String pwd = p.getProperty("pwd");
  24.                         String initsize = p.getProperty("initsize");
  25.                         String maxsize = p.getProperty("maxsize");
  26.                         //创建连接池
  27.                         ds = new BasicDataSource();
  28.                         //设置参数
  29.                         ds.setDriverClassName(driver);
  30.                         ds.setUrl(url);
  31.                         ds.setUsername(user);
  32.                         ds.setPassword(pwd);
  33.                         ds.setInitialSize(new Integer(initsize));
  34.                         ds.setMaxActive(new Integer(maxsize));
  35.                 } catch (IOException e) {
  36.                         e.printStackTrace();
  37.                         throw new RuntimeException(
  38.                                 "加载配置文件失败.", e);
  39.                 }
  40.         }
  41.        
  42.         /**
  43.          * 本方法是采用BasicDataSource创建的连接,
  44.          * 则Connection接口是由DBCP实现的,而并非
  45.          * 由Oracle实现.当然DBCP的实现类中会调用
  46.          * Oracle的驱动类.
  47.          */
  48.         public static Connection getConnection()
  49.                 throws SQLException {
  50.                 return ds.getConnection();
  51.         }
  52.        
  53.         /**
  54.          * 由DBCP所提供的连接对象的实现类,
  55.          * 其close方法的逻辑不是关闭连接,
  56.          * 而是归还连接,即它会将连接内包含的
  57.          * 数据清空,并且将连接的状态设置为空闲.
  58.          * */
  59.         public static void close(Connection conn) {
  60.                 if(conn != null) {
  61.                         try {
  62.                                 conn.close();
  63.                         } catch (SQLException e) {
  64.                                 e.printStackTrace();
  65.                                 throw new RuntimeException(
  66.                                         "关闭连接失败.", e);
  67.                         }
  68.                 }
  69.         }
  70.        
  71.         public static void rollback(Connection conn) {
  72.                 if(conn != null) {
  73.                         try {
  74.                                 conn.rollback();
  75.                         } catch (SQLException e) {
  76.                                 e.printStackTrace();
  77.                                 throw new RuntimeException(
  78.                                         "事务回滚失败.", e);
  79.                         }
  80.                 }
  81.         }
  82.        
  83.         public static void main(String[] args)
  84.                 throws SQLException {
  85.                 Connection conn =
  86.                         DBUtil.getConnection();
  87.                 System.out.println(conn);
  88.                 DBUtil.close(conn);
  89.         }
  90.  
  91. }
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  

Reply to "Create connection"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website