免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2814 | 回复: 3
打印 上一主题 下一主题

数据库操作的封装 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-02-09 20:02 |只看该作者 |倒序浏览
我用这个类来完成所有的数据库操作,使用这个类的类采用线程序安全,请大家指点不足之处,谢谢:)
//很多功能还未实现,比如事务等等

  1. /*============================================================
  2. * 版权: 凌云客栈 版权所有 (c) 2002 - 2003
  3. * 文件: org.skyinn.sql.DBOperator
  4. * 功能: 数据库操作类
  5. * 所含类: DBOperator
  6. * 修改记录:
  7. * 日期                作者           内容
  8. * =============================================================
  9. * 2003-01-15          walker        创建文件,并实现基本功能
  10. * 2003-01-19          walker        修改
  11. * ============================================================*/
  12. package org.skyinn.sql;

  13. //sql
  14. import java.sql.Connection;
  15. import java.sql.Statement;
  16. import java.sql.PreparedStatement;
  17. import java.sql.CallableStatement;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. //date
  21. import java.util.Date;
  22. //日志
  23. import org.skyinn.util.Logger;

  24. /**
  25. * <p>;数据库操作类。</p>;

  26. * @author walker
  27. */
  28. public class DBOperator
  29. {
  30.   /**日志处理对象*/
  31.   protected static Logger log = Logger.getInstance("DBOperator");
  32.   /**数据库连接对象*/
  33.   protected Connection conn = null;
  34.   /**Statement*/
  35.   protected Statement stmt = null;
  36.   /**PreparedStatement*/
  37.   protected PreparedStatement pstmt = null;
  38.   /**CallableStatement*/
  39.   protected CallableStatement cstmt = null;
  40.   /**数据库连接池管理对象*/
  41.   protected DBConnectionManager dbmanager = null;

  42.   /**
  43.    * 构造方法。
  44.   *
  45.    * 取数据库连接并生产Statement。
  46.    *
  47.    * @throws SQLException
  48.    */
  49.   public DBOperator(){
  50.     try{
  51.       dbmanager = DBConnectionManager.getInstance();
  52.       /** @todo pool name */
  53.       this.conn = dbmanager.getConnection("DB");
  54.       this.stmt = this.conn.createStatement();
  55.     }catch(Exception e){
  56.       log.error(e.getMessage());
  57.     }
  58.   }

  59.   /**
  60.    * 构造方法。
  61.    *
  62.    * 取数据库连接,生成Statement,根据sql参数生成PrepareStatement。
  63.    *
  64.    * @param sql
  65.    * @throws SQLException
  66.    */

  67.   public DBOperator(String sql){
  68.     try{
  69.       dbmanager = DBConnectionManager.getInstance();
  70.       this.conn = dbmanager.getConnection("DB");
  71.       this.stmt = this.conn.createStatement();
  72.       this.pstmt = this.conn.prepareStatement(sql);
  73.     }catch(Exception e){
  74.       log.error(e.getMessage());
  75.     }
  76.   }//end DBOperator()

  77.   /**
  78.    * 设置PrepareStatement,并同时其清空参数列表。
  79.    *
  80.    * @param sql SQL语句
  81.    * @throws SQLException SQL异常
  82.    */
  83.   public void setPrepareStatement(String sql) throws SQLException
  84.   {
  85.     this.clearParameters();
  86.     this.pstmt = this.conn.prepareStatement(sql);
  87.   }

  88.   /**
  89.    * 设置字符串值。
  90.    *
  91.    * @param index 索引
  92.    * @param value 字符串值
  93.    * @throws SQLException SQL异常
  94.    */
  95.   public void setString(int index,String value) throws SQLException
  96.   {
  97.     pstmt.setString(index,value);
  98.   }

  99.   /**
  100.    * 设置整形值。
  101.    *
  102.    * @param index 索引
  103.    * @param value 整形值
  104.    * @throws SQLException SQL异常
  105.    */
  106.   public void setInt(int index,int value) throws SQLException
  107.   {
  108.     pstmt.setInt(index,value);
  109.   }

  110.   /**
  111.    * 设置布尔值。
  112.    *
  113.    * @param index 索引
  114.    * @param value 布尔值
  115.    * @throws SQLException SQL异常
  116.    */
  117.   public void setBoolean(int index,boolean value) throws SQLException
  118.   {
  119.     pstmt.setBoolean(index,value);
  120.   }

  121.   /**
  122.    * 设置时间值。
  123.    *
  124.    * @param index 索引
  125.    * @param value 时间值
  126.    * @throws SQLException SQL异常
  127.    */
  128.   public void setDate(int index,Date value) throws SQLException
  129.   {
  130.     //pstmt.setDate(index,value);
  131.     /** @todo dateformat and insert... */
  132.     pstmt.setString(index,value.toString());
  133.   }

  134.   /**
  135.    * 设置Long值。
  136.    *
  137.    * @param index 索引
  138.    * @param value Long值
  139.    * @throws SQLException SQL异常
  140.    */
  141.   public void setLong(int index,long value) throws SQLException
  142.   {
  143.     pstmt.setLong(index,value);
  144.   }

  145.   /**
  146.    * 设置浮点值。
  147.    *
  148.    * @param index 索引
  149.    * @param value 浮点值
  150.    * @throws SQLException SQL异常
  151.    */
  152.   public void setFloat(int index,float value) throws SQLException
  153.   {
  154.     pstmt.setFloat(index,value);
  155.   }

  156.   /**
  157.    * 清空PrepareStatement中的参数。
  158.    *
  159.    * @throws SQLException SQL异常
  160.    */
  161.   public void clearParameters() throws SQLException
  162.   {
  163.     if(null != this.pstmt){
  164.       pstmt.clearParameters();
  165.     }
  166.   }

  167.   /**
  168.    * 执行查询。
  169.    *
  170.    * 该方法调用Statement的executeQuery(sql)方法并返回ResultSet结果集。
  171.    *
  172.    * @param sql SQL语句
  173.    * @return ResultSet结果集
  174.    * @throws SQLException SQL异常
  175.    */
  176.   public ResultSet executeQuery(String sql) throws SQLException
  177.   {
  178.     if(null != stmt){
  179.       return stmt.executeQuery(sql);
  180.     }
  181.     return null;
  182.   }//end executeQuery()

  183.   /**
  184.    * 执行查询。
  185.    *
  186.    * 必须先使用DBOperator(conn,sql)或setPrepareStatement(sql)创建PrepareStatement。
  187.    *
  188.    * @return ResultSet结果集
  189.    * @throws SQLException SQL异常
  190.    */
  191.   public ResultSet executeQuery() throws SQLException
  192.   {
  193.     if(null != pstmt){
  194.       return pstmt.executeQuery();
  195.     }
  196.     return null;
  197.   }//end executeQuery()

  198.   /**
  199.    * 更新数据库。
  200.    *
  201.    * 该方法调用Statement的executeUpdate(sql)方法并返回修改的行数。
  202.    *
  203.    * @param sql SQL语句
  204.    * @return 修改的行数
  205.    * @throws SQLException SQL异常
  206.    */
  207.   public int executeUpdate(String sql) throws SQLException
  208.   {
  209.     if(null != stmt){
  210.       return this.stmt.executeUpdate(sql);
  211.     }
  212.     return -1;
  213.   }

  214.   /**
  215.    * 更新数据库。
  216.    *
  217.    * 必须先使用DBOperator(conn,sql)或setPrepareStatement(sql)创建PrepareStatement。
  218.    *
  219.    * @return 修改的行数
  220.    * @throws SQLException SQL异常
  221.    */
  222.   public int executeUpdate() throws SQLException
  223.   {
  224.     if(null != pstmt){
  225.       return this.pstmt.executeUpdate();
  226.     }
  227.     return -1;
  228.   }

  229.   /**
  230.    * 关闭数据库操作,释放数据库连接。
  231.    */
  232.   public void close()
  233.   {
  234.     try{
  235.       if(null != stmt){
  236.         stmt.close();
  237.         stmt = null;
  238.       }
  239.       if(null != pstmt){
  240.         pstmt.close();
  241.         pstmt = null;
  242.       }
  243.       if(null != cstmt){
  244.         cstmt.close();
  245.         cstmt = null;
  246.       }
  247.     }catch(SQLException e){
  248.       log.error("DBOperator close() error: " + e.getMessage());
  249.     }finally{
  250.       try{
  251.         dbmanager.freeConnection(this.conn,"DB");
  252.       }catch(SQLException e){
  253.         log.error("DBOperator close() error: " + e.getMessage());
  254.       }//try...freeConnection...
  255.     }//try...catch...finally...
  256.   }//end close()
  257. }//end DBOperator class
复制代码


调用这个类的代码:

  1.     ...
  2.     DBOperator dbo = new DBOperator();
  3.     dbo.setPrepareStatement(SQLCollection.INSERT_USERINFO);
  4.     //添加用户信息
  5.     dbo.setInt(1,nextID);
  6.     ...
  7.     //执行
  8.     dbo.executeUpdate();
  9.     ...

复制代码

我关心的一个是效率,一个是安全,不知道这么做是否会有问题,如果有,该怎么做?谢谢指点了:)

论坛徽章:
0
2 [报告]
发表于 2003-07-08 15:25 |只看该作者

数据库操作的封装

论坛徽章:
0
3 [报告]
发表于 2005-11-23 16:15 |只看该作者
很不错,顶上来

论坛徽章:
0
4 [报告]
发表于 2005-11-23 17:04 |只看该作者
打击你一句

封装的东西好像不是很多.............
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP