关于Servlet控制数据库连接池的问题
我用Servlet写了一个数据库连接池的管理。在读取属性文件的时候老是出错。呵呵。。请大侠帮忙看看。
管理类的代码
import java.sql.*;
import java.util.*;
import java.util.Date;
import java.io.*;
public class DBConnectionManager {
static private DBConnectionManager instance;
static private int clients;
private Vector driverlist = new Vector();
private HashMap pools = new HashMap();
String poolname;
static synchronized public DBConnectionManager getInstance(){
if(instance == null){
instance = new DBConnectionManager();
}
return instance;
}
private DBConnectionManager(){
init();
}
private void init(){
InputStream is = getClass().getResourceAsStream( "/db.properties ");
Properties dbProp = new Properties();
try{
dbProp.load(is);
}catch(Exception e){
e.printStackTrace();
return;
}
LoadDrivers(dbProp);
creatPools(dbProp);
}
private void LoadDrivers(Properties Prop){
String driverClass = Prop.getProperty( "driver ");
StringTokenizer st = new StringTokenizer(driverClass);
while(st.hasMoreElements()){
String driverClassName = st.nextToken().trim();
try{
Driver driver = (Driver)Class.forName(driverClassName).newInstance();
DriverManager.registerDriver(driver);
driverlist.addElement(driver);
}catch(Exception e){
e.printStackTrace();
}
}
}
private void creatPools(Properties Prop){
Enumeration propNames = Prop.propertyNames();
while(propNames.hasMoreElements()){
String name = (String)propNames.nextElement();
if(name.endsWith( ".url ")){
poolname = name.substring(0,name.lastIndexOf( ". "));
String url = Prop.getProperty(poolname+ ".url ");
if(url==null){
continue;
}
String user = Prop.getProperty(poolname+ ".user ");
String pwd = Prop.getProperty(poolname+ ".pwd ");
String maxConn = Prop.getProperty(poolname+ ".maxConn ");
int max ;
try{
max = Integer.parseInt(maxConn);
}catch(NumberFormatException e){
e.printStackTrace();
max = 0;
}
DBConnectionPool pool = new DBConnectionPool(poolname,url,user,pwd,max);
pools.put(poolname,pool);
}
}
}
public Connection getConnection(String poolname){
DBConnectionPool pool = (DBConnectionPool)pools.get(poolname);
if(pool!=null){
return pool.getConnection();
}
return null;
}
public Connection getConnection(String poolname,long time){
DBConnectionPool pool = (DBConnectionPool)pools.get(poolname);
if(pool!=null){
return pool.getConnection(time);
}
return null;
}
public synchronized void release(){
if(--clients!=0){
return;
}
while(pools.isEmpty()){
DBConnectionPool pool = (DBConnectionPool)pools.get(poolname);
pool.release();
}
Enumeration allDrivers = driverlist.elements();
while(allDrivers.hasMoreElements()){
Driver driver = (Driver)allDrivers.nextElement();
try{
DriverManager.deregisterDriver(driver);
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
属性文件的代码
drivers=sun.jdbc.odbc.JdbcOdbcDriver jdbc.idbDriver
idb.url=127.0.0.1:8080
idb.maxConn=2
idb.url=jdbc:odbc:LocalServer
idb.user=sa
idb.pwd=
出错提示
java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:365)
at java.util.Properties.load(Properties.java:293)
at aa.DBConnectionManager.init(DBConnectionManager.java:27)
at aa.DBConnectionManager. <init> (DBConnectionManager.java:21)
at aa.DBConnectionManager.getInstance(DBConnectionManager.java:16)
at aa.Servlet1.init(Servlet1.java:14)
at javax.servlet.GenericServlet.init(GenericServlet.java:211)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1091)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:750)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:130)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
[解决办法]
好像是配置文件的路径不太对
你把那个配置文件写成绝对路径试一下
[解决办法]
InputStream is = getClass().getResourceAsStream( "/db.properties ");
改成
InputStream is = getClass().getResourceAsStream( "db.properties ");
然后把这个db.properties跟编译好的class文件放到同一个目录下试试