类型:转载 责任编辑:asp.net 日期:2007/05/23
热门软件下载:










页面导航:
正文内容: 在开发有关数据库的应用时,虽然jdbc提供给我们许多便利,但在我们开发不同的应用的时候还是会重复着一些相同的工作,比如说编写数据库连接的程序,工作虽然不算多,但不断地重复实在是浪费时间,也增加了调试的复杂度;还有一点,我们应该尽量使用配置的数据源,笔者用的是mysql,当用到中文应用上的时候mysql的数据源没办法解决中文问题,另一方面又不希望每次都手动创建连接。基于以上遇到的两个小障碍,我编写了一个小框架,实现数据库使用方法最简单,能创建数据连接池。这些工作的实现都不需要再编程。
在web应用中我们只需要在web-inf文件夹下面加入一个dbconfig.properties的文件,简单设置几个参数,再在web.xml文件内加入一个listener元素即可。凡是jdbc支持的数据库类型都可以通过这两个文件来设置得到与数据库的连接。有了这个基础,开发者可以只需要考虑业务上的编程。比如说要实现什么样的查询和数据操作,开发者只需要定义这些业务上的方法即可。这样对开发效率来说大有改善。当然,如果在一些大的应用里面,用到orm框架的话,这个框架就派不上用场了。这个小框架只适用于中小型的“利用关系数据库本土语言来工作”的应用。
使用的步骤:
一、配置dbconfig.properties
dbconfig.properties的设置例子如下:
url=jdbc:mysql://localhost:3306/article?useunicode=true&characterencoding=gb2312
user=dbuser
password=227711
drivername=com.mysql.jdbc.driver
maxconnections=10
url参数指定所要的数据库连接的url。
user参数是连接该数据库的用户名
password参数是连接用户的密码
drivername参数是所用数据库类型的驱动类
maxconnections可以设置最大连接数目
二、配置web.xml:
在web.xml中这样设置便可:
<web-app>
<listener>
<listener-class>bbmyth.util.database.datasourceproviderservlet</listener-class>
</listener>
</web-app>
不要去改变<listener>里面的所有东西。
三、用本应用包的api来编写业务程序
确保bbmyth.util.database这个应用包在你的classpath(供编译程序用)中,或直接将该包import入来。
最后定义自已的数据库操作bean。注意这个bean要继承bbmyth.util.database.dbmanager类,在这个dbmanager类里提供了四个数据库操作变量:
connection con:是连接类,框架已提供现成的连接,开发者可在程序里以下面的语句来获得数据库的连接:con=getconnection();
statement smt:是查询语句类的变量,可直接使用;
preparedstatement psmt:是预编译查询语句类的变量,可直接使用;
resultset rs:是结果集的变量,可直接使用。
每一次连接使用完之后,不管是用编译语句还是预编译语句和有没有返回结果集都可以直接用closeall()函数来关闭它们,不推荐用他们自已的close()方法。
四、在jsp中应用usebean标签把该业务javabean应用进去,然后用setproperty标签设置其一个参数。如:
<jsp:setproperty name="articledb" property="provider" value="<%=application.getattribute("connector")%>"/>
articledb是你自已的usebean定义的id。其他的不要改动。
五、调试运行!这样,就可以按照用户给的参数,自动在应用启动的时候创建所需要的连接池。在应用退出的时候释放连接。用mysql在配置数据源因为总是加不了中文编码的参数,而在这里,可以直接在url里加入中文参数。既实现了数据库连接的便利,又解决了中文连接的问题。
工作原理:
工作原理很简单:一个类实现了servletcontextlistener的接口,可以实现web应用的事件监听。我在应用的启动的时候读进配置文件的参数,然后按照参数连接数据库,把完成的连接存放到一个bean当中,然后把这个bean作为一个变量存放在servletcontext中即jsp的application范围中,整个应用都可以使用的。
接着,编写业务方法是用了该包里面的api,在jsp页面里使用这个连接池实例的时候要通过usebaen和setproperty的标签来把servletcontext中的连接池变量作为参数用到自已的业务bean来,这样就可以使用现成的连接池工作了。
另外,用户写业务bean的时候继承dbmanager类。该类已经定义好了一些必要的变量和实现了连接。用户可以直接开始写自已的业务逻辑方法。只须要知道要怎样开始,怎样结束就得了。
下面是该应用包的源代码:共有三个类文件,datasourceproviderservlet .java在应用启动的时候生成连接存放到connectionprovider.java的类中。dbmanager则是为编程提供便利的。
/*******************************************************************/
一、datasourceproviderservlet .java
package bbmyth.util.database;
import javax.servlet.http.httpservlet;
import javax.servlet.servletcontextlistener;
import javax.servlet.servletcontextevent;
import javax.servlet.servletcontext;
import java.util.*;
import java.io.*;
import javax.sql.*;
import java.sql.*;
public class datasourceproviderservlet implements servletcontextlistener
{
public void contextdestroyed(servletcontextevent sce)
{
/*本方法是在应用退出时由系统调用的,在这个时候我们
把所有的连接都关闭!*/
((connectionprovider)(sce.getservletcontext().getattribute("connector"))).destroy();
sce.getservletcontext().removeattribute("connector");
}
public void contextinitialized(servletcontextevent sce)
{
/*本方法在应用初始时调用,这时我们可以创建连接并且存到
一个容器当中,存储在应用的上下文中,然后交由一个javabean
处理。*/
properties ps=new properties();
vector connections=new vector();
string url=null;
string user=null;
string password=null;
int maxconnections;
string drivername=null;
servletcontext context=sce.getservletcontext();
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)
{
/*dont 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)
{
e.printstacktrace();
}
}
}
二、article.java
package article;
import java.sql.*;
import java.util.*;
import javax.naming.*;
import javax.sql.*;
public class article
{
string kind;
string author;
string title;
string date;
string body;
int checknum;
public article()
{
}
public article(string kind,string author,string title,string date,string body,int checknum)
{
this.kind=kind;
this.author=author;
this.title=title;
this.date=date;
this.body=body;
this.checknum=checknum;
}
/*********读取字段值********/
public int getchecknum()
{
return this.checknum;
}
public string getkind()
{
return this.kind;
}
public string getauthor()
{
return this.author;
}
public string gettitle()
{
return this.title;
}
public string getdate()
{
return this.date;
}
public string getbody()
{
return this.body;
}
/**********设置字段值*********/
public void setkind(string kind)
{
this.kind=kind;
}
public void setauthor(string author)
{
this.author=author;
}
public void settitle(string title)
{
this.title=title;
}
public void setdate(string date)
{
this.date=date;
}
public void setbody(string body)
{
this.body=body;
}
public void setchecknum(int checknum)
{
this.checknum=checknum;
}
}
附:在jsp中这样设置的:
<jsp:usebean id="articledb" class="article.articledb" scope="application">
<jsp:setproperty name="articledb" property="provider" value="<%=application.getattribute("connector")%>"/>
</jsp:usebean>
这么一来,在jsp中就可以随处使用你的业务bean的方法来工作了。
总结:这是个很简单的小应用工具,适用于小型或中小型的应用。可以减少开发者在数据库连接上的花的工夫。专心编写业务程序。当然,也许很多人认为不必要,呵,这是我个人认为了。最好大家都能自已动手写一个,这样的话可以对数据库连接和相关的知识有更进一步的了解,另一方面可以方便自已以后的开发。数据池,最后不要用别人写的,要么自已写!写得不好没关系,再写!直接拿别人的来用就什么都学不到了。呵呵。我是这样认为的。这个工具到目前为止功能不算多,以后还会继续增加。