zoukankan      html  css  js  c++  java
  • 使用MySQL连接池

    手动配置连接池:

    /**
         * 手动设置连接池
         */
        public void demo1(){
    
            // 获得连接:
            Connection conn = null;
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            try{
                // 创建连接池:
                ComboPooledDataSource dataSource = new ComboPooledDataSource();
                // 设置连接池的参数:
                dataSource.setDriverClass("com.mysql.jdbc.Driver");
                dataSource.setJdbcUrl("jdbc:mysql:///jdbctest");
                dataSource.setUser("root");
                dataSource.setPassword("abc");
                dataSource.setMaxPoolSize(20);
                dataSource.setInitialPoolSize(3);
                
                // 获得连接:
                conn = dataSource.getConnection();
                // 编写Sql:
                String sql = "select * from user";
                // 预编译SQL:
                pstmt = conn.prepareStatement(sql);
                // 设置参数
                // 执行SQL:
                rs = pstmt.executeQuery();
                while(rs.next()){
                    System.out.println(rs.getInt("uid")+"   "+rs.getString("username")+"   "+rs.getString("password")+"   "+rs.getString("name"));
                }
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                JDBCUtils.release(rs, pstmt, conn);
            }
        }

    使用配置文件配置连接池:

    配置文件xml如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <c3p0-config>
    
      <default-config>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql:///jdbctest</property>
        <property name="user">root</property>
        <property name="password">abc</property>
        <property name="initialPoolSize">5</property>
        <property name="maxPoolSize">20</property>
      </default-config>
      
    </c3p0-config>

    代码如下:

    /**
         * 使用配置文件的方式
         */
        public void demo2(){
            Connection conn = null;
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            try{
                /*// 获得连接:
                ComboPooledDataSource dataSource = new ComboPooledDataSource();*/
                // 获得连接:
                // conn = dataSource.getConnection();
                conn = JDBCUtils2.getConnection();
                // 编写Sql:
                String sql = "select * from user";
                // 预编译SQL:
                pstmt = conn.prepareStatement(sql);
                // 设置参数
                // 执行SQL:
                rs = pstmt.executeQuery();
                while(rs.next()){
                    System.out.println(rs.getInt("uid")+"   "+rs.getString("username")+"   "+rs.getString("password")+"   "+rs.getString("name"));
                }
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                JDBCUtils2.release(rs, pstmt, conn);
            }
        }
  • 相关阅读:
    codeforces 980A Links and Pearls
    zoj 3640 Help Me Escape
    sgu 495 Kids and Prizes
    poj 3071 Football
    hdu 3853 LOOPS
    hdu 4035 Maze
    hdu 4405 Aeroplane chess
    poj 2096 Collecting Bugs
    scu 4444 Travel
    zoj 3870 Team Formation
  • 原文地址:https://www.cnblogs.com/shouyaya/p/12336632.html
Copyright © 2011-2022 走看看