高分求一个关于Bean的简单问题

lichp 2003-12-29 04:49:35
问题是这样的,我参照书写了一个连接池的Bean名叫:connPool.java
然后又写了一个连接MySql的Bean名叫:mySqlConn.java
分别编译的时候都可以顺利编译,但现在我想把这他们整合,就是让mySqlConn使用connPool打开数据库,我的代码如下:
=============connPool.java===============
package common;

import java.lang.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;

public class connPool{
private static final int defaultMaxConnections = 3;

private Vector freeConnections;
private Hashtable boundConnections;
private static final String driverName = "org.gjt.mm.mysql.Driver";
private static final String jdbcURL = "jdbc:mysql://localhost/univessence";
private static final String username = "root";
private static final String password = "";
private int maxConnections;

public connPool(int numConnections){
maxConnections = numConnections;
boundConnections = null;
freeConnections = null;
}

public connPool(){
this(defaultMaxConnections);
}

public void closeDB() throws SQLException{
if (boundConnections != null){
for(Enumeration e = boundConnections.elements();
e.hasMoreElements();){
Connection conn = (Connection)e.nextElement();
conn.close();
}
boundConnections.clear();
boundConnections = null;
}
if (freeConnections != null){
for (Enumeration e = freeConnections.elements();
e.hasMoreElements();){
Connection conn = (Connection)e.nextElement();
conn.close();
}
freeConnections.removeAllElements();
freeConnections = null;
}
}

public synchronized Connection getConnection()
throws SQLException{
if (freeConnections == null)
throw new SQLException(
"The connection pool has not been established yet.");
if (boundConnections.get(Thread.currentThread()) != null)
throw new SQLException(
"Cannot get connections over once for this current running thread.");
try{
if (freeConnections.size() == 0)
wait();
}
catch(InterruptedException ex){
throw new SQLException(ex.toString());
}
Connection conn = (Connection)freeConnections.firstElement();
freeConnections.removeElement(conn);
boundConnections.put(Thread.currentThread(),conn);
return conn;
}


public void openDB(String drvName,String url,
String uname,String passwd)
throws SQLException{
try{
boundConnections = new Hashtable(maxConnections);
freeConnections = new Vector(maxConnections);
Class.forName(drvName);
for (int i=0;i<maxConnections;i++)
freeConnections.addElement(
DriverManager.getConnection(url,uname,passwd));
}
catch(Exception ex){
boundConnections = null;
freeConnections = null;
throw new SQLException(ex.toString());
}
}

public synchronized void returnConnection()
throws SQLException{
Connection conn =
(Connection)boundConnections.remove(
Thread.currentThread());
if (conn == null)
throw new SQLException(
"The connection which this current running thread got is not found.");
freeConnections.addElement(conn);
notify();
}


public void setConnectionSwitch(String on_off)
throws ServletException{
try{
if (on_off.equalsIgnoreCase("ON"))
openDB(driverName,jdbcURL,username,password);
else if (on_off.equalsIgnoreCase("OFF"))
closeDB();
}
catch(SQLException ex){
throw new ServletException(ex.toString());
}
}

}


=============mySqlConn.java===============
package common;

import java.lang.*;
import java.sql.*;
import javax.servlet.*;

public class mySqlConn{
private Connection conn;
private ResultSet rs;
private ResultSetMetaData rsmd;
private Statement stmt;
private ConnPool connPool; //错误就是在这行产生的
private static final String driverName = "org.gjt.mm.mysql.Driver";
private static final String jdbcURL = "jdbc:mysql://localhost/univessence";
private static final String username = "root";
private static final String password = "";

public mySqlConn(){
conn = null;
rs = null;
rsmd = null;
stmt = null;
}

//由于字数限制,代码不能完全贴出来

}

mySqlConn.java:12:cannot resolve symbol
symbol:class ConnPool
location:common.mySqlConn
private ConnPool connPool; 这是错误之一,几个错误是一样的,我想其中一个解决了另几个就很容易了
...全文
24 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
lichp 2003-12-30
  • 打赏
  • 举报
回复
已经搞定了,虽然不是用上面的大虾们的办法搞定的,但还是加分
jokerjava 2003-12-29
  • 打赏
  • 举报
回复
package common;

import java.lang.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;

public class ConnPool{
private static final int defaultMaxConnections = 3;

private Vector freeConnections;
private Hashtable boundConnections;
private static final String driverName = "org.gjt.mm.mysql.Driver";
private static final String jdbcURL = "jdbc:mysql://localhost/univessence";
private static final String username = "root";
private static final String password = "";
private int maxConnections;

public ConnPool(int numConnections){
maxConnections = numConnections;
boundConnections = null;
freeConnections = null;
}

public ConnPool(){
this(defaultMaxConnections);
}

public void closeDB() throws SQLException{
if (boundConnections != null){
for(Enumeration e = boundConnections.elements();
e.hasMoreElements();){
Connection conn = (Connection)e.nextElement();
conn.close();
}
boundConnections.clear();
boundConnections = null;
}
if (freeConnections != null){
for (Enumeration e = freeConnections.elements();
e.hasMoreElements();){
Connection conn = (Connection)e.nextElement();
conn.close();
}
freeConnections.removeAllElements();
freeConnections = null;
}
}

public synchronized Connection getConnection()
throws SQLException{
if (freeConnections == null)
throw new SQLException(
"The connection pool has not been established yet.");
if (boundConnections.get(Thread.currentThread()) != null)
throw new SQLException(
"Cannot get connections over once for this current running thread.");
try{
if (freeConnections.size() == 0)
wait();
}
catch(InterruptedException ex){
throw new SQLException(ex.toString());
}
Connection conn = (Connection)freeConnections.firstElement();
freeConnections.removeElement(conn);
boundConnections.put(Thread.currentThread(),conn);
return conn;
}


public void openDB(String drvName,String url,
String uname,String passwd)
throws SQLException{
try{
boundConnections = new Hashtable(maxConnections);
freeConnections = new Vector(maxConnections);
Class.forName(drvName);
for (int i=0;i<maxConnections;i++)
freeConnections.addElement(
DriverManager.getConnection(url,uname,passwd));
}
catch(Exception ex){
boundConnections = null;
freeConnections = null;
throw new SQLException(ex.toString());
}
}

public synchronized void returnConnection()
throws SQLException{
Connection conn =
(Connection)boundConnections.remove(
Thread.currentThread());
if (conn == null)
throw new SQLException(
"The connection which this current running thread got is not found.");
freeConnections.addElement(conn);
notify();
}


public void setConnectionSwitch(String on_off)
throws ServletException{
try{
if (on_off.equalsIgnoreCase("ON"))
openDB(driverName,jdbcURL,username,password);
else if (on_off.equalsIgnoreCase("OFF"))
closeDB();
}
catch(SQLException ex){
throw new ServletException(ex.toString());
}
}

}

package common;
import java.lang.*;
import java.sql.*;
import javax.servlet.*;

public class MySqlConn{
private Connection conn;
private ResultSet rs;
private ResultSetMetaData rsmd;
private Statement stmt;
private ConnPool connPool; //错误就是在这行产生的
private static final String driverName = "org.gjt.mm.mysql.Driver";
private static final String jdbcURL = "jdbc:mysql://localhost/univessence";
private static final String username = "root";
private static final String password = "";

public MySqlConn(){
conn = null;
rs = null;
rsmd = null;
stmt = null;
}

//由于字数限制,代码不能完全贴出来

}
这是该完的
jokerjava 2003-12-29
  • 打赏
  • 举报
回复
改成小写我都编译过了

这是个很基本的问题
类名明显的写错了
你可能还有的地方没该把

lichp 2003-12-29
  • 打赏
  • 举报
回复
类文件名及里面的类名构造函数等都改成大写开头了,问题依旧
lichp 2003-12-29
  • 打赏
  • 举报
回复
不是这样原因,我改成小写一样的错误。
jokerjava 2003-12-29
  • 打赏
  • 举报
回复
就算都在一个包里也 需要这样

楼上这种说法根本不对哦

同一个包中不用import的

类文件最好大写开头
jokerjava 2003-12-29
  • 打赏
  • 举报
回复
connPool的大小写写错了
private ConnPool connPool;
改成
private connPoolconnPool;
就可以了
usaspy 2003-12-29
  • 打赏
  • 举报
回复
就算都在一个包里也 需要这样
usaspy 2003-12-29
  • 打赏
  • 举报
回复
import common.*;

你必须要在
mySqlConn.java中引用它需要的类
haley_hj 2003-12-29
  • 打赏
  • 举报
回复
在mySqlConn.java里面要引用connPool

import common.*;
lichp 2003-12-29
  • 打赏
  • 举报
回复
是同在一个工程下,不过我编译是在Dos里编译的,在JBuilder里编译不成功
haley_hj 2003-12-29
  • 打赏
  • 举报
回复
你用的什么编辑工具?

用Jbuilder等工程管理工具,讲它们作为一个工程就可以实现。

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧