Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 287|回复: 0

[jsp学习]一个学习编写连接池的好例子

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-1 22:31:47 | 显示全部楼层 |阅读模式
    1. 一个学习编写连接池的好例子,有自动调节,输出使用报告等功能。源码请下载,这是如何使用这个连接池的代码:
    2. import java.sql.*;
    3. public class SimpleConstructorExample
    4. {
    5.         // 如果你的数据库不是mysql,请改变这些配置
    6.         static String dbURL = "jdbc:mysql://localhost:3306/store_product";
    7.         static String dbUserName = "";
    8.         static String dbPassword = "";
    9.         static String dbDriver = "org.gjt.mm.mysql.Driver";
    10.         // 展示一个使用构造器的例子
    11.     public static void main(String[] args)
    12.     {
    13.         ConnectionPool pool = new ConnectionPool();  // start a default pool
    14.         try
    15.         {
    16.             pool.setURL( dbURL );                                  // 数据库 url
    17.             pool.setDriverClassName( dbDriver );                   // 驱动程序必须在classpath中!
    18.                         pool.setUser( dbUserName );                            // 设置用户名
    19.                         pool.setPassword( dbPassword );                        // 口令
    20.             pool.start();                                          // 启动连接池
    21.             Connection con = pool.getConnection("Test Program");   // 从池中获取一个连接并标识自己         
    22.                         useConnection(con);                                                   // 使用连接
    23.             pool.returnConnection( con );                          // 将连接归还到连接池,以便重用
    24.             System.out.println( pool.getReport() );                // 打印连接池的使用报告
    25.         }
    26.         catch(SQLException e)  
    27.         {
    28.             System.out.println( e.getMessage() );
    29.         }
    30.         catch(ClassNotFoundException cnf)  // might be thrown by setDriverClassName()
    31.         {
    32.             System.out.println( cnf.getMessage() );
    33.         }      
    34.     }
    35.     // 使用连接的简单例子
    36.         public static void useConnection(Connection con) throws SQLException
    37.         {
    38.                 System.out.println("仅显示在当前数据库中的表...");
    39.                 Statement statmt;
    40.                 ResultSet res;
    41.                 statmt = con.createStatement();
    42.                 String statement = "show tables";
    43.                 res = statmt.executeQuery(statement);
    44.                 while ( res.next() )
    45.                         System.out.println( res.getString(1) );
    46.                 res.close();
    47.                 statmt.close();
    48.         }
    49. }
    50. 下面是作者关于这个连接池的介绍:
    51. ==========================================================================
    52. 0. WHAT THIS PROGRAM DOES
    53. ==========================================================================
    54. This JDBC Connection Pool is SELF REGULATING.  That is, when you create
    55. the pool, you tell it how big to make itself.  If, over time, you need
    56. more connections than you originally requested, the pool will build new
    57. ones.
    复制代码
    These extra connections, if left idle for more than a certain definable
    time, will be closed down by the pool to be kind to your database. You can access a report from your pool to find out how many connections
    are in it, how long they have been idle, which ones are being used, and
    what program is using the used connections. ==========================================================================
    1. ABOUT THE AUTHOR
    ========================================================================== Jeff Patterson - from Portland, Oregon. Senior type programmer guy. On the web at : http://www.webdoyen.com
    Email me at : jeff@webdoyen.com
      

      
      
       
       

         
       

         
       
      





    源码下载:http://file.javaxxz.com/2014/10/1/223146156.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-27 05:31 , Processed in 0.459086 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表