服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

用一个javabean封装jdbc操作


  关于数据库在项目开发中的重要性,我想不用我在此多费唇舌;因此关于对数据库操作的重要性我想更不需要我在此大声疾呼!然而你是否思考过这样的几个问题:在你的每一个项目中共有多少子项目需要对数据库进行操作?你为此设计的操作数据库的javabean是否能满足不同子项目的要求?当用户要求采用其他数据库系统时你是否需要对已经编译好的class文件重新编译?
  
  如果你是一位很勤奋的程序员,你可能会说:“无所谓,只要修改一下连接方式然后再编译一下就ok了!”然而你是否曾经想过,如果我们可以花一点时间把这个javabean完善一下,我们甚至连这种“修改”都可以省去?
  
  笔者并不是一个勤奋的程序员,因此希望通过一个javabean来完成对数据库的大部分操作,同时希望该javabean能够满足对目前大部分主流数据库的操作。在此基础上写了一个javabean,目前暂时命名为lpwdatabaseoperation。以下为该javabean的相关信息:
  
  一、支持对oracle、sybase、mysql、sqlserver、db2、postgresql、jdbc-odbc-bridge等数据库的drivermanager格式的操作;
  
  二、提供了以tomcate服务器为平台的数据源的操作(其他诸如weblogic、websphere等大型服务器通常通过实体bean来访问数据库,因此本程序没有提供对这两种服务器数据源的访问,但保留了相应位置,有兴趣的读者可以自己进行补充。);
  
  三、使用了唯一的一个方法executesql对数据的增加、删除、修改和查询等操作进行统一处理,省去了记忆多个方法的麻烦;
  
  四、提供了对分页显示的简单支持。
  
  本文提供了该javabean的完整源代码,同时源代码中有详细的文档注释,有兴趣的读者可以将其编译成开发文档,以供随时参考。源代码如下:
  package lpw.beans;
  
  import java.sql.drivermanager;
  import java.sql.connection;
  import java.sql.preparedstatement;
  import java.sql.resultset;
  import javax.sql.datasource;
  import javax.naming.context;
  import javax.naming.initialcontext;
  
  /
  

title:


  

description:


  

copyright: copyright 2004


  

company: 吕培文


  @author not attributable
  @version 1.0
  /
  public class lpwdatabaseoperation
  {
  /
  使用oracle格式的drivermanager
  @see lpwdatabaseoperation#getusecontexttype
  /
  public final int useoracledrivermanager = 0;
  /
  使用sybase格式的drivermanager
  @see lpwdatabaseoperation#getusecontexttype
  /
  public final int usesybasedrivermanager = 1;
  /
  使用mysql格式的drivermanager
  @see lpwdatabaseoperation#getusecontexttype
  /
  public final int usemysqldrivermanager = 2;
  /
  使用sqlserver格式的drivermanager
  @see lpwdatabaseoperation#getusecontexttype
  /
  public final int usesqlserverdrivermanager = 3;
  /
  使用db2格式的drivermanager
  @see lpwdatabaseoperation#getusecontexttype
  /
  public final int usedb2drivermanager = 4;
  /
  使用informix格式的drivermanager
  @see lpwdatabaseoperation#getusecontexttype
  /
  public final int useinformixdrivermanager = 5;
  /
  使用postgresql格式的drivermanager
  @see lpwdatabaseoperation#getusecontexttype
  /
  public final int usepostgresqldrivermanager = 6;
  /
  使用jdbc-odbc-bridge格式的drivermanager
  @see lpwdatabaseoperation#getusecontexttype
  /
  public final int usejdbcodbcbridge = 7;
  /
  使用tomcate格式的datasource
  @see lpwdatabaseoperation#getusecontexttype
  /
  public final int usetomcatedatasource = 8;
  /
  使用weblogic格式的datasource
  @see lpwdatabaseoperation#getusecontexttype
  /
  public final int useweblogicdatasource = 9;
  /
  使用websphere格式的datasource
  @see lpwdatabaseoperation#getusecontexttype
  /
  public final int usewebspheredatasource = 10;
  /////////////////////////////////////////////////////////
  private string[] drivermanagertype;
  private int usecontexttype;
  private int pagesize,pagecount,absolutepage,recordcount;
  //////////////////////////////////////////////////////////
  private connection connection;
  private preparedstatement preparedstatement;
  private resultset resultset;
  
  /
  构造lpwdatabaseoperation。
  在构造时需选择所使用的环境类型,
  lpwdatabaseoperation支持多种主流的drivermanager驱动及datasource环境,
  并定义了一些常用drivermanager和datasource的标准格式,
  其中包括oracle、sybase、mysql、sqlserver等主流数据库的drivermanager格式,
  以及jdbc-odbc-bridge的标准格式,
  同时还提供了tomcate、weblogic和websphere下datasource的标准格式,
  程序员在使用时只需要选择相应的常量即可。
  @param usecontexttype int
使用的环境类型
  @see lpwdatabaseoperation#getusecontexttype
  /
  public lpwdatabaseoperation(int usecontexttype)
  {
  if(usecontexttype<0) usecontexttype = 0;
  if(usecontexttype>7) usecontexttype = 7;
  this.usecontexttype = usecontexttype;
  /////////////////////////////////////////////////////////
  this.drivermanagertype[this.useoracledrivermanager] = new string("oracle.jdbc.driver.oracledriver");
  this.drivermanagertype[this.usesybasedrivermanager] = new string("com.sybase.jdbc.sybdriver");
  this.drivermanagertype[this.usemysqldrivermanager] = new string("com.mysql.jdbc.driver");
  this.drivermanagertype[this.usesqlserverdrivermanager] = new string("com.microsoft.jdbc.sqlserver.sqlserverdriver");
  this.drivermanagertype[this.usedb2drivermanager] = new string("com.ibm.db2.jdbc.app.db2driver");
  this.drivermanagertype[this.useinformixdrivermanager] = new string("com.informix.jdbc.ifxdriver");
  this.drivermanagertype[this.usepostgresqldrivermanager] = new string("org.postgresql.driver");
  this.drivermanagertype[this.usejdbcodbcbridge] = new string("sun.jdbc.odbc.jdbcodbcdrive");
  /////////////////////////////////////////////////////////
  this.pagesize = 20;
  this.pagecount = 0;
  this.absolutepage = 0;
  this.recordcount = 0;
  //////////////////////////////////////////////////////////
  this.connection = null;
  this.preparedstatement = null;
  this.resultset = null;
  }
  
  /
  打开数据库,需要给该方法提供数据库的url地址、用户名称及用户密码。
  成功打开数据库后便可通过executesql方法对数据库进行操作。
  @param databaseurl string
数据库的url地址,如果使用datasource则为数据库的jndi-name
  @param username string
用户名称
  @param password string
用户密码
  @throws java.sql.sqlexception
  @throws java.lang.classnotfoundexception
  @throws javax.naming.namingexception
  /
  public void opendatabase(string databaseurl,string username,string password)
  throws java.sql.sqlexception,java.lang.classnotfoundexception,javax.naming.namingexception
  {
  if(this.usecontexttype>=0 && this.usecontexttype<=this.usejdbcodbcbridge)
  {
  class.forname(this.drivermanagertype[this.usecontexttype]);
  this.connection = drivermanager.getconnection(databaseurl,username,password);
  }
  else if(this.usecontexttype==this.usetomcatedatasource)
  {
  context context = new initialcontext();
  datasource datasource = (datasource)context.lookup(databaseurl);
  this.connection = datasource.getconnection(username,password);
  }
  else if(this.usecontexttype==this.useweblogicdatasource)
  {
  }
  else if(this.usecontexttype==this.usewebspheredatasource)
  {
  }
  }
  
  /
  执行sql语句,可以是select、insert、delete、update中的任何一个。
  @param sql string
欲被执行的sql语句
  @return resultset
如果执行查询操作,则返回该查询操作的resultset;如果执行的是其它操作,则返回null。
  @throws java.sql.sqlexception
  /
  public resultset executesql(string sql) throws java.sql.sqlexception
  {
  sql = sql.trim();
  this.preparedstatement = this.c

扫描关注微信公众号