⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbconnectionmanager.java

📁 三层架构实现左侧菜单
💻 JAVA
字号:
package database;

import java.sql.*;

//import oracle.jdbc.driver.*;

import javax.naming.NamingException;



public class DBConnectionManager

{


  protected transient Connection dbConnection = null;


  protected transient CallableStatement callStat = null;

  
  protected transient Statement stmt = null;


  protected transient ResultSet resultSet = null;


  private transient PreparedStatement PrepStmt = null;


  public DBConnectionManager() {
  }
  
  DBConnection db=new DBConnection();

  public void Open(String path) throws SQLException, ClassNotFoundException {
	  
//	  dbConnection=db.getConnFromProperties();
	  dbConnection=db.getConn(path);
	  stmt=dbConnection.createStatement();
  }

  public Statement CreateStatement(Connection dbConnection) throws SQLException {
    Statement stmt = dbConnection.createStatement();
    return stmt;
  }
 

  
  public DatabaseMetaData getMetaData() throws SQLException {
	 	DatabaseMetaData dbMeta = dbConnection.getMetaData();   
	    return dbMeta;
	  }
  
  public PreparedStatement getPrepStmt(String StrSql) throws SQLException {
    PrepStmt = dbConnection.prepareStatement(StrSql);
    return PrepStmt;
  }

 
  public void AddBatch(String StrSql) throws SQLException {
    stmt.addBatch(StrSql);
  }

  
  public int[] ExecBatch() throws SQLException {
    return (stmt.executeBatch());
  }

 
  public void SetMaxTime(Statement stmt, int times) throws SQLException {
    stmt.setQueryTimeout(times);
  }

  
  public Connection getConnection() throws SQLException {
    return this.dbConnection;
  }

  
  public void ExecuteSQL(String sqlSentence) throws SQLException {
    resultSet = stmt.executeQuery(sqlSentence);
  }

  
  public ResultSet QueryRs(String sqlSentence) throws SQLException {
    resultSet = stmt.executeQuery(sqlSentence);
    return resultSet;
  }

  
  public int UpdateSQL(String sqlSentence) throws SQLException,NamingException {
    int i=-1;
    i=stmt.executeUpdate(sqlSentence);
    return i;
  }

  
  public void ExecuteCall(String callName, String[] Volume) throws SQLException {
//    String re = "";
    int i;
    i = 0;
    try {
      callStat = dbConnection.prepareCall("{ call " + callName + " }");
      for (i = 0; i < Volume.length; i++) {
        callStat.setString(i + 1, Volume[i]);
      }
      callStat.execute();
    }
    catch (SQLException ex) {
      System.err.println("getSequence :" + ex);
    }
  }


//  public void DoAffair(SessionContext sessionContext, Collection coll) throws
//      SQLException {
//    UserTransaction ut = sessionContext.getUserTransaction();
//    try {
//      ut.begin();//事务开始
//      this.Open();//打开数据库连接
//      Iterator oi = (java.util.Iterator) coll.iterator();
//      while (oi.hasNext()) {
//        String sql = (String) oi.next();
//        this.UpdateSQL(sql);
//      }
//      this.Close();//关闭数据库连接
//      ut.commit();//提交事务
//    }
//    catch (Exception ex) {
//      try {
//        ut.rollback();//回滚
//        this.Close();//关闭数据库连接
//        throw ex;
//      }
//      catch (Exception e) {
//        this.Close();//关闭数据库连接
//      }
//    }
//  }

  
//  public void dump() {
//    ic = null;
//    dataSource = null;
//  }

  
  public void Close() {
    CloseStmt();
    CloseRs();
    CloseCon();
    CloseCallStm();
    ClosePrepStmt();
//    dump();
  }

  
  public void CloseRs() {
    try {
      if (resultSet != null) {
        resultSet.close();
      }
    }
    catch (Exception ex) {}
  }

  /**
   * 关闭数据库连接
   */
  public void CloseCon() {
    try {
      if (dbConnection != null) {
        dbConnection.close();
      }
    }
    catch (Exception ex) {}
  }

  /**
   * 关闭Statement
   */
  public void CloseStmt() {
    try {
      if (stmt != null) {
        stmt.close();
      }
    }
    catch (Exception ex) {}
  }

  /**
   * 关闭预处理语句
   */
  public void ClosePrepStmt() {
    try {
      if (PrepStmt != null) {
        PrepStmt.close();
      }
    }
    catch (Exception ex) {}
  }

  /**
   * 关闭CallAbleStatement
   */
  public void CloseCallStm() {
    try {
      if (callStat != null) {
        callStat.close();
      }
    }
    catch (Exception ex) {}
  }

  public void setAutoCommit(boolean isAutoCommit){
	  try {
		dbConnection.setAutoCommit(isAutoCommit);
	} catch (SQLException e) {
		e.printStackTrace();
	}
 }
  public void commit(){
	  try {
		dbConnection.commit();
	} catch (SQLException e) {
		e.printStackTrace();
	}
 }
  public void rollback(){
	  try {
		dbConnection.rollback();
	} catch (SQLException e) {
		e.printStackTrace();
	}
 }
  
//public static void main(String[] args) {
//	BaseDAO basedao=new BaseDAO();
//	try {
//		basedao.Open();
//	} catch (SQLException e) {
//		// TODO 自动生成 catch 块
//		e.printStackTrace();
//	}
//	
//}
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -