温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java数据库连接池(经典)

发布时间:2020-08-11 19:44:19 来源:ITPUB博客 阅读:104 作者:liujian_jed 栏目:编程语言

转载自:http://langzixin.iteye.com/blog/808975

不错的案例,收藏起来,要不时间长了就找不到了

  1. // 唯一实例  
  2.     /** 
  3.     * 返回唯一实例.如果是第一次调用此方法,则创建实例 
  4.     * 
  5.     * @return DBConnectionManager 唯一实例 
  6.     */  
  7.     /** 
  8.     * 建构函数私有以防止其它对象创建本类实例 
  9.     */  
  10.     /** 
  11.     * 将连接对象返回给由名字指定的连接池 
  12.     * 
  13.     * @param name 在属性文件中定义的连接池名字 
  14.     * @param con 连接对象 
  15.     */  
  16.     /** 
  17.     * 获得一个可用的(空闲的)连接.如果没有可用连接,且已有连接数小于最大连接数 
  18.     * 限制,则创建并返回新连接 
  19.     * 
  20.     * @param name 在属性文件中定义的连接池名字 
  21.     * @return Connection 可用连接或null 
  22.     */  
  23.     /** 
  24.     * 获得一个可用连接.若没有可用连接,且已有连接数小于最大连接数限制, 
  25.     * 则创建并返回新连接.否则,在指定的时间内等待其它线程释放连接. 
  26.     * 
  27.     * @param name 连接池名字 
  28.     * @param time 以毫秒计的等待时间 
  29.     * @return Connection 可用连接或null 
  30.     */  
  31.     /** 
  32.     * 关闭所有连接,撤销驱动程序的注册 
  33.     */  
  34.     // 等待直到最后一个客户程序调用  
  35.     "撤销JDBC驱动程序 " + driver.getClass().getName()+"的注册");  
  36.     }  
  37.     "无法撤销下列JDBC驱动程序的注册: " + driver.getClass().getName());  
  38.     }  
  39.     }  
  40.     }  
  41.   
  42.     /** 
  43.     * 根据指定属性创建连接池实例. 
  44.     * 
  45.     * @param props 连接池属性 
  46.     */  
  47.     ".url")) {  
  48.     String poolName = name.substring("."));  
  49.     String url = props.getProperty(poolName + ".url");  
  50.     "没有为连接池" + poolName + "指定URL");  
  51.     ".user");  
  52.     String password = props.getProperty(poolName + ".password");  
  53.     String maxconn = props.getProperty(poolName + ".maxconn""0");  
  54.        
  55.     "错误的最大连接数限制: " + maxconn + " .连接池: " + poolName);  
  56.     max = "成功创建连接池" + poolName);  
  57.     }  
  58.     }  
  59.     }  
  60.   
  61.     /** 
  62.     * 读取属性完成初始化 
  63.     */  
  64.     "/db.properties");  
  65.     Properties dbProps = "读取数据成功!");  
  66.     }  
  67.     "不能读取属性文件. " +  
  68.     "请确保db.properties在CLASSPATH指定的路径中");  
  69.     "logfile""DBConnectionManager.log");  
  70.     System.out.print(logFile);  
  71.     "无法打开日志文件: " + logFile);  
  72.     log = /** 
  73.     * 装载和注册所有JDBC驱动程序 
  74.     * 
  75.     * @param props 属性 
  76.     */  
  77.     "drivers");  
  78.     StringTokenizer st = "成功注册JDBC驱动程序" + driverClassName);  
  79.     }  
  80.     "无法注册JDBC驱动程序: " +  
  81.     driverClassName + ", 错误: " + e);  
  82.     }  
  83.     }  
  84.     }  
  85.   
  86.     /** 
  87.     * 将文本信息写入日志文件 
  88.     */  
  89.     ": " + msg);  
  90.     }  
  91.   
  92.     /** 
  93.     * 将文本信息与异常写入日志文件 
  94.     */  
  95.     ": " + msg);  
  96.     e.printStackTrace(log);  
  97.     }  
  98.   
  99.     /** 
  100.     * 此内部类定义了一个连接池.它能够根据要求创建新连接,直到预定的最 
  101.     * 大连接数为止.在返回连接给客户程序之前,它能够验证连接的有效性. 
  102.     */  
  103.     /** 
  104.     * 创建新的连接池 
  105.     * 
  106.     * @param name 连接池名字 
  107.     * @param URL 数据库的JDBC URL 
  108.     * @param user 数据库帐号,或 null 
  109.     * @param password 密码,或 null 
  110.     * @param maxConn 此连接池允许建立的最大连接数 
  111.     */  
  112.     /** 
  113.     * 将不再使用的连接返回给连接池 
  114.     * 
  115.     * @param con 客户程序释放的连接 
  116.     */  
  117.     // 将指定连接加入到向量末尾  
  118.     freeConnections.addElement(con);  
  119.     checkedOut--;  
  120.     notifyAll();  
  121.     }  
  122.   
  123.     /** 
  124.     * 从连接池获得一个可用连接.如没有空闲的连接且当前连接数小于最大连接 
  125.     * 数限制,则创建新连接.如原来登记为可用的连接不再有效,则从向量删除之, 
  126.     * 然后递归调用自己以尝试新的可用连接. 
  127.     */  
  128.     // 获取向量中第一个可用连接  
  129.     con = (Connection) freeConnections.firstElement();  
  130.     freeConnections.removeElementAt("从连接池" + name+"删除一个无效连接");  
  131.     // 递归调用自己,尝试再次获取可用连接  
  132.     con = getConnection();  
  133.     }  
  134.     }  
  135.     "从连接池" + name+"删除一个无效连接");  
  136.     // 递归调用自己,尝试再次获取可用连接  
  137.     con = getConnection();  
  138.     }  
  139.     }  
  140.     /** 
  141.     * 从连接池获取可用连接.可以指定客户程序能够等待的最长时间 
  142.     * 参见前一个getConnection()方法. 
  143.     * 
  144.     * @param timeout 以毫秒计的等待时间限制 
  145.     */  
  146.     // wait()返回的原因是超时  
  147.     /** 
  148.     * 关闭所有连接 
  149.     */  
  150.     "关闭连接池" + name+"中的一个连接");  
  151.     }  
  152.     "无法关闭连接池" + name+"中的连接");  
  153.     }  
  154.     }  
  155.     freeConnections.removeAllElements();  
  156.     }  
  157.   
  158.     /** 
  159.     * 创建新的连接 
  160.     */  
  161.     "连接池" + name+"创建一个新的连接");  
  162.     }  
  163.     "无法创建下列URL的连接: " + URL);  
  164.     
  165. <%@ page language="java" "java.util.*" pageEncoding="UTF-8"%>  
  166. <%@ page "java.sql.*"%>  
  167. <%@ page "myDB.DBConnectionManager"%>  
  168. <%  
  169. String path = request.getContextPath();  
  170. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  171. %>  
  172.   
  173. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  174. <html>  
  175.     <head>  
  176.         <base href="<%=basePath%>">  
  177.   
  178.         <title>My JSP 'index.jsp' starting page</title>  
  179.         <meta http-equiv="pragma" content="no-cache">  
  180.         <meta http-equiv="cache-control" content="no-cache">  
  181.         <meta http-equiv="expires" content="0">  
  182.         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  183.         <meta http-equiv="description" content="This is my page">  
  184.         <!--  
  185.     <link rel="stylesheet" type="text/css" href="styles.css">  
  186.     -->  
  187.     </head>  
  188.   
  189.     <body>  
  190.         This is my JSP page.  
  191.         <br>  
  192. <%  
  193. //单例模式,只是在第一次调用该方法时生成实例,在服务器中该类只保存一个实例  
  194.   out.println("JAVA连接池类测试<br/>");  
  195.   Connection con = dbM.getConnection("mysqldb");//mysqldb是db.properties配置文件中设置的  
  196.   out.print(dbM.getClient());  
  197.   "不能获取数据库连接.");  
  198.    //dbM.release();  
  199.   }  
  200.   "select * from first_table");  
  201.    "<br/>");  
  202.    }  
  203.    rs.close();  
  204.    stmt.close();  
  205.    dbM.freeConnection("mysqldb", con);  
  206.      
  207.   }  
  208.     
  209.   Connection oraclecon = dbM.getConnection("oracledb");//oracledb是db.properties配置文件中设置的  
  210.   out.print(dbM.getClient());  
  211.   "不能获取数据库连接.");  
  212.    //dbM.release();  
  213.   }  
  214.   "select * from on_campus_student where rownum<=5");  
  215.    "<br/>");  
  216.    }  
  217.    rs.close();  
  218.    stmt.close();  
  219.    dbM.freeConnection("oracledb", oraclecon);  
  220.      
  221.   }  
  222.  } catch (Exception ee)  
  223.  {  
  224.   out.print(ee.getMessage());  
  225.  }  
  226. %>  
  227.     </body>  
  228. </html>  

 7、可以方便的获得各种数据库连接(预先在db.properties中配置) 

 

 com.mysql.jdbc.Driver oracle.jdbc.driver.OracleDriver

 

 不同数据库之间用空格隔开即可


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI