package mytest;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
class DBPoolManager
{ static private DBPoolManager instance;
static private int clients;
private Vector drivers=new Vector();
private Hashtable pools=new Hashtable();
private DBPoolManager()
{
init();
} static synchronized public DBPoolManager getInstance()
{
if (instance == null)
{
instance = new DBPoolManager();
}
clients++;
return instance;
}
private void init()
{
InputStream is = getClass().getResourceAsStream("/db.properties");
Properties dbProps = new Properties();
try
{
dbProps.load(is);
}
catch (Exception e)
{
System.err.println("Can not read the properties file. " + "Make sure db.properties is in the CLASSPATH");
return;
} loadDrivers(dbProps);
createPools(dbProps);
} private void loadDrivers(Properties props)
{
String driverClasses = props.getProperty("drivers");
StringTokenizer st = new StringTokenizer(driverClasses);
while (st.hasMoreElements())
{
String driverClassName = st.nextToken().trim();
try
{
Driver driver = (Driver)Class.forName(driverClassName).newInstance();
DriverManager.registerDriver(driver);
drivers.addElement(driver); }
catch (Exception e)
{
}
}
}
private void createPools(Properties props)
{
Enumeration propNames = props.propertyNames();
while (propNames.hasMoreElements())
{
String name = (String) propNames.nextElement();
if (name.endsWith(".url"))
{
String poolName = name.substring(0, name.lastIndexOf("."));
String url = props.getProperty(poolName + ".url");
if (url == null)
{
continue;
}
String user = props.getProperty(poolName + ".user");
String password = props.getProperty(poolName + ".password");
String maxconn = props.getProperty(poolName + ".maxconn", "0");
int max;
try
{
max = Integer.valueOf(maxconn).intValue();
}
catch (NumberFormatException e)
{
max = 0;
}
ConnectionPool pool = new ConnectionPool(poolName, url, user, password, max,5,30000000);
pools.put(poolName, pool); }
}
}
public Connection getConnection(String name)
{
Connection conn=null;
ConnectionPool cp=(ConnectionPool)pools.get(name);
if(cp!=null)
{
try{
conn=cp.getConnection();
}
catch(Exception e)
{}
}
return conn;
}
public void freeConnection(String name,Connection conn)
{
ConnectionPool pool=(ConnectionPool)pools.get(name);
if(pool!=null)
{
pool.freeConnection(conn);
}
}
}
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
class DBPoolManager
{ static private DBPoolManager instance;
static private int clients;
private Vector drivers=new Vector();
private Hashtable pools=new Hashtable();
private DBPoolManager()
{
init();
} static synchronized public DBPoolManager getInstance()
{
if (instance == null)
{
instance = new DBPoolManager();
}
clients++;
return instance;
}
private void init()
{
InputStream is = getClass().getResourceAsStream("/db.properties");
Properties dbProps = new Properties();
try
{
dbProps.load(is);
}
catch (Exception e)
{
System.err.println("Can not read the properties file. " + "Make sure db.properties is in the CLASSPATH");
return;
} loadDrivers(dbProps);
createPools(dbProps);
} private void loadDrivers(Properties props)
{
String driverClasses = props.getProperty("drivers");
StringTokenizer st = new StringTokenizer(driverClasses);
while (st.hasMoreElements())
{
String driverClassName = st.nextToken().trim();
try
{
Driver driver = (Driver)Class.forName(driverClassName).newInstance();
DriverManager.registerDriver(driver);
drivers.addElement(driver); }
catch (Exception e)
{
}
}
}
private void createPools(Properties props)
{
Enumeration propNames = props.propertyNames();
while (propNames.hasMoreElements())
{
String name = (String) propNames.nextElement();
if (name.endsWith(".url"))
{
String poolName = name.substring(0, name.lastIndexOf("."));
String url = props.getProperty(poolName + ".url");
if (url == null)
{
continue;
}
String user = props.getProperty(poolName + ".user");
String password = props.getProperty(poolName + ".password");
String maxconn = props.getProperty(poolName + ".maxconn", "0");
int max;
try
{
max = Integer.valueOf(maxconn).intValue();
}
catch (NumberFormatException e)
{
max = 0;
}
ConnectionPool pool = new ConnectionPool(poolName, url, user, password, max,5,30000000);
pools.put(poolName, pool); }
}
}
public Connection getConnection(String name)
{
Connection conn=null;
ConnectionPool cp=(ConnectionPool)pools.get(name);
if(cp!=null)
{
try{
conn=cp.getConnection();
}
catch(Exception e)
{}
}
return conn;
}
public void freeConnection(String name,Connection conn)
{
ConnectionPool pool=(ConnectionPool)pools.get(name);
if(pool!=null)
{
pool.freeConnection(conn);
}
}
}
解决方案 »
- 请教几个另初学者混淆的关于(运行/开发)环境的概念问题?
- 如何抓取到以某product_id=XXXX结尾的所有url地址 100分送出
- 想要2个关于时间的代码...
- 请问怎样根据输入条件动态从数据库提取出数据?
- oracle数据库中记录是乱码,怎么把他们还原出来????
- Expression Language的问题,请大家帮忙看看
- 一个heibernate的问题(欢迎对heibernate的感兴趣的朋友们进来讨论)
- 提供一个算法?give 100分
- Oracle9和 jsp 的冲突
- 请问IIS和jsp服务器如何分工
- servlet的路径问题
- 连接数据库提示我"DriverManager" is either a misplaced package name or a non-existent entity.
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Servletone extends HttpServlet
{
private DBPoolManager connMgr; public void init(ServletConfig conf) throws ServletException
{
super.init(conf);
connMgr = DBPoolManager.getInstance();
} public void service(HttpServletRequest req, HttpServletResponse res)
throws IOException
{ res.setContentType("text/html");
PrintWriter out = res.getWriter(); Connection con=null;
con = connMgr.getConnection("Test_Data.MDF");
if (con == null)
{
out.println("database connection failed.");
return;
}
ResultSet rs = null;
ResultSetMetaData md = null;
Statement stmt = null;
try
{
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM test");
md = rs.getMetaData();
out.println("<H1>member</H1>");
while (rs.next()) {
out.println("<BR>");
for (int i = 1; i < md.getColumnCount(); i++)
{
out.print(rs.getString(i) + ", ");
}
}
stmt.close();
rs.close();
}
catch (SQLException e) {
e.printStackTrace(out);
}
connMgr.freeConnection("Test_Data.MDF",con); } public void destroy()
{ super.destroy();
}
}
HttpConnector すべてのホストIPアドレスのサーバソケットをオープンしますサービス Tomcat-Standalone を起動しますApache Tomcat/4.0.3WebappLoader[]: クラスリポジトリを作業ディレクトリ D:\program\mytest\Tomcat\work に配備しますStandardManager[]: 乱数発生器のクラス java.security.SecureRandom のシードを生成していますStandardManager[]: 乱数発生器のシードの生成が完了しましたContextConfig[]: 証明書をリクエストの属性値に追加しますStandardWrapper[:default]: コンテナサーブレット default をロードしますStandardWrapper[:invoker]: コンテナサーブレット invoker をロードしますHttpConnector[8080] バックグラウンドスレッドを起動しますCan not read the properties file. Make sure db.properties is in the CLASSPATHStandardWrapperValve[servletone]: サーブレット servletone に例外を割り当てますjavax.servlet.ServletException: サーブレット servletone のServlet.init()が例外を投げましたjavax.servlet.ServletException: サーブレット servletone のServlet.init()が例外を投げました at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:935) at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:653) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214) at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472) at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190) at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566) at org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246) at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472) at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943) at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2343) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180) at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566) at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170) at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170) at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472) at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174) at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472) at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943) at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1012) at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1107) at java.lang.Thread.run(Thread.java:536)