jdbc.java

来自「一、 网站名称:旅游信息管理网站 二、 编程语言:JSP+javabean+s」· Java 代码 · 共 69 行

JAVA
69
字号
package jdbc;

import java.sql.*;
import java.util.*;

public class jdbc
{
  private int inuse=0;
  private int max=999;
  private Vector connections=new Vector();

  public synchronized void releaseConnection(Connection con)
  {
    connections.addElement(con);
    inuse--;
  }
 
  public synchronized Connection getConnection()
  {
    Connection con=null;
    if(connections.size()>0)
     {
       con=(Connection) connections.elementAt(0);
       connections.removeElementAt(0);
       try{
       if(con.isClosed())
       {
         con=getConnection();
       }
       }catch(SQLException e){}
     }
     else if(max==0||inuse<max)
      {
         con=newConnection();
      }
     if(con!=null)
      {inuse++;}
   return con;    
  }
  
  private Connection newConnection()
  {
    Connection con=null;
    try
    {
      Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    }catch(Exception e){}
    try
    {
      con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=tour","tour","wushili");
    }catch(SQLException e)
     {
      e.printStackTrace();
      return null;
     }
    return con;  
  }
  
  public synchronized void closeCon()
  {
    Enumeration allConnections=connections.elements();
    while(allConnections.hasMoreElements())
    {
      Connection con=(Connection)allConnections.nextElement();
      try{con.close();}catch(SQLException e){}
    }
    connections.removeAllElements();
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?