站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2SE 专题初学,请教小问题????????????

初学,请教小问题????????????

摘要: 初学,请教小问题????????????


import java.sql.*;
import java.util.*;

public class ConnectionPool
{
//JDBC Driver name
private String driver=null;
private String url=null;
private int size=0;
private String username=null;
private String password=null;
private Vector pool=null;

public ConnectionPool()
{
}

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

public String getDriver()
{
return driver;
}

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

public String getURL()
{
return url;
}

public void setSize(int value)
{
if(value> 1)
{
size=value;
}
}

public int getSize()
{
return size;
}

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

public String getUsername()
{
return username;
}

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

public String getPassword()
{
return password;
}

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 Name Specified!");
}
if(url==null)
{
throw new Exception("No URL Specified!");
}
if(size < 1)
{
throw new Exception("Pool size is less than 1!");
}
try
{
Class.forName(driver);
for(int x=0;x {
System.err.println("Open JDBC Connection:"+x);
Connection con=createConnection();
if(con!=null)
{
PooledConnection pcon=new PooledConnection(con);
addConnection(pcon);
}
}
}
catch(SQLException sqle)
{
System.err.println(sqle.getMessage());
}
catch(ClassNotFoundException cnfe)
{
System.err.println(cnfe.getMessage());
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
private void addConnection(PooledConnection value)
{
if(pool==null)
{
pool=new Vector(size);
}
pool.addElement(value);
}

public synchronized void releaseConnection(Connection con)
{
for(int x=0;x{
PooledConnection pcon=(PooledConnection)pool.elementAt(x);
if(pcon.getConnection()==con)
{
System.err.println("Release Connection:"+x);

pcon.setInUse(false);
break;
}
}
}

public synchronized Connection getConnection() throws Exception
{
PooledConnection pcon=null;
for(int x=0;x{
pcon=(PooledConnection)pool.elementAt(x);
if(pcon.inUse()==false)
{
pcon.setInUse(true);
return pcon.getConnection();
}
}
try
{
Connection con=createConnection();
pcon=new PooledConnection(con);
pcon.setInUse(true);
pool.addElement(pcon);
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
return pcon.getConnection();
}
public synchronized void emptyPool()
{
for(int x=0;x{
System.err.println("Closing JDBC Connection:"+x);
PooledConnection pcon=(PooledConnection)pool.elementAt(x);
if(pcon.inUse()==false)
{
pcon.close();
}
else
{
try
{
java.lang.Thread.sleep(30000);
pcon.close();
}
catch(InterruptedException ie)
{
System.err.println(ie.getMessage());
}
}
}
}
}


提示如下错误:
--------------------Configuration: --------------------
E:/学习/Java/ConnectionPool.java:131: 找不到符号
符号: 类 PooledConnection
位置: 类 ConnectionPool
private void addConnection(PooledConnection value)
^

请指点一下!!!!!!!!!!!!!
谢谢


PooledConnection 对象呢?

呵呵,这个连接池还有一个PooledConnection 的类。当然不是系统类库的!


昨天有人问连接的问题,我放上去和上面类似的代码,不过PooledConnection的类没有放!


你就在你的web.xml文件中配一个,没必要写啊


从你现在给处的出错信息能够得出的结论就是,找不到PooledConnection这个类,别的问题暂时没看出来.


public ConnectionPool()
{
}
想請教一下,只寫了上面這個是何目的?


回楼上:显性声明无参构造函数.


如果楼主需要,我会把PooledConnection 的代码放上来,说声就行!


楼上的放上来PooledConnection吧,学习一下!!!


应楼上的要求:
/*
* PooledConnection.java
*
* Created on November 8, 2005, 9:30 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package db.lhp.com;

/**
*
* @author Cary
*/

import java.sql.*;
public class PooledConnection {
private Connection connection=null;
private boolean inUser;
public PooledConnection(Connection connection) {
this.connection=connection;
}
public Connection getConnection() {
return connection;
}
public boolean isInUser() {
return inUser;
}
public void setConnection(Connection connection) {
this.connection = connection;
}
public void setInUser(boolean inUser) {
this.inUser = inUser;
}

public void close(){
try{
if (connection != null) {
connection.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}

}
=========
/*
* ConnectionPool.java
*
* Created on November 8, 2005, 8:30 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package db.lhp.com;

/**
*
* @author Cary
*/



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

public class ConnectionPool {

private static ConnectionPool pcon=new ConnectionPool();
private ArrayList pools=new ArrayList();
private String driver;
private String url;
private String uesrname;
private String password;
private int size=5;


private ConnectionPool() {
initialPools();
}
public synchronized static ConnectionPool getConnectionPool(){
if(pcon==null){
pcon=new ConnectionPool();
}
return pcon;
}
private void initialPools() {
try{
InputStream in = this.getClass().getResourceAsStream("/db.properties");
Properties props = new Properties();
props.load(in);

this.setDriver(props.getProperty("driver"));
this.setUrl(props.getProperty("url"));
this.setUesrname(props.getProperty("username"));
this.setPassword(props.getProperty("password"));
this.setSize(Integer.parseInt(props.getProperty("size")));

Class.forName(driver);
for (int i = 0; i < size; i++) {
Connection con = DriverManager.getConnection(url, uesrname, password);
PooledConnection poolcon = new PooledConnection(con);
poolcon.setInUser(false);
pools.add(poolcon);
}
}catch(Exception e){
e.printStackTrace();
}
}
public synchronized void close(){
try{
Iterator its=pools.iterator();
while(its.hasNext()){
PooledConnection pool=(PooledConnection)its.next();
pool.close();
}
pools=null;
}catch(Exception e){
e.printStackTrace();
}
}
public synchronized Connection getConnection()throws Exception{
Iterator its=pools.iterator();
while(its.hasNext()){
PooledConnection poolCon=(PooledConnection)its.next();
if(!poolCon.isInUser())
poolCon.setInUser(true);
return poolCon.getConnection();
}

Class.forName(driver);
Connection con=DriverManager.getConnection(url,uesrname,password);
PooledConnection poolcon=new PooledConnection(con);
poolcon.setInUser(true);
pools.add(poolcon);
return con;
}
public synchronized void releaseConnection(Connection con){
Iterator its=pools.iterator();
while(its.hasNext()){
PooledConnection poolcon=(PooledConnection)its.next();
if(poolcon.getConnection()==con)
{
poolcon.setInUser(false);
break;
}
}
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUesrname() {
return uesrname;
}
public void setUesrname(String uesrname) {
this.uesrname = uesrname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
}



谢谢大家,结帖了,给分!!


↑返回目录
前一篇: 如何最快的判断一个数字(或字符串)是否是一个集合中的一员?谢谢大家。
后一篇: 在 jdk1.4 下 , int.class 怎么表示。