package com.sl.java.jdbc;import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.Vector;//import com.sl.java.bean.Student;public class Test {
private static Connection conn;
private Test(){

}
private static Test sdao=new  Test(); 
public static  Test getSdao(){
return sdao;
}
public static Vector<Connection> pool=new Vector<Connection>();
private static String driver;
private static String url;
private static String user;
    private static String password;
private static int current_num=0;
private static int MAX_SIZE = 100;
static{
Properties per=new Properties();
try{
per.load(Test.class.getResourceAsStream("pool.per"));
driver=per.getProperty("driver");
url=per.getProperty("url");
user=per.getProperty("user");
password=per.getProperty("password");
MAX_SIZE=Integer.parseInt(per.getProperty("MAX_SIZE"));
Class.forName(driver);
}catch(IOException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
private void init(){
try{
for(int i=0;i<MAX_SIZE;i++){
pool.add(createConnection());
}
}catch(SQLException e){
e.printStackTrace();
}
}
private static Connection createConnection() throws SQLException {
conn=DriverManager.getConnection(url,user,password);
return conn;
}
public synchronized Connection getConnection() {

if (pool.size() == 0) {// 第一个用户
// CPU切换到另一个线程
init();
}
conn = pool.firstElement();
pool.removeElementAt(0);
current_num++;
System.out.println("当前连接数目为:"+current_num+",连接池对象为:"+pool.size());
return conn;
}
public void callback(Connection con) {
if (con != null) {
pool.add(con);
current_num--;
}
}
public void release() {
for (int i = 0; i < pool.size(); i++) {
pool.removeElementAt(0);
}
}
public static void main(String[] args){
sdao.getConnection();
}
}

解决方案 »

  1.   

    肯定是没编译, 是在Eclipse集成开发环境吧 ? 看看你的JDK配置
      

  2.   

    配置下环境变量。用myeclipse偶尔会出现这种情况
      

  3.   

    Eclipse会报java.lang.ExceptionInInitializerError,提示main线程有异常
      

  4.   

    配置环境变量
    如果是用javac编译加上路径名
      

  5.   

    直接运行的,只添加一个MAIN方法可以,但是创建连接池就有问题
      

  6.   

    谢谢楼上各位,问题找到了。我把文件名写错了per.load(Test.class.getResourceAsStream("pool.per"));
      

  7.   

    应该是你的main函数中使用Test.class有问题这里取不到
    per.load(Test.class.getResourceAsStream("pool.per"));