zoukankan      html  css  js  c++  java
  • 数据库连接池之_Druid简单使用

    数据库连接池:

      连接池是创建和管理一个连接的缓冲池的技术,这些连接真备好被任何需要他们的线程使用,可以对传统的JDBCjava数据库连接()进行优化

      在实际开发中,我们需要频繁的操作数据库,这就意味着我们要不断的创建连接对象,而频繁创建连接对象并且销毁连接对象相对来讲是比较耗时的,针对于这种情况,我们可以创建一个池子出来,里面放了一些连接对象,用的时候从里面拿,用完之后再还回去,这个池子就是数据库连接池.

    常见的数据库连接池:

      DBCP: (DataBase Connection Pool)数据库连接池, 由Apache公司开发.
            Druid: 阿里旗下的连接池技术.
            C3P0: 目前使用它的开源项目有: Hibernate, Spring等.

    Druid连接池:

        1,自动读取配置文件

     1 @Test
     2     public void demo2(){
     3         Connection connection=null;
     4         PreparedStatement preparedStatement=null;
     5         ResultSet resultSet=null;
     6         
     7         try {
     8             //读取配置文件
     9             Properties properties =new Properties();
    10             properties.load(new FileInputStream("src//config.properties"));
    11             DataSource dataSource=DruidDataSourceFactory.createDataSource(properties);
    12             
    13             //获取连接
    14             connection =dataSource .getConnection();
    15             
    16             String sql="select * from Book;";
    17             preparedStatement=connection.prepareStatement(sql);
    18             resultSet=preparedStatement.executeQuery();
    19             while(resultSet.next()){
    20                 System.out.println(resultSet.getInt("id")+"  "+resultSet.getString("bname"));
    21             }
    22         } catch (Exception e) {
    23             e.printStackTrace();
    24         }finally {
    25             JDBCUtils.close(resultSet,preparedStatement,connection);
    26         }
    27         
    28     }

      2,手动设置参数

     1 @Test
     2     public void demo() {
     3         Connection conn = null;
     4         PreparedStatement ps = null;
     5         ResultSet resultSet = null;
     6 
     7         DruidDataSource dds = new DruidDataSource();
     8         dds.setDriverClassName("com.mysql.jdbc.Driver");
     9         dds.setUrl("jdbc:mysql:///day03");
    10         dds.setUsername("root");
    11         dds.setPassword("123");
    12 
    13         try {
    14             conn = dds.getConnection();
    15             String sql = "select * from Book;";
    16             ps = conn.prepareStatement(sql);
    17             resultSet = ps.executeQuery();
    18             while (resultSet.next()) {
    19                 System.out.println(resultSet.getInt("id") + "  " + resultSet.getString("bname"));
    20             }
    21         } catch (SQLException e) {
    22             e.printStackTrace();
    23         } finally {
    24             if (conn != null) {
    25                 try {
    26                     conn.close();
    27                 } catch (SQLException e) {
    28                     e.printStackTrace();
    29                 }
    30                 conn = null;
    31             }
    32             if (ps != null) {
    33                 try {
    34                     ps.close();
    35                 } catch (SQLException e) {
    36                     e.printStackTrace();
    37                 }
    38                 ps = null;
    39             }
    40             if (resultSet != null) {
    41                 try {
    42                     resultSet.close();
    43                 } catch (SQLException e) {
    44                     e.printStackTrace();
    45                 }
    46                 resultSet = null;
    47             }
    48         }
    49     }
  • 相关阅读:
    必备单词
    Vim
    Linux基础
    python链表操作详解
    冒泡和快速排序
    学员练车选课系统
    面试题
    获取resp:heads:content-disposition的filename
    记录springBoot启动报错(无脑型)
    springBoot+Vue搭建新项目(1)
  • 原文地址:https://www.cnblogs.com/zhuguangzhe/p/7458166.html
Copyright © 2011-2022 走看看