JAVA:数据库操作封装

原创 2005年03月04日 08:28:00

package creator.common.db;

import java.io.InputStream;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;

/**
 *
 * <p>Title: dbBean.java</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author TanBo
 * @version 1.0
 */
public class DbBean {
  public java.sql.Connection conn = null; //connection object
  public ResultSet rs = null; //resultset object
  public Statement stmt = null; //statement object
  public PreparedStatement prepstmt = null; //preparedstatement object
  private String drivers = null; //connection parameter:drivers
  private String url = null; //connection parameter:url
  private String user = null; //connection parameter:user
  private String password = null; //connection parameter:password
  private String jndi_name = null; //connection pool parameter:jndi name

  /**
   * init()
   */
  public DbBean(){
    try{
      //parameter init
      drivers = creator.config.ConfigBundle.getString("drivers");
      url = creator.config.ConfigBundle.getString("url");
      user = creator.config.ConfigBundle.getString("user");
      password = creator.config.ConfigBundle.getString("password");
      jndi_name = creator.config.ConfigBundle.getString("jndi_name");

      //db connection pool init
      //InitialContext env = new InitialContext();
      //javax.sql.DataSource pool = (javax.sql.DataSource)env.lookup(jndi_name);
      //conn = pool.getConnection();

      //db connection init
      Class.forName(drivers);
      conn = DriverManager.getConnection(url,user,password);

      //db statement init
      stmt = conn.createStatement();
    }catch(Exception e){
      System.out.println("dbBean: init error!"+e.toString());
    }
  }

  /**
   * @function prepareStatement
   * @param sql String
   * @throws SQLException
   */
  public void prepareStatement(String sql) throws SQLException {
    prepstmt = conn.prepareStatement(sql);
  }

  /**
   * @function prepareStatement
   * @param sql String
   * @param resultSetType int
   * @param resultSetConcurrency int
   * @throws SQLException
   */
  public void prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
      throws SQLException {
    prepstmt = conn.prepareStatement(sql, resultSetType, resultSetConcurrency);
  }

  /**
   * @function executeQuery
   * @param sql String
   * @throws SQLException
   * @return ResultSet
   */
  public ResultSet executeQuery(String sql) throws SQLException {
    if (stmt != null) {
      return stmt.executeQuery(sql);
    }else return null;
  }

  /**
   * @function executeQuery
   * @throws SQLException
   * @return ResultSet
   */
  public ResultSet executeQuery() throws SQLException {
    if (prepstmt != null) {
      return prepstmt.executeQuery();
    }else return null;
  }

  /**
   * @function executeUpdate
   * @param sql String
   * @throws SQLException
   */
  public void executeUpdate(String sql) throws SQLException {
    if (stmt != null)
      stmt.executeUpdate(sql);
  }

  /**
   * @function executeUpdate
   * @throws SQLException
   */
  public void executeUpdate() throws SQLException {
    if (prepstmt != null)
      prepstmt.executeUpdate();
  }

  /**
   * @function executeUpdate
   * @throws SQLException
   */
  public void executeBatch() throws SQLException {
    if (prepstmt != null)
      prepstmt.executeBatch();
  }

  /**
   * @function addBatch
   * @param value String
   * @throws SQLException
   */
  public void addBatch(String value) throws SQLException {
    prepstmt.addBatch(value);
  }

  /**
   * @function setString
   * @param index int
   * @param value String
   * @throws SQLException
   */
  public void setString(int index,String value) throws SQLException {
    prepstmt.setString(index, value);
  }

  /**
   * @function setInt
   * @param index int
   * @param value int
   * @throws SQLException
   */
  public void setInt(int index,int value) throws SQLException {
    prepstmt.setInt(index,value);
  }

  /**
   * @function setBoolean
   * @param index int
   * @param value boolean
   * @throws SQLException
   */
  public void setBoolean(int index,boolean value) throws SQLException {
    prepstmt.setBoolean(index,value);
  }

  /**
   * @function setDate
   * @param index int
   * @param value Date
   * @throws SQLException
   */
  public void setDate(int index,Date value) throws SQLException {
    prepstmt.setDate(index,value);
  }

  /**
   * @function setLong
   * @param index int
   * @param value long
   * @throws SQLException
   */
  public void setLong(int index,long value) throws SQLException {
    prepstmt.setLong(index,value);
  }

  /**
   * @function setFloat
   * @param index int
   * @param value float
   * @throws SQLException
   */
  public void setFloat(int index,float value) throws SQLException {
    prepstmt.setFloat(index,value);
  }

  /**
   * @function setBytes
   * @param index int
   * @param value byte[]
   * @throws SQLException
   */
  public void setBytes(int index,byte[] value) throws SQLException{
    prepstmt.setBytes(index,value);
  }

  /**
   * @function setBinaryStream
   * @param index int
   * @param value InputStream
   * @param len int
   * @throws SQLException
   */
  public void setBinaryStream(int index,InputStream value,int len) throws SQLException{
    prepstmt.setBinaryStream(index,value,len);
  }

  /**
   * @function setTimestamp
   * @param index int
   * @param timestamp Timestamp
   * @throws SQLException
   */
  public void setTimestamp(int index,Timestamp timestamp) throws SQLException {
    prepstmt.setTimestamp(index, timestamp);
  }

  /**
   * @function setAutoCommit
   * @param value boolean
   * @throws SQLException
   */
  public void setAutoCommit(boolean value) throws SQLException{
    if(this.conn != null)
      this.conn.setAutoCommit(value);
  }

  /**
   * @function commit
   * @throws SQLException
   */
  public void commit() throws SQLException{
    this.conn.commit();
  }

  /**
   * @function rollback
   * @throws SQLException
   */
  public void rollback() throws SQLException{
    this.conn.rollback();
  }

  /**
   * @function close
   * @throws Exception
   */
  public void close(){
    try{
      if (rs != null) {
        rs.close();
        rs = null;
      }
    }catch(Exception e){
      System.out.println("dbBean close rs error!");
    }finally{
      try{
        if (stmt != null) {
          stmt.close();
          stmt = null;
        }
      }catch(Exception e){
        System.out.println("dbBean close stmt error!");
      }finally{
        try{
          if (prepstmt != null) {
            prepstmt.close();
            prepstmt = null;
          }
        }catch(Exception e){
          System.out.println("dbBean close prepstmt error!");
        }finally{
          try{
            if (conn != null) {
              conn.close();
              conn = null;
            }
          }catch(Exception e){
            System.out.println("dbBean close conn error!");
          }
        }
      }
    }
  }

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

JAVA:数据库操作封装

  • 2007-09-08 19:25
  • 34KB
  • 下载

封装JAVA数据库操作

import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sq...

Java数据库操作封装类

  • 2017-07-09 09:33
  • 5.19MB
  • 下载

小博老师解析Java核心技术 ——JDBC数据库操作类封装

[引言] 我们在学习Java编程时,连接数据库技术(JDBC)是一项必备技能,我们经常需要读取或操作持久性存储的结构化数据。那么现在开始,小博老师就开始为大家讲解JDBC中的常用操作技术,今天我们来...

java数据库操作

  • 2012-10-08 13:42
  • 161KB
  • 下载

Java 数据库操作

  • 2014-09-05 12:31
  • 131KB
  • 下载

java数据库操作

转载自曹胜欢博客专栏数据库访问几乎每一个稍微成型的程序都要用到的知识,怎么高效的访问数据库也是我们学习的一个重点,今天的任务就是总结java访问数据库的方法和有关API,java访问数据库主要用的方法...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)