`
qcyycom
  • 浏览: 181947 次
社区版块
存档分类
最新评论

java程序员从笨鸟到菜鸟之(七)一—java数据库操作

    博客分类:
  • java
阅读更多

更多信息请查看 java进阶网 http://www.javady.com


 数据库访问几乎每一个稍微成型的程序都要用到的知识,怎么高效的访问数据库也是我们学习的一个重点,今天的任务就是总结
java访问数据库的方法和有关APIjava访问数据库主要用的方法是JDBC,是java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法,下面我们就具体来总结一下JDBC

一:Java访问数据库的具体步骤:

加载(注册)数据库 

 驱动加载就是把各个数据库提供的访问数据库的API加载到我们程序进来,加载JDBC驱动,并将其注册到DriverManager中,每一种数据库提供的数据库驱动不一样,加载驱动时要把jar包添加到lib文件夹下,下面看一下一些主流数据库的JDBC驱动加裁注册的代码

//Oracle8/8i/9iO数据库(thin模式

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); 

//Sql Server7.0/2000数据库   Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 

//Sql Server2005/2008数据库   Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 

//DB2数据库 

Class.froName("com.ibm.db2.jdbc.app.DB2Driver").newInstance();  

//MySQL数据库  Class.forName("com.mysql.jdbc.Driver").newInstance(); 

建立链接   

建立数据库之间的连接是访问数据库的必要条件,就像南水北调调水一样,要想调水首先由把沟通的河流打通。建立连接对于不同数据库也是不一样的,下面看一下一些主流数据库建立数据库连接,取得Connection对象的不同方式:

 //Oracle8/8i/9i数据库(thin模式

  String url="jdbc:oracle:thin:@localhost:1521:orcl"; 

  String user="scott"; 

  String password="tiger"; 

  Connection conn=DriverManager.getConnection(url,user,password); 

  

  //Sql Server7.0/2000/2005/2008数据库 

  String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs"; 

  String user="sa"; 

  String password=""; 

  Connection conn=DriverManager.getConnection(url,user,password); 

  

  //DB2数据库 

  String url="jdbc:db2://localhost:5000/sample"; 

  String user="amdin" 

  String password=-""; 

  Connection conn=DriverManager.getConnection(url,user,password); 

  

//MySQL数据库 

String url="jdbc:mysql://localhost:3306/testDB?user=root&password=root&useUnicode=true&characterEncoding=gb2312"; 

Connection conn=DriverManager.getConnection(url); 

3. 执行SQL语句  

数据库连接建立好之后,接下来就是一些准备工作和执行sql语句了,准备工作要做的就是建立Statement对象PreparedStatement对象,例如:

 //建立Statement对象 

 Statement stmt=conn.createStatement(); 

 //建立PreparedStatement对象 

 String sql="select * from user where userName=? and password=?"; 

  PreparedStatement pstmt=Conn.prepareStatement(sql); 

  pstmt.setString(1,"admin"); 

  pstmt.setString(2,"liubin"); 

做好准备工作之后就可以执行sql语句了,执行sql语句:

String sql="select * from users"; 

ResultSet rs=stmt.executeQuery(sql); 

//执行动态SQL查询 

ResultSet rs=pstmt.executeQuery(); 

//执行insert update delete等语句,先定义sql 

stmt.executeUpdate(sql); 

处理结果集  

 访问结果记录集ResultSet对象。例如: 

  while(rs.next) 

  { 

  out.println("你的第一个字段内容为:"+rs.getString("Name")); 

  out.println("你的第二个字段内容为:"+rs.getString(2)); 

  } 

关闭数据库 

 依次将ResultSetStatementPreparedStatementConnection对象关     闭,释放所占用的资源.例如

  rs.close(); 

  stmt.clost(); 

  pstmt.close(); 

  con.close(); 

二:JDBC事务

什么是事务:

首先,说说什么事务。我认为事务,就是一组操作数据库的动作集合。

事务是现代数据库理论中的核心概念之一。如果一组处理步骤或者全部发生或者一步也不执行,我们称该组处理步骤为一个事务。当所有的步骤像一个操 作一样被完整地执行,我们称该事务被提交。由于其中的一部分或多步执行失败,导致没有步骤被提交,则事务必须回滚到最初的系统状态。

事务必须服从ISO/IEC所制定的ACID原则。ACID是原子性(atomicity)、一致性(consistency)、隔离性 (isolation)和持久性(durability)的缩写。事务的原子性表示事务执行过程中的任何失败都将导致事务所做的任何修改失效。一致性表示 当事务执行失败时,所有被该事务影响的数据都应该恢复到事务执行前的状态。隔离性表示在事务执行过程中对数据的修改,在事务提交之前对其他事务不可见。持 久性表示当系统或介质发生故障时,确保已提交事务的更新不能丢失。持久性通过数据库备份和恢复来保证。

JDBC 事务是用 Connection 对象控制的。JDBC Connection 接口( java.sql.Connection )提供了两种事务模式:自动提交和手工提交。 java.sql.Connection 提供了以下控制事务的方法: 
public void setAutoCommit(boolean) 
public boolean getAutoCommit() 
public void commit() 
public void rollback() 
使用 
JDBC 事务界定时,您可以将多个 SQL 语句结合到一个事务中。JDBC 事务的一个缺点是事务的范围局限于一个数据库连接。一个 JDBC 事务不能跨越多个数据库。

 

三:java操作数据库连接池

在总结java操作数据库连接池发现一篇很好的文章,所以就不做具体总结了,直接上地址:

http://www.blogjava.net/chunkyo/archive/2007/01/16/94266.html

最后附一段比较经典的代码吧:

 


  1. import java.sql.Connection;  
  2. import java.sql.DatabaseMetaData;  
  3. import java.sql.Driver;  
  4. import java.sql.DriverManager;  
  5. import java.sql.SQLException;  
  6. import java.sql.Statement;  
  7. import java.util.Enumeration;  
  8. import java.util.Vector;  
  9. public class ConnectionPool {  
  10. private String jdbcDriver = ""// 数据库驱动  
  11. private String dbUrl = ""// 数据 URL  
  12. private String dbUsername = ""// 数据库用户名  
  13. private String dbPassword = ""// 数据库用户密码  
  14. private String testTable = ""// 测试连接是否可用的测试表名,默认没有测试表  
  15. private int initialConnections = 10// 连接池的初始大小  
  16. private int incrementalConnections = 5;// 连接池自动增加的大小  
  17. private int maxConnections = 50// 连接池最大的大小  
  18. private Vector connections = null// 存放连接池中数据库连接的向量 , 初始时为 null  
  19.   
  20. // 它中存放的对象为 PooledConnection 型  
  21.   
  22. /** 
  23. * 构造函数 
  24. * 
  25. * @param jdbcDriver String JDBC 驱动类串 
  26. * @param dbUrl String 数据库 URL 
  27. * @param dbUsername String 连接数据库用户名 
  28. * @param dbPassword String 连接数据库用户的密码 
  29. * 
  30. */  
  31.   
  32. public ConnectionPool(String jdbcDriver,String dbUrl,String dbUsername,String dbPassword) {  
  33.          this.jdbcDriver = jdbcDriver;  
  34.          this.dbUrl = dbUrl;  
  35.          this.dbUsername = dbUsername;   
  36.          this.dbPassword = dbPassword;  
  37. }  
  38.   
  39. /** 
  40.  
  41. * 返回连接池的初始大小 
  42. * 
  43. * @return 初始连接池中可获得的连接数量 
  44. */  
  45. public int getInitialConnections() {  
  46.   
  47.          return this.initialConnections;  
  48. }  
  49.   
  50. /** 
  51.  
  52. * 设置连接池的初始大小 
  53.  
  54. * 
  55.  
  56. * @param 用于设置初始连接池中连接的数量 
  57.  
  58. */  
  59.   
  60. public void setInitialConnections(int initialConnections) {  
  61.          this.initialConnections = initialConnections;  
  62. }  
  63.   
  64. /** 
  65.  
  66. * 返回连接池自动增加的大小 、 
  67. * 
  68. * @return 连接池自动增加的大小 
  69. */  
  70. public int getIncrementalConnections() {  
  71.   
  72.          return this.incrementalConnections;  
  73.   
  74. }  
  75.   
  76. /** 
  77. * 设置连接池自动增加的大小 
  78. * @param 连接池自动增加的大小 
  79. */  
  80.   
  81. public void setIncrementalConnections(int incrementalConnections) {  
  82.   
  83.          this.incrementalConnections = incrementalConnections;  
  84.   
  85. }  
  86.   
  87. /** 
  88. * 返回连接池中最大的可用连接数量 
  89. * @return 连接池中最大的可用连接数量 
  90. */  
  91.   
  92. public int getMaxConnections() {  
  93.          return this.maxConnections;  
  94. }  
  95.   
  96. /** 
  97.  
  98. * 设置连接池中最大可用的连接数量 
  99.  
  100. * 
  101.  
  102. * @param 设置连接池中最大可用的连接数量值 
  103.  
  104. */  
  105.   
  106. public void setMaxConnections(int maxConnections) {  
  107.   
  108.          this.maxConnections = maxConnections;  
  109.   
  110. }  
  111.   
  112. /** 
  113.  
  114. * 获取测试数据库表的名字 
  115. * 
  116. * @return 测试数据库表的名字 
  117. */  
  118. public String getTestTable() {  
  119.   
  120.          return this.testTable;  
  121.   
  122. }  
  123.   
  124. /** 
  125. * 设置测试表的名字 
  126. * @param testTable String 测试表的名字 
  127. */  
  128. public void setTestTable(String testTable) {  
  129.          this.testTable = testTable;  
  130. }  
  131.   
  132. /** 
  133.  
  134. * 
  135. * 创建一个数据库连接池,连接池中的可用连接的数量采用类成员 
  136. * initialConnections 中设置的值 
  137. */  
  138. public synchronized void createPool() throws Exception {  
  139.   
  140.          // 确保连接池没有创建  
  141.     
  142.          // 如果连接池己经创建了,保存连接的向量 connections 不会为空  
  143.     
  144.          if (connections != null) {  
  145.     
  146.           return// 如果己经创建,则返回  
  147.     
  148.          }  
  149.     
  150.          // 实例化 JDBC Driver 中指定的驱动类实例  
  151.     
  152.          Driver driver = (Driver) (Class.forName(this.jdbcDriver).newInstance());  
  153.     
  154.          DriverManager.registerDriver(driver); // 注册 JDBC 驱动程序  
  155.     
  156.          // 创建保存连接的向量 , 初始时有 0 个元素  
  157.     
  158.          connections = new Vector();  
  159.     
  160.          // 根据 initialConnections 中设置的值,创建连接。  
  161.     
  162.          createConnections(this.initialConnections);  
  163.     
  164.          System.out.println(" 数据库连接池创建成功! ");  
  165.   
  166. }  
  167.   
  168. /** 
  169.  
  170. * 创建由 numConnections 指定数目的数据库连接 , 并把这些连接 
  171.  
  172. * 放入 connections 向量中 
  173. * 
  174. * @param numConnections 要创建的数据库连接的数目 
  175. */  
  176. @SuppressWarnings("unchecked")  
  177. private void createConnections(int numConnections) throws SQLException {  
  178.   
  179.          // 循环创建指定数目的数据库连接  
  180.     
  181.          for (int x = 0; x < numConnections; x++) {  
  182.     
  183.           // 是否连接池中的数据库连接的数量己经达到最大?最大值由类成员 maxConnections  
  184.      
  185.           // 指出,如果 maxConnections 为 0 或负数,表示连接数量没有限制。  
  186.      
  187.           // 如果连接数己经达到最大,即退出。  
  188.      
  189.           if (this.maxConnections > 0 && this.connections.size() >= this.maxConnections) {  
  190.      
  191.            break;  
  192.      
  193.           }  
  194.     
  195.           //add a new PooledConnection object to connections vector  
  196.      
  197.           // 增加一个连接到连接池中(向量 connections 中)  
  198.      
  199.           try{  
  200.      
  201.            connections.addElement(new PooledConnection(newConnection()));  
  202.      
  203.           }catch(SQLException e){  
  204.      
  205.            System.out.println(" 创建数据库连接失败! "+e.getMessage());  
  206.      
  207.           throw new SQLException();  
  208.      
  209.           }  
  210.      
  211.           System.out.println(" 数据库连接己创建 ......");  
  212.      
  213.          }  
  214. }  
  215.   
  216. /** 
  217.  
  218. * 创建一个新的数据库连接并返回它 
  219. * 
  220. * @return 返回一个新创建的数据库连接 
  221. */  
  222. private Connection newConnection() throws SQLException {  
  223.   
  224.          // 创建一个数据库连接  
  225.     
  226.          Connection conn = DriverManager.getConnection(dbUrl, dbUsername, dbPassword);  
  227.     
  228.          // 如果这是第一次创建数据库连接,即检查数据库,获得此数据库允许支持的  
  229.     
  230.          // 最大客户连接数目  
  231.     
  232.          //connections.size()==0 表示目前没有连接己被创建  
  233.     
  234.          if (connections.size() == 0) {  
  235.     
  236.           DatabaseMetaData metaData = conn.getMetaData();  
  237.      
  238.           int driverMaxConnections = metaData.getMaxConnections();  
  239.      
  240.           // 数据库返回的 driverMaxConnections 若为 0 ,表示此数据库没有最大  
  241.      
  242.           // 连接限制,或数据库的最大连接限制不知道  
  243.      
  244.           //driverMaxConnections 为返回的一个整数,表示此数据库允许客户连接的数目  
  245.      
  246.           // 如果连接池中设置的最大连接数量大于数据库允许的连接数目 , 则置连接池的最大  
  247.      
  248.           // 连接数目为数据库允许的最大数目  
  249.      
  250.           if (driverMaxConnections > 0 && this.maxConnections > driverMaxConnections) {  
  251.      
  252.            this.maxConnections = driverMaxConnections;  
  253.      
  254.           }  
  255.          }   
  256.          return conn; // 返回创建的新的数据库连接  
  257.   
  258. }  
  259.   
  260. /** 
  261.  
  262. * 通过调用 getFreeConnection() 函数返回一个可用的数据库连接 , 
  263.  
  264. * 如果当前没有可用的数据库连接,并且更多的数据库连接不能创 
  265.  
  266. * 建(如连接池大小的限制),此函数等待一会再尝试获取。 
  267.  
  268. * 
  269.  
  270. * @return 返回一个可用的数据库连接对象 
  271.  
  272. */  
  273.   
  274. public synchronized Connection getConnection() throws SQLException {  
  275.   
  276.          // 确保连接池己被创建  
  277.     
  278.          if (connections == null) {  
  279.     
  280.           return null// 连接池还没创建,则返回 null  
  281.     
  282.          }  
  283.     
  284.          Connection conn = getFreeConnection(); // 获得一个可用的数据库连接  
  285.     
  286.          // 如果目前没有可以使用的连接,即所有的连接都在使用中  
  287.     
  288.          while (conn == null){  
  289.     
  290.           // 等一会再试  
  291.      
  292.           wait(250);  
  293.      
  294.           conn = getFreeConnection(); // 重新再试,直到获得可用的连接,如果  
  295.      
  296.           //getFreeConnection() 返回的为 null  
  297.      
  298.           // 则表明创建一批连接后也不可获得可用连接  
  299.     
  300.          }  
  301.     
  302.          return conn;// 返回获得的可用的连接  
  303. }  
  304.   
  305. /** 
  306.  
  307. * 本函数从连接池向量 connections 中返回一个可用的的数据库连接,如果 
  308.  
  309. * 当前没有可用的数据库连接,本函数则根据 incrementalConnections 设置 
  310.  
  311. * 的值创建几个数据库连接,并放入连接池中。 
  312.  
  313. * 如果创建后,所有的连接仍都在使用中,则返回 null 
  314.  
  315. * @return 返回一个可用的数据库连接 
  316.  
  317. */  
  318.   
  319. private Connection getFreeConnection() throws SQLException {  
  320.   
  321.          // 从连接池中获得一个可用的数据库连接  
  322.     
  323.          Connection conn = findFreeConnection();  
  324.     
  325.          if (conn == null) {  
  326.     
  327.           // 如果目前连接池中没有可用的连接  
  328.      
  329.           // 创建一些连接  
  330.      
  331.           createConnections(incrementalConnections);  
  332.      
  333.           // 重新从池中查找是否有可用连接  
  334.      
  335.           conn = findFreeConnection();  
  336.      
  337.           if (conn == null) {  
  338.      
  339.            // 如果创建连接后仍获得不到可用的连接,则返回 null  
  340.       
  341.            return null;  
  342.      
  343.           }  
  344.     
  345.          }  
  346.     
  347.          return conn;  
  348.   
  349. }  
  350.   
  351. /** 
  352.  
  353. * 查找连接池中所有的连接,查找一个可用的数据库连接, 
  354.  
  355. * 如果没有可用的连接,返回 null 
  356.  
  357. * 
  358.  
  359. * @return 返回一个可用的数据库连接 
  360.  
  361. */  
  362.   
  363. private Connection findFreeConnection() throws SQLException {  
  364.   
  365.          Connection conn = null;  
  366.     
  367.          PooledConnection pConn = null;  
  368.     
  369.          // 获得连接池向量中所有的对象  
  370.     
  371.          Enumeration enumerate = connections.elements();  
  372.     
  373.          // 遍历所有的对象,看是否有可用的连接  
  374.     
  375.          while (enumerate.hasMoreElements()) {  
  376.     
  377.           pConn = (PooledConnection) enumerate.nextElement();  
  378.      
  379.           if (!pConn.isBusy()) {  
  380.      
  381.            // 如果此对象不忙,则获得它的数据库连接并把它设为忙  
  382.       
  383.            conn = pConn.getConnection();  
  384.       
  385.            pConn.setBusy(true);  
  386.       
  387.            // 测试此连接是否可用  
  388.       
  389.            if (!testConnection(conn)) {  
  390.       
  391.             // 如果此连接不可再用了,则创建一个新的连接,  
  392.        
  393.             // 并替换此不可用的连接对象,如果创建失败,返回 null  
  394.        
  395.             try{  
  396.        
  397.              conn = newConnection();  
  398.        
  399.             }catch(SQLException e){  
  400.        
  401.              System.out.println(" 创建数据库连接失败! "+e.getMessage());  
  402.        
  403.              return null;  
  404.        
  405.             }  
  406.       
  407.             pConn.setConnection(conn);  
  408.       
  409.            }  
  410.       
  411.            break// 己经找到一个可用的连接,退出  
  412.      
  413.           }  
  414.     
  415.          }  
  416.     
  417.          return conn;// 返回找到到的可用连接  
  418.   
  419. }  
  420.   
  421. /** 
  422.  
  423. * 测试一个连接是否可用,如果不可用,关掉它并返回 false 
  424.  
  425. * 否则可用返回 true 
  426.  
  427. * 
  428.  
  429. * @param conn 需要测试的数据库连接 
  430.  
  431. * @return 返回 true 表示此连接可用, false 表示不可用 
  432.  
  433. */  
  434.   
  435. private boolean testConnection(Connection conn) {  
  436.   
  437.          try {  
  438.     
  439.           // 判断测试表是否存在  
  440.      
  441.           if (testTable.equals("")) {  
  442.      
  443.            // 如果测试表为空,试着使用此连接的 setAutoCommit() 方法  
  444.       
  445.            // 来判断连接否可用(此方法只在部分数据库可用,如果不可用 ,  
  446.       
  447.            // 抛出异常)。注意:使用测试表的方法更可靠  
  448.       
  449.            conn.setAutoCommit(true);  
  450.      
  451.           } else {// 有测试表的时候使用测试表测试  
  452.      
  453.            //check if this connection is valid  
  454.       
  455.            Statement stmt = conn.createStatement();  
  456.       
  457.            stmt.execute("select count(*) from " + testTable);  
  458.      
  459.           }  
  460.     
  461.          } catch (SQLException e) {  
  462.     
  463.           // 上面抛出异常,此连接己不可用,关闭它,并返回 false;  
  464.     
  465.           closeConnection(conn);  
  466.     
  467.           return false;  
  468.     
  469.          }  
  470.     
  471.          // 连接可用,返回 true  
  472.     
  473.          return true;  
  474.   
  475. }  
  476.   
  477. /** 
  478.  
  479. * 此函数返回一个数据库连接到连接池中,并把此连接置为空闲。 
  480.  
  481. * 所有使用连接池获得的数据库连接均应在不使用此连接时返回它。 
  482.  
  483. * 
  484.  
  485. * @param 需返回到连接池中的连接对象 
  486.  
  487. */  
  488.   
  489. public void returnConnection(Connection conn) {  
  490.   
  491.          // 确保连接池存在,如果连接没有创建(不存在),直接返回  
  492.     
  493.          if (connections == null) {  
  494.     
  495.           System.out.println(" 连接池不存在,无法返回此连接到连接池中 !");  
  496.      
  497.           return;  
  498.     
  499.          }  
  500.   
  501.          PooledConnection pConn = null;  
  502.     
  503.          Enumeration enumerate = connections.elements();  
  504.     
  505.          // 遍历连接池中的所有连接,找到这个要返回的连接对象  
  506.     
  507.          while (enumerate.hasMoreElements()) {  
  508.   
  509.           pConn = (PooledConnection) enumerate.nextElement();  
  510.      
  511.           // 先找到连接池中的要返回的连接对象  
  512.      
  513.           if (conn == pConn.getConnection()) {  
  514.      
  515.            // 找到了 , 设置此连接为空闲状态  
  516.       
  517.            pConn.setBusy(false);  
  518.       
  519.            break;  
  520.      
  521.           }  
  522.   
  523.          }  
  524.   
  525. }  
  526.   
  527. /** 
  528.  
  529. * 刷新连接池中所有的连接对象 
  530.  
  531. * 
  532.  
  533. */  
  534.   
  535. public synchronized void refreshConnections() throws SQLException {  
  536.   
  537.          // 确保连接池己创新存在  
  538.     
  539.          if (connections == null) {  
  540.     
  541.           System.out.println(" 连接池不存在,无法刷新 !");  
  542.     
  543.           return;  
  544.     
  545.          }  
  546.     
  547.          PooledConnection pConn = null;  
  548.     
  549.          Enumeration enumerate = connections.elements();  
  550.     
  551.          while (enumerate.hasMoreElements()) {  
  552.   
  553.           // 获得一个连接对象  
  554.      
  555.           pConn = (PooledConnection) enumerate.nextElement();  
  556.      
  557.           // 如果对象忙则等 5 秒 ,5 秒后直接刷新  
  558.      
  559.           if (pConn.isBusy()) {  
  560.      
  561.            wait(5000); // 等 5 秒  
  562.      
  563.           }  
  564.      
  565.           // 关闭此连接,用一个新的连接代替它。  
  566.      
  567.           closeConnection(pConn.getConnection());  
  568.      
  569.           pConn.setConnection(newConnection());  
  570.      
  571.           pConn.setBusy(false);  
  572.      
  573.          }  
  574.   
  575. }  
  576.   
  577. /** 
  578.  
  579. * 关闭连接池中所有的连接,并清空连接池。 
  580.  
  581. */  
  582.   
  583. public synchronized void closeConnectionPool() throws SQLException {  
  584.   
  585.          // 确保连接池存在,如果不存在,返回  
  586.     
  587.          if (connections == null) {  
  588.     
  589.           System.out.println(" 连接池不存在,无法关闭 !");  
  590.     
  591.           return;  
  592.     
  593.          }  
  594.     
  595.          PooledConnection pConn = null;  
  596.     
  597.          Enumeration enumerate = connections.elements();  
  598.     
  599.          while (enumerate.hasMoreElements()) {  
  600.     
  601.           pConn = (PooledConnection) enumerate.nextElement();  
  602.     
  603.           // 如果忙,等 5 秒  
  604.     
  605.           if (pConn.isBusy()) {  
  606.     
  607.            wait(5000); // 等 5 秒  
  608.     
  609.           }  
  610.     
  611.          //5 秒后直接关闭它  
  612.     
  613.          closeConnection(pConn.getConnection());  
  614.     
  615.          // 从连接池向量中删除它  
  616.     
  617.          connections.removeElement(pConn);  
  618.     
  619.          }  
  620.     
  621.          // 置连接池为空  
  622.     
  623.          connections = null;  
  624.   
  625. }  
  626.   
  627. /** 
  628.  
  629. * 关闭一个数据库连接 
  630.  
  631. * 
  632.  
  633. * @param 需要关闭的数据库连接 
  634.  
  635. */  
  636.   
  637. private void closeConnection(Connection conn) {  
  638.   
  639.          try {  
  640.     
  641.           conn.close();  
  642.     
  643.          }catch (SQLException e) {  
  644.     
  645.           System.out.println(" 关闭数据库连接出错: "+e.getMessage());  
  646.     
  647.          }  
  648.   
  649. }  
  650.   
  651. /** 
  652.  
  653. * 使程序等待给定的毫秒数 
  654.  
  655. * 
  656.  
  657. * @param 给定的毫秒数 
  658.  
  659. */  
  660.   
  661. private void wait(int mSeconds) {  
  662.   
  663.          try {  
  664.     
  665.           Thread.sleep(mSeconds);  
  666.     
  667.          } catch (InterruptedException e) {  
  668.     
  669.          }  
  670.   
  671. }  
  672.   
  673. /** 
  674.  
  675. * 
  676.  
  677. * 内部使用的用于保存连接池中连接对象的类 
  678.  
  679. * 此类中有两个成员,一个是数据库的连接,另一个是指示此连接是否 
  680.  
  681. * 正在使用的标志。 
  682.  
  683. */  
  684.   
  685. class PooledConnection {  
  686.   
  687.          Connection connection = null;// 数据库连接  
  688.     
  689.          boolean busy = false// 此连接是否正在使用的标志,默认没有正在使用  
  690.     
  691.          // 构造函数,根据一个 Connection 构告一个 PooledConnection 对象  
  692.     
  693.          public PooledConnection(Connection connection) {  
  694.     
  695.           this.connection = connection;  
  696.     
  697.          }  
  698.     
  699.          // 返回此对象中的连接  
  700.     
  701.          public Connection getConnection() {  
  702.     
  703.           return connection;  
  704.   
  705.          }  
  706.   
  707.          // 设置此对象的,连接  
  708.     
  709.          public void setConnection(Connection connection) {  
  710.     
  711.           this.connection = connection;  
  712.      
  713.          }  
  714.      
  715.          // 获得对象连接是否忙  
  716.      
  717.          public boolean isBusy() {  
  718.      
  719.           return busy;  
  720.      
  721.          }  
  722.      
  723.          // 设置对象的连接正在忙  
  724.      
  725.          public void setBusy(boolean busy) {  
  726.      
  727.           this.busy = busy;  
  728.      
  729.          }  
  730.   
  731. }  
  732.   
  733. }  
  734.   
  735. =======================================  
  736.   
  737. 这个例子是根据POSTGRESQL数据库写的,  
  738. 请用的时候根据实际的数据库调整。  
  739.   
  740. 调用方法如下:  
  741.   
  742. ① ConnectionPool connPool  
  743.                                      = new ConnectionPool("org.postgresql.Driver"  
  744.                                                                          ,"jdbc:postgresql://dbURI:5432/DBName"  
  745.                                                                          ,"postgre"  
  746.                                                                          ,"postgre");  
  747.   
  748. ② connPool .createPool();  
  749.   Connection conn = connPool .getConnection();  
2
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics