📄 jdbc.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -