你仔细看看是不是在DBConnectionManager中少了一个大括号。
解决方案 »
- 关于javax.media.*
- 门外汉求助!!!用myeclipse8.5把程序打成jar包,有登陆界面,之后就没有显示了,求救!
- 关于创建Singleton模式
- 一个初学者配置JAVA环境的疑惑!
- 求救:如何从控制台退出程序
- 关于当前时间的菜鸟问题,求救啊!!!
- laughsmile(海边的星空)和test1234()请进(偶给你俩个人加分。。。)
- 小弟初学java想装个jdk但是不知道怎么装和设置环境变量(在linux和win2000中) 请大虾们帮忙!!!
- oracle java.sql.SQLException: ORA-01000: 超出打开游标的最大数
- 20分只给一个人
- JDOM如何生成这样的根元素??
- 看一下撒
import java.sql.*;
import java.util.*;public class DBConnectionManager
{
class DBConnectionPool
{ private int checkedOut;
private Vector freeConnections;
private int maxConn;
private String name;
private String password;
private String URL;
private String user; public synchronized void freeConnection(Connection connection)
{
freeConnections.addElement(connection);
checkedOut--;
notifyAll();
} public synchronized Connection getConnection()
{
Connection connection = null;
if(freeConnections.size() > 0)
{
connection = (Connection)freeConnections.firstElement();
freeConnections.removeElementAt(0);
try
{
if(connection.isClosed())
{
log("从连接池" + name + "删除一个无效连接");
connection = getConnection();
}
}
catch(SQLException _ex)
{
log("从连接池" + name + "删除一个无效连接");
connection = getConnection();
}
} else
if(maxConn == 0 || checkedOut < maxConn)
connection = newConnection();
if(connection != null)
checkedOut++;
return connection;
} public synchronized Connection getConnection(long l)
{
long l1 = (new Date()).getTime();
Connection connection;
while((connection = getConnection()) == null)
{
try
{
wait(l);
}
catch(InterruptedException _ex) { }
if((new Date()).getTime() - l1 >= l)
return null;
}
return connection;
} private Connection newConnection()
{
Connection connection = null;
try
{
if(user == null)
connection = DriverManager.getConnection(URL);
else
connection = DriverManager.getConnection(URL, user, password);
log("连接池" + name + "创建一个新的连接");
}
catch(SQLException sqlexception)
{
log(sqlexception, "无法创建下列URL的连接: " + URL);
return null;
}
return connection;
} public synchronized void release()
{
for(Enumeration enumeration = freeConnections.elements(); enumeration.hasMoreElements();)
{
Connection connection = (Connection)enumeration.nextElement();
try
{
connection.close();
log("关闭连接池" + name + "中的一个连接");
}
catch(SQLException sqlexception)
{
log(sqlexception, "无法关闭连接池" + name + "中的连接");
}
} freeConnections.removeAllElements();
}
{
freeConnections = new Vector();
name = s;
URL = s1;
user = s2;
password = s3;
maxConn = i;
}
}
private static DBConnectionManager instance;
private static int clients;
private Vector drivers;
private PrintWriter log;
private Hashtable pools; private DBConnectionManager()
{
drivers = new Vector();
pools = new Hashtable();
init();
} private void createPools(Properties properties)
{
for(Enumeration enumeration = properties.propertyNames(); enumeration.hasMoreElements();)
{
String s = (String)enumeration.nextElement();
if(s.endsWith(".url"))
{
String s1 = s.substring(0, s.lastIndexOf("."));
String s2 = properties.getProperty(s1 + ".url");
if(s2 == null)
{
log("没有为连接池" + s1 + "指定URL");
} else
{
String s3 = properties.getProperty(s1 + ".user");
String s4 = properties.getProperty(s1 + ".password");
String s5 = properties.getProperty(s1 + ".maxconn", "0");
int i;
try
{
i = Integer.valueOf(s5).intValue();
}
catch(NumberFormatException _ex)
{
log("错误的最大连接数限制: " + s5 + " .连接池: " + s1);
i = 0;
}
DBConnectionPool dbconnectionpool = new DBConnectionPool(s1, s2, s3, s4, i);
pools.put(s1, dbconnectionpool);
log("成功创建连接池" + s1);
}
}
} } public void freeConnection(String s, Connection connection)
{
DBConnectionPool dbconnectionpool = (DBConnectionPool)pools.get(s);
if(dbconnectionpool != null)
dbconnectionpool.freeConnection(connection);
} public Connection getConnection(String s)
{
DBConnectionPool dbconnectionpool = (DBConnectionPool)pools.get(s);
if(dbconnectionpool != null)
return dbconnectionpool.getConnection();
else
return null;
} public Connection getConnection(String s, long l)
{
DBConnectionPool dbconnectionpool = (DBConnectionPool)pools.get(s);
if(dbconnectionpool != null)
return dbconnectionpool.getConnection(l);
else
return null;
} public static synchronized DBConnectionManager getInstance()
{
if(instance == null)
instance = new DBConnectionManager();
clients++;
return instance;
} private void init()
{
java.io.InputStream inputstream = getClass().getResourceAsStream("db.properties");
Properties properties = new Properties();
try
{
properties.load(inputstream);
}
catch(Exception _ex)
{
System.err.println("不能读取属性文件. 请确保db.properties在CLASSPATH指定的路径中");
return;
}
String s = properties.getProperty("logfile", "DBConnectionManager.log");
try
{
log = new PrintWriter(new FileWriter(s, true), true);
}
catch(IOException _ex)
{
System.err.println("无法打开日志文件: " + s);
log = new PrintWriter(System.err);
}
loadDrivers(properties);
createPools(properties);
} private void loadDrivers(Properties properties)
{
String s = properties.getProperty("drivers");
for(StringTokenizer stringtokenizer = new StringTokenizer(s); stringtokenizer.hasMoreElements();)
{
String s1 = stringtokenizer.nextToken().trim();
try
{
Driver driver = (Driver)Class.forName(s1).newInstance();
DriverManager.registerDriver(driver);
drivers.addElement(driver);
log("成功注册JDBC驱动程序" + s1);
}
catch(Exception exception)
{
log("无法注册JDBC驱动程序: " + s1 + ", 错误: " + exception);
}
} } private void log(String s)
{
log.println(String.valueOf(new Date()) + ": " + s);
} private void log(Throwable throwable, String s)
{
log.println(String.valueOf(new Date()) + ": " + s);
throwable.printStackTrace(log);
} public synchronized void release()
{
if(--clients != 0)
return;
DBConnectionPool dbconnectionpool;
for(Enumeration enumeration = pools.elements(); enumeration.hasMoreElements(); dbconnectionpool.release())
dbconnectionpool = (DBConnectionPool)enumeration.nextElement(); for(Enumeration enumeration1 = drivers.elements(); enumeration1.hasMoreElements();)
{
Driver driver = (Driver)enumeration1.nextElement();
try
{
DriverManager.deregisterDriver(driver);
log("撤销JDBC驱动程序 " + driver.getClass().getName() + "的注册");
}
catch(SQLException sqlexception)
{
log(sqlexception, "无法撤销下列JDBC驱动程序的注册: " + driver.getClass().getName());
}
} }
}
导入文件该为:
import java.io.*;
import java.sql.*;
import java.util.Date;
import java.util.Vector;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import java.util.StringTokenizer;
DBConnectionManager.access$0(DBConnectionManager.this, "关闭连接池" + name + "中的一个连接");
实际上是想调用DBConnectionManager的log方法,而log方法在DBConnectionManager中声明为private.
由于出错代码只是起记录日志的作用。所以可以把它们全注释掉。或者,要保留日志,按照下面的方法修改一下即可。
我不贴修改后的代码,太长。你照这个改一下就行:
DBConnectionManager:
将 private PrintWriter log;
改为 private static PrintWriter log;
将 private void log(String s)
{
log.println(String.valueOf(new Date()) + ": " + s);
} private void log(Throwable throwable, String s)
{
log.println(String.valueOf(new Date()) + ": " + s);
throwable.printStackTrace(log);
}
改为 public static void log(String s)
{
log.println(String.valueOf(new Date()) + ": " + s);
} public static void log(Throwable throwable, String s)
{
log.println(String.valueOf(new Date()) + ": " + s);
throwable.printStackTrace(log);
}DBConnectionPool:
将 try
{
connection.close();
DBConnectionManager.access$0(DBConnectionManager.this, "关闭连接池" + name + "中的一个连接");//放此处没用
}
catch(SQLException sqlexception)
{
DBConnectionManager.access$1(DBConnectionManager.this, sqlexception, "无法关闭连接池" + name + "中的连接");
}
改为
try
{
if(connection.isClosed())
{
connection = getConnection();
}
}
catch(SQLException _ex)
{
DBConnectionManager.log(_ex, "从连接池" + name + "删除一个无效连接");
connection = getConnection();
}修改都是类似的,相信你很容易搞定。