编译数据库连接池出错,不知什么原因,请大家帮忙看看,谢谢发,急!! 你仔细看看是不是在DBConnectionManager中少了一个大括号。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是你反编译的吧,DBConnectionManager的源代码呢,对照一下改改。 DBConnectionManager里面有些内部类,调用需要改改。 To: stonegump(龙飞虎) 对啊,我这个是反编译的,DBConnectionManager编译通过,下面的代码,你帮帮忙怎么改。package myshop;import java.io.*;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(); } 接上: public DBConnectionPool(String s, String s1, String s2, String s3, int i) { 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()); } } }} 我编译过了没有你说的错误。但是需要指明Date()是属于哪个包的。导入文件该为: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; To: little0416(天涯海角) 我是第一贴子那个代码有错误。 我看过代码了。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(); }修改都是类似的,相信你很容易搞定。 用纯java驱动连接数据库,为什么会不成功? 如何初始化jtree? New IO 读写文件,编码出现问题! 有熟悉ofbiz的吗,快帮我一下很急!!! 基本类型中的boolean占用多少内存? 那位高人解释这个hashCode()为什么要这么写? 如何从oracle数据库中的表生成一个XML文件??使用Java实现。 请问怎么把java程序封装成在windows下可以双击运行的文件? package city;产生了好多目录,真弄不明白?????????? 问一个关于对话框的问题 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();
}修改都是类似的,相信你很容易搞定。