Z
zlc_168
Unregistered / Unconfirmed
GUEST, unregistred user!
TOMCAT默认的工作目录tomcat/webapps/root,在tomcat/webapp/root/web-inf/classes
目录下新建一目录dbean:tomcat/webapp/root/web-inf/classes/dbean,在此目录下放置了
一javabean文件(文件名BConnectionManager.java,其中定义了两个类:DBConnectionManager
和DBConnectionpool),做数据库连接池用,设置了包名:package dbean,然后在相同的目
录下放置了一个servlet文件(文件名:getData.java),包名同javabean相同:
package dbean,设置的classpath已设置了set classpath=.,但编译时,javabean文件
DBConnectionManager.java能够编译通过,但生成了两个字节码文件:
DBConnectionManager$DBConnectionpool.class和DBConnectionManager.class,但编译
servlet文件getData.java文件时,报出错信息,找不到类DBConnectionManager,源代码
如下:
javabean文件:
package dbean;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date;
public class DBConnectionManager{
static DBConnectionManager instance;
static private int clients;
private Vector drivers=new Vector();
private PrintWriter log;
private Hashtable pools=new Hashtable();
static synchronized public DBConnectionManager getInstance() {
if (instance == null) {
instance = new DBConnectionManager();
}
clients++;
return instance;
}
private DBConnectionManager(){
init();
}
public void freeConnection(String name,Connection conn) {
DBConnectionpool pool=(DBConnectionpool) pools.get(name);
if (pool!=null) {
pool.freeConnection(conn);
}
}
public Connection getConnection(String name) {
DBConnectionpool pool=(DBConnectionpool) pools.get(name);
if (pool!=null) {
return pool.getConnection();
}
return null;
}
public Connection getConnection(String name,long time) {
DBConnectionpool pool=(DBConnectionpool) pools.get(name);
if (pool!=null) {
return pool.getConnection(time);
}
return null;
}
public synchronized void release() {
if (--clients!=0) {
return;
}
Enumeration allPools=pools.elements();
while (allPools.hasMoreElements()){
DBConnectionpool pool=(DBConnectionpool) allPools.nextElement();
pool.release();
}
Enumeration allDrivers=drivers.elements();
while (allDrivers.hasMoreElements()){
Driver driver=(Driver) allDrivers.nextElement();
try{
DriverManager.deregisterDriver(driver);
log("撤消JDBC驱动程序"+driver.getClass().getName()+"的注册!");
}
catch(SQLException e){
log("无法撤消下列驱动程序"+driver.getClass().getName());
}
}
}
private void createPools(Properties props){
Enumeration propName=props.propertyNames();
while(propName.hasMoreElements()){
String name=(String) propName.nextElement();
if (name.endsWith(".url")){
String poolName=name.substring(0,name.lastIndexOf("."));
String url=props.getProperty(poolName+".url");
if (url==null){
log("没有为连接池"+poolName+"指定url");
continue;
}
String user=props.getProperty(poolName+".user");
String pwd=props.getProperty(poolName+".password");
String maxconn=props.getProperty(poolName+".maxconn");
int max;
try{
max=Integer.valueOf(maxconn).intValue();
}
catch(NumberFormatException e){
log("设定的错误连接限制数:"+maxconn+"连接池:"+poolName);
max=0;
}
DBConnectionpool pool=new DBConnectionpool(poolName,url,user,pwd,max);
pools.put(poolName,pool);
log("成功创建连接池:"+poolName);
}
}
}
private void init() {
InputStream is=getClass().getResourceAsStream("db.properties");
Properties dbProps=new Properties();
try {
dbProps.load(is);
}
catch(Exception e) {
System.err.println("无法读取属性文件,请确保文件在CLASSPATH指定的路径中");
return;
}
String logFile=dbProps.getProperty("logfile","DBConnection Manager.log");
try {
log=new PrintWriter(new FileWriter(logFile),true);
}
catch(IOException e) {
System.err.println("无法打开日志文件:"+logFile);
log=new PrintWriter(System.err);
}
loadDrivers(dbProps);
createPools(dbProps);
}
private void loadDrivers(Properties props) {
String driverClass=props.getProperty("drivers");
StringTokenizer st=new StringTokenizer(driverClass);
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){
log("无法注册JDBC驱动程序"+driverClassName,e);
}
}
}
private void log(String msg) {
log.println (new Date()+":"+msg);
}
private void log(String msg,Throwable e) {
log.println (new Date()+":"+msg);
e.printStackTrace (log);
}
class DBConnectionpool {
private int checkout;
private Vector freeConnections=new Vector();
private int maxConn;
private String name;
private String password;
private String URL;
private String user;
public DBConnectionpool(String name,String URL,String user,String pwd,int max) {
this.name=name;
this.URL=URL;
this.user=user;
this.password=pwd;
this.maxConn=max;
}
public synchronized void freeConnection(Connection conn) {
freeConnections.addElement(conn);
checkout--;
notifyAll();
}
public Connection getConnection() {
Connection conn=null;
if (freeConnections.size()>0) {
conn=(Connection) freeConnections.firstElement();
freeConnections.removeElementAt(0);
try{
if (conn.isClosed()){
log("从连接池"+name+"删除一个无效连接");
conn=getConnection();
}
}
catch(SQLException e) {
log("从连接池"+name+"删除一个无效连接");
conn=getConnection();
}
}
else
if (maxConn==0 || checkout<maxConn) {
conn=newConnection();
}
if (conn!=null){
checkout++;
}
return conn;
}
public Connection getConnection(long timeout) {
Connection conn=null;
long startTime=new Date().getTime();
while ((conn=getConnection())==null) {
try{
wait(timeout);
}
catch(InterruptedException e){}
if (new Date().getTime()-startTime>=timeout) {
return null;
}
}
return conn;
}
public void release() {
Connection conn=null;
Enumeration allconn=freeConnections.elements();
while (allconn.hasMoreElements()){
conn=(Connection) allconn.nextElement();
try{
conn.close();
}
catch(SQLException e){
log("关闭连接池"+name+"错误",e);
}
}
freeConnections.removeAllElements();
}
public Connection newConnection() {
Connection conn=null;
if (user==null) {
try{
conn=DriverManager.getConnection(URL);
}
catch(SQLException e){
log("新建连接池"+name+"的一个连接失败", e);
return null;
}
}
else
{
try{
conn=DriverManager.getConnection(URL,user,password);
}
catch(SQLException e){
log("新建连接池"+name+"的一个连接失败",e);
return null;
}
}
return conn;
}
}//DBConnectionpool结尾
}//DBConnectionManager结尾
servlet文件
package dbean;
import java.io.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class getData extends HttpServlet {
DBConnectionManager connMgr;
public voiddo
Get(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
PrintWriter out=response.getWriter();
Connection conn=null;
String dbName="Northwind";
String sqlstr="select * from employees";
connMgr=DBConnectionManager.getInstance();
conn=(Connection) connMgr.getConnection(dbName);
Statement lstmt=null;
ResultSet rs=null;
if (conn==null){
out.println("无法连接数据库");
return;
}
else
{
try{
lstmt=conn.createStatement();
rs=lstmt.executeQuery(sqlstr);
out.println("<h1>数据</h1>");
out.println("<table border=/"1/" cellSpacing=0
cellPadding=0>");
out.println("<tr><th>名</th><th>姓</th></tr>");
while (rs.next()){
out.println("<tr><td>"+rs.getString("lastname")+"</td>");
out.println("<td>"+rs.getString("firstname")+"</td></tr>");
}
out.println("</table>");
try{
rs.close();
lstmt.close();
conn.close();
}
catch(SQLException e){
out.println("关闭数据库失败!");
}
}
catch(SQLException e){
out.println("无法检索数据!");
return;
}
}
}
}
请各位大哥帮帮忙!!!
目录下新建一目录dbean:tomcat/webapp/root/web-inf/classes/dbean,在此目录下放置了
一javabean文件(文件名BConnectionManager.java,其中定义了两个类:DBConnectionManager
和DBConnectionpool),做数据库连接池用,设置了包名:package dbean,然后在相同的目
录下放置了一个servlet文件(文件名:getData.java),包名同javabean相同:
package dbean,设置的classpath已设置了set classpath=.,但编译时,javabean文件
DBConnectionManager.java能够编译通过,但生成了两个字节码文件:
DBConnectionManager$DBConnectionpool.class和DBConnectionManager.class,但编译
servlet文件getData.java文件时,报出错信息,找不到类DBConnectionManager,源代码
如下:
javabean文件:
package dbean;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date;
public class DBConnectionManager{
static DBConnectionManager instance;
static private int clients;
private Vector drivers=new Vector();
private PrintWriter log;
private Hashtable pools=new Hashtable();
static synchronized public DBConnectionManager getInstance() {
if (instance == null) {
instance = new DBConnectionManager();
}
clients++;
return instance;
}
private DBConnectionManager(){
init();
}
public void freeConnection(String name,Connection conn) {
DBConnectionpool pool=(DBConnectionpool) pools.get(name);
if (pool!=null) {
pool.freeConnection(conn);
}
}
public Connection getConnection(String name) {
DBConnectionpool pool=(DBConnectionpool) pools.get(name);
if (pool!=null) {
return pool.getConnection();
}
return null;
}
public Connection getConnection(String name,long time) {
DBConnectionpool pool=(DBConnectionpool) pools.get(name);
if (pool!=null) {
return pool.getConnection(time);
}
return null;
}
public synchronized void release() {
if (--clients!=0) {
return;
}
Enumeration allPools=pools.elements();
while (allPools.hasMoreElements()){
DBConnectionpool pool=(DBConnectionpool) allPools.nextElement();
pool.release();
}
Enumeration allDrivers=drivers.elements();
while (allDrivers.hasMoreElements()){
Driver driver=(Driver) allDrivers.nextElement();
try{
DriverManager.deregisterDriver(driver);
log("撤消JDBC驱动程序"+driver.getClass().getName()+"的注册!");
}
catch(SQLException e){
log("无法撤消下列驱动程序"+driver.getClass().getName());
}
}
}
private void createPools(Properties props){
Enumeration propName=props.propertyNames();
while(propName.hasMoreElements()){
String name=(String) propName.nextElement();
if (name.endsWith(".url")){
String poolName=name.substring(0,name.lastIndexOf("."));
String url=props.getProperty(poolName+".url");
if (url==null){
log("没有为连接池"+poolName+"指定url");
continue;
}
String user=props.getProperty(poolName+".user");
String pwd=props.getProperty(poolName+".password");
String maxconn=props.getProperty(poolName+".maxconn");
int max;
try{
max=Integer.valueOf(maxconn).intValue();
}
catch(NumberFormatException e){
log("设定的错误连接限制数:"+maxconn+"连接池:"+poolName);
max=0;
}
DBConnectionpool pool=new DBConnectionpool(poolName,url,user,pwd,max);
pools.put(poolName,pool);
log("成功创建连接池:"+poolName);
}
}
}
private void init() {
InputStream is=getClass().getResourceAsStream("db.properties");
Properties dbProps=new Properties();
try {
dbProps.load(is);
}
catch(Exception e) {
System.err.println("无法读取属性文件,请确保文件在CLASSPATH指定的路径中");
return;
}
String logFile=dbProps.getProperty("logfile","DBConnection Manager.log");
try {
log=new PrintWriter(new FileWriter(logFile),true);
}
catch(IOException e) {
System.err.println("无法打开日志文件:"+logFile);
log=new PrintWriter(System.err);
}
loadDrivers(dbProps);
createPools(dbProps);
}
private void loadDrivers(Properties props) {
String driverClass=props.getProperty("drivers");
StringTokenizer st=new StringTokenizer(driverClass);
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){
log("无法注册JDBC驱动程序"+driverClassName,e);
}
}
}
private void log(String msg) {
log.println (new Date()+":"+msg);
}
private void log(String msg,Throwable e) {
log.println (new Date()+":"+msg);
e.printStackTrace (log);
}
class DBConnectionpool {
private int checkout;
private Vector freeConnections=new Vector();
private int maxConn;
private String name;
private String password;
private String URL;
private String user;
public DBConnectionpool(String name,String URL,String user,String pwd,int max) {
this.name=name;
this.URL=URL;
this.user=user;
this.password=pwd;
this.maxConn=max;
}
public synchronized void freeConnection(Connection conn) {
freeConnections.addElement(conn);
checkout--;
notifyAll();
}
public Connection getConnection() {
Connection conn=null;
if (freeConnections.size()>0) {
conn=(Connection) freeConnections.firstElement();
freeConnections.removeElementAt(0);
try{
if (conn.isClosed()){
log("从连接池"+name+"删除一个无效连接");
conn=getConnection();
}
}
catch(SQLException e) {
log("从连接池"+name+"删除一个无效连接");
conn=getConnection();
}
}
else
if (maxConn==0 || checkout<maxConn) {
conn=newConnection();
}
if (conn!=null){
checkout++;
}
return conn;
}
public Connection getConnection(long timeout) {
Connection conn=null;
long startTime=new Date().getTime();
while ((conn=getConnection())==null) {
try{
wait(timeout);
}
catch(InterruptedException e){}
if (new Date().getTime()-startTime>=timeout) {
return null;
}
}
return conn;
}
public void release() {
Connection conn=null;
Enumeration allconn=freeConnections.elements();
while (allconn.hasMoreElements()){
conn=(Connection) allconn.nextElement();
try{
conn.close();
}
catch(SQLException e){
log("关闭连接池"+name+"错误",e);
}
}
freeConnections.removeAllElements();
}
public Connection newConnection() {
Connection conn=null;
if (user==null) {
try{
conn=DriverManager.getConnection(URL);
}
catch(SQLException e){
log("新建连接池"+name+"的一个连接失败", e);
return null;
}
}
else
{
try{
conn=DriverManager.getConnection(URL,user,password);
}
catch(SQLException e){
log("新建连接池"+name+"的一个连接失败",e);
return null;
}
}
return conn;
}
}//DBConnectionpool结尾
}//DBConnectionManager结尾
servlet文件
package dbean;
import java.io.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class getData extends HttpServlet {
DBConnectionManager connMgr;
public voiddo
Get(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
PrintWriter out=response.getWriter();
Connection conn=null;
String dbName="Northwind";
String sqlstr="select * from employees";
connMgr=DBConnectionManager.getInstance();
conn=(Connection) connMgr.getConnection(dbName);
Statement lstmt=null;
ResultSet rs=null;
if (conn==null){
out.println("无法连接数据库");
return;
}
else
{
try{
lstmt=conn.createStatement();
rs=lstmt.executeQuery(sqlstr);
out.println("<h1>数据</h1>");
out.println("<table border=/"1/" cellSpacing=0
cellPadding=0>");
out.println("<tr><th>名</th><th>姓</th></tr>");
while (rs.next()){
out.println("<tr><td>"+rs.getString("lastname")+"</td>");
out.println("<td>"+rs.getString("firstname")+"</td></tr>");
}
out.println("</table>");
try{
rs.close();
lstmt.close();
conn.close();
}
catch(SQLException e){
out.println("关闭数据库失败!");
}
}
catch(SQLException e){
out.println("无法检索数据!");
return;
}
}
}
}
请各位大哥帮帮忙!!!