try
{
InputStream input=context
.getResourceAsStream("/WEB-INF/DBconfig.properties");
ps.load(input);
input.close();
url=(String)ps.get("url");
user=(String)ps.get("user");
passWord=(String)ps.get("passWord");
DriverName=(String)ps.get("DriverName");
maxConnections=Integer.parseInt(((String)ps.get("maxConnections")).trim(),10);
for(int i=0;i<maxConnections;i )
{
connections.add(getConnection(url,user,passWord));
}
}
catch(Exception e)
{
e.printStackTrace();
}
context.setAttribute("CONNECTOR",new ConnectionProvider(connections));
}
public Connection getConnection(String url,String user,
String passWord)throws Exception
{
Class.forName(DriverName);
return DriverManager.getConnection(url,user,passWord);
}
}
二、ConnectionProvider.java
package bbmyth.util.dataBase;
import java.util.*;
import java.sql.*;
import javax.sql.*;
public class ConnectionProvider
{
private int maxConnections;
private Vector freeConnections;
private Vector nowConnections;
public ConnectionProvider(Vector connections)
{
freeConnections=new Vector();
nowConnections=new Vector();
freeConnections=connections;
maxConnections=freeConnections.size();
}
public Connection getConnection()
{
Connection temp=(Connection)freeConnections.firstElement();
freeConnections.remove(temp);
nowConnections.add(temp);
return temp;
}
public void closeConnection(Connection con)
{
/*Don't use the method close() provid by Connection to close the connection!
beacuase if you do that,the connection will not return to the pool!*/
nowConnections.remove(con);
freeConnections.add(con);
}
public void destroy()//can the container call this method?
{
freeConnections.removeAllElements();
nowConnections.removeAllElements();
}
}
三、DBManager.java
package bbmyth.util.dataBase;
import java.sql.*;
import javax.sql.*;
import java.util.*;
public class DBManager
{
protected ConnectionProvider provider=null;
protected Connection con=null;
protected Statement smt=null;
protected PreparedStatement psmt=null;
protected ResultSet rs=null;
public void setProvider(Object provider)
{
this.provider=(ConnectionProvider)provider;
}
public Connection getConnection()throws SQLException //get acess to database
{
if(provider==null)
throw new SQLException("missing the property 'provider'!!");
return provider.getConnection();
}
public void closeAll()
{
try
{
if(rs!=null)rs.close();
if(smt!=null)smt.close();
if(psmt!=null)psmt.close();
if(con!=null)provider.closeConnection(con);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
/**************************************************************************/
下面是一个应用的例子:该应用是一个文档的添加和查看的应用。在业务逻辑JavaBean里面只有三个方法
而这些方法是根据你自已的业务需要去添加和编写的。另外有一个代表了编文章的JavaBean。
一、ArticleDB.java
package article;
import java.sql.*;
import java.util.*;
import javax.naming.*;
import javax.sql.*;
import bbmyth.util.dataBase.*;
public class ArticleDB extends DBManager
{
public Article getArticleByTitle(String title)throws SQLException
{
Article article=new Article();
try
{
con=getConnection();
String sql="select * " "from articles where title='" title "'";
smt=con.createStatement();
rs=smt.executeQuery(sql);
rs.next();
article.kind=rs.getString(1);
article.author=rs.getString(2);
article.title=rs.getString(3);
article.date=rs.getString(4);
article.body=rs.getString(5);
article.checknum=rs.getInt(6);
closeAll();
}
catch(Exception e)
{
e.printStackTrace();
}
return article;
}
public ArrayList executeQuery(String sql)throws SQLException
{
ArrayList list=new ArrayList();
try
{
con=this.getConnection();
smt=con.createStatement();
rs=smt.executeQuery(sql);
while(rs.next())
{
Article article =new Article();
article.kind=rs.getString(1);
article.author=rs.getString(2);
article.title=rs.getString(3);
article.date=rs.getString(4);
article.body=rs.getString(5);
article.checknum=rs.getInt(6);
list.add(article);
}
closeAll();
}
catch(SQLException e)
{
e.printStackTrace();
}
return list;
}
public void executeUpdate(String sql)throws SQLException
{
try
{
con=this.getConnection();
smt=con.createStatement();
smt.executeUpdate(sql);
closeAll();
}
catch(Exception e)
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




