Topic: 请高手帮忙看看错误在哪啊(Connection Pool)

  Print this page

1.请高手帮忙看看错误在哪啊(Connection Pool) Copy to clipboard
Posted by: dream830101
Posted on: 2005-08-23 15:56

Please show us the error message(s)...

请尽量使用准确的文字描述作为标题
Your next post without a proper Subject will be removed.

Original subject :
请高手帮忙看看错误在哪啊


我看了很多天了编译了很多次 仍然没发改正,请哪个高手帮忙看看 谢谢了。。。。!!!!


package connection;
import java.io.Serializable;
import java.sql.*;
import java.util.*;

public class ConnPool implements java.io.Serializable{
private String driver=null;
private String url=null;
private int size=0;
private String username="";
private String password="";
private DbConn dc=null;
private Vector pool=null;

public ConnPool(){}

public void setDriver(String driver){
if (driver!=null) this.driver=driver;
}
public String getDriver(){
return driver;
}

public void setURL(String url){
if (url!=null) this.url=url;
}

public String getURL(){
return url;
}
//设置连接条数
public void setSize(int size){
if (size>1) this.size=size;
}
public void getSize(){
return size;
}

public void setUsername(String username){
if (username!=null) this.userneme=username;
}
public String getUserName(){
return username;
}

public void setPassword(String password){
if (password!=null) this.password=password;
}
public String getPassword(){
return password;
}

public void setConnBean(DbConn dc){
if (dc!=null) this.dc=dc;
}
public DbConn getConnBean() throws Exception{
Connection conn=getConnection();
DbConn dc=new DbConn(conn);
dc.setInuse(true);
return dc;
}
//创建连接
private Connection createConnection() throws Exception{
Connection con=null;
con=DriverManager.getConnection(url,username,password);
return con;
}

public synchronized void initializePool() throws Exception{
if (driver==null)
throw new Exception("No Driver Provided!");
if (url==null)
throw new Exception("No URL Proviced!");
if (size<1)
throw new Exception("Connection Pool Size is less than 1!");
try{
Class.forName(driver);
for (int i=0; i<size; i++){
Connection con=createConnection();
if (con!=null){
DbConn dc=new DbConn(con);
addConnection(dc);
}
}
}catch (Exception e){
System.err.println(e.getMessage());
throw new Exception(e.getMessage());
}
}

private void addConnection(DbConn conn){
if (pool==null) pool=new Vector(size);
pool.addElement(conn);
}

public synchronized void releaseConnection(Connection con){
for (int i=0; i<pool.size(); i++){
Dbconn connBean=(DbConn)pool.elementAt(i);
if (connBean.getConnection()==con){
connBean.setInuse(false);
break;
}
}
}

public synchronized Connection getConnection() throws Exception{
DbConn dc = null;
for (int i=0;i<pool.size();i++){
dc=(DbConn)pool.elementAt(i);
if (dc.getInuse()==false){
dc.setInuse(true);
Connection con=dc.getConnection();
return con;
}
}
try{
Connection con=createConnection();
dc=new DbConn(con);
dc.setInuse(true);
pool.addElement(dc);
}catch (Exception e){
System.err.println(e.getMessage());
throw new Exception(e.getMessage());
}
return dc.getConnection();
}

public synchronized void emptyPool(){
for (int i=0;i<pool.size();i++){
DbConn connBean=(DbConn)pool.elementAt(i);
if (dc.getInuse()==false)
dc.close();
else{
try{
java.lang.Thread.sleep(20000);
dc.close();
}catch (InterruptedException ie){
System.err.println(ie.getMessage());
}
}
}
}
}

2.Re:请高手帮忙看看错误在哪啊 [Re: dream830101] Copy to clipboard
Posted by: chengbd
Posted on: 2005-08-23 20:46

能提供个完整的程序就好了

3.Re:请高手帮忙看看错误在哪啊 [Re: dream830101] Copy to clipboard
Posted by: liang
Posted on: 2005-09-04 12:43

灯泡?

4.Re:请高手帮忙看看错误在哪啊 [Re: dream830101] Copy to clipboard
Posted by: YuLimin
Posted on: 2005-09-05 16:03

DbConn的程序不清楚,这个程序修改两个地方

1、public void setUsername(String username){
if (username!=null) this.userneme=username;
}

==>this.username=username;//字母错误,请你用相关的Java IDE工具进行程序的编写

2、public void getSize(){
return size;
}

==>public int,既然要返回值,怎么可能返回是void呢?

完整的程序如下:

package com.gdlinkway.budget.webapp.action;

import java.io.Serializable;
import java.sql.*;
import java.util.*;

public class Pool implements java.io.Serializable
{
private String driver = null;
private String url = null;
private int size = 0;
private String username = "";
private String password = "";
private DbConn dc = null;
private Vector pool = null;

public Pool()
{}

public void setDriver(String driver)
{
if(driver != null)
{
this.driver = driver;
}
}

public String getDriver()
{
return driver;
}

public void setURL(String url)
{
if(url != null)
{
this.url = url;
}
}

public String getURL()
{
return url;
}

//设置连接条数
public void setSize(int size)
{
if(size > 1)
{
this.size = size;
}
}

public int getSize()
{
return size;
}

public void setUsername(String username)
{
if(username != null)
{
this.username = username;
}
}

public String getUserName()
{
return username;
}

public void setPassword(String password)
{
if(password != null)
{
this.password = password;
}
}

public String getPassword()
{
return password;
}

public void setConnBean(DbConn dc)
{
if(dc != null)
{
this.dc = dc;
}
}

public DbConn getConnBean() throws Exception
{
Connection conn = getConnection();
DbConn dc = new DbConn(conn);
dc.setInuse(true);
return dc;
}

//创建连接
private Connection createConnection() throws Exception
{
Connection con = null;
con = DriverManager.getConnection(url,username,password);
return con;
}

public synchronized void initializePool() throws Exception
{
if(driver == null)
{
throw new Exception("No Driver Provided!");
}
if(url == null)
{
throw new Exception("No URL Proviced!");
}
if(size < 1)
{
throw new Exception("Connection Pool Size is less than 1!");
}
try
{
Class.forName(driver);
for(int i = 0;i < size;i++)
{
Connection con = createConnection();
if(con != null)
{
DbConn dc = new DbConn(con);
addConnection(dc);
}
}
}
catch(Exception e)
{
System.err.println(e.getMessage());
throw new Exception(e.getMessage());
}
}

private void addConnection(DbConn conn)
{
if(pool == null)
{
pool = new Vector(size);
}
pool.addElement(conn);
}

public synchronized void releaseConnection(Connection con)
{
for(int i = 0;i < pool.size();i++)
{
Dbconn connBean = (DbConn)pool.elementAt(i);
if(connBean.getConnection() == con)
{
connBean.setInuse(false);
break;
}
}
}

public synchronized Connection getConnection() throws Exception
{
DbConn dc = null;
for(int i = 0;i < pool.size();i++)
{
dc = (DbConn)pool.elementAt(i);
if(dc.getInuse() == false)
{
dc.setInuse(true);
Connection con = dc.getConnection();
return con;
}
}
try
{
Connection con = createConnection();
dc = new DbConn(con);
dc.setInuse(true);
pool.addElement(dc);
}
catch(Exception e)
{
System.err.println(e.getMessage());
throw new Exception(e.getMessage());
}
return dc.getConnection();
}

public synchronized void emptyPool()
{
for(int i = 0;i < pool.size();i++)
{
DbConn connBean = (DbConn)pool.elementAt(i);
if(dc.getInuse() == false)
{
dc.close();
}
else
{
try
{
java.lang.Thread.sleep(20000);
dc.close();
}
catch(InterruptedException ie)
{
System.err.println(ie.getMessage());
}
}
}
}
}


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923