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

📄 attributemanager.java

📁 我的文件是一个数据添加页面
💻 JAVA
字号:
package kmd.gxml;


import java.util.ArrayList;
import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

import kmd.Debug;
import kmd.commo.SequenceProducer;
import kmd.jdbc.DBConnection;





/**
 * 针对对象类型的属性进行管理的类
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2007</p>
 * <p>Company: 重庆科美达电脑有限公司</p>
 * @author not attributable
 * @version 1.0
 */
public class AttributeManager {
  /**
   * 默认构造符
   */
  public AttributeManager() {}


  /**
   * 新增属性信息,属性编号、所属对象类型的编号,属性名称为必填项,并且对象类型的编号所指的对象类型必须是存在的
   * @param info AttributeInfo 属性信息对象
   * @throws Exception 当保存失败的时候抛出此异常
   */
  public void addAttribute(AttributeInfo info) throws Exception {
    Connection cn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      cn = DBConnection.getConnection();
      addAttribute(info, cn);
    } catch (Exception ex) {
      if (Debug.isJavaBeanDebug) {
        ex.printStackTrace();
      }
      throw ex;
    } finally {
      if (rs != null) {try {rs.close();
      rs = null;
      } catch (Exception e) {}
      }
      if (ps != null) {try {ps.close();
      ps = null;
      } catch (Exception e) {}
      }
      if (cn != null) {try {cn.close();
      cn = null;
      } catch (Exception e) {}
      }
    }
  }


  /**
   * 新增属性信息,属性编号、所属对象类型的编号,属性名称为必填项,并且对象类型的编号所指的对象类型必须是存在的
   * 此方法将有可能参与一个事务
   * @param info AttributeInfo 属性信息对象
   * @param conn Connection 数据库连接
   * @throws Exception 当保存失败的时候抛出此异常
   */
  public void addAttribute(AttributeInfo info, Connection conn) throws
      Exception {
    Connection cn = null;
    PreparedStatement ps = null;
    try {
      cn = conn;
      ps = cn.prepareStatement(
          "INSERT INTO DataAttribute(AId, TId, Name, AttributeType)"
          + " VALUES(?,?,?,?)");
      ps.setLong(1, SequenceProducer.getId());
      ps.setLong(2, info.getTid());
      ps.setString(3, info.getName());
      ps.setString(4, info.getType());
      int state = ps.executeUpdate();
      if (state <= 0) {
        throw new Exception("未能成功新增属性信息。");
      }
    } catch (Exception ex) {
      if (Debug.isJavaBeanDebug) {
        ex.printStackTrace();
      }
      throw ex;
    } finally {
      if (ps != null) {try {ps.close();
      ps = null;
      } catch (Exception e) {}
      }
    }
  }


  /**
   * 修改属性信息,这里可能修改的信息只能是属性的名称
   * @param info AttributeInfo 属性信息
   * @throws Exception 当修改失败的时候抛出此异常
   */
  public void modifyAttribute(AttributeInfo info) throws Exception {
    Connection cn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      cn = DBConnection.getConnection();
      modifyAttribute(info, cn);
    } catch (Exception ex) {
      if (Debug.isJavaBeanDebug) {
        ex.printStackTrace();
      }
      throw ex;
    } finally {
      if (rs != null) {try {rs.close();
      rs = null;
      } catch (Exception e) {}
      }
      if (ps != null) {try {ps.close();
      ps = null;
      } catch (Exception e) {}
      }
      if (cn != null) {try {cn.close();
      cn = null;
      } catch (Exception e) {}
      }
    }
  }


  /**
   * 修改属性信息,这里可能修改的信息只能是属性的名称
   * 此方法可能会参予一个事务
   * @param info AttributeInfo 属性信息
   * @param conn Connection 数据库连接
   * @throws Exception 当修改失败的时候抛出此异常
   */
  public void modifyAttribute(AttributeInfo info, Connection conn) throws
      Exception {
    Connection cn = null;
    PreparedStatement ps = null;
    try {
      cn = conn;
      ps = cn.prepareStatement(
          "UPDATE DataAttribute SET Name = ? WHERE AId = ?");
      ps.setString(1, info.getName());
      ps.setLong(2, info.getAid());
      int state = ps.executeUpdate();
      if (state <= 0) {
        throw new Exception("未能成功修改属性信息。");
      }
    } catch (Exception ex) {
      if (Debug.isJavaBeanDebug) {
        ex.printStackTrace();
      }
      throw ex;
    } finally {
      if (ps != null) {try {ps.close();
      ps = null;
      } catch (Exception e) {}
      }
    }
  }


  /**
   * 删除某一属性信息,以及所有数据对象中包含的此项属性信息
   * @param aid long 属性编号
   * @return int 被删除的属性数量
   * @throws Exception 当删除失败时抛出此异常
   */
  public int deleteAttribute(long aid) throws Exception {
    Connection cn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    int deleteNum = 0;
    try {
      cn = DBConnection.getConnection();
      cn.setAutoCommit(false);
      deleteNum = deleteAttribute(aid, cn);
      cn.commit();
      cn.setAutoCommit(true);
    } catch (Exception ex) {
      if (Debug.isJavaBeanDebug) {
        ex.printStackTrace();
      }
      try {
        if (cn != null && !cn.getAutoCommit()) {
          cn.rollback();
          cn.setAutoCommit(true);
        }
      } catch (Exception e) {}
      throw ex;
    } finally {
      if (rs != null) {try {rs.close();
      rs = null;
      } catch (Exception e) {}
      }
      if (ps != null) {try {ps.close();
      ps = null;
      } catch (Exception e) {}
      }
      if (cn != null) {try {cn.close();
      cn = null;
      } catch (Exception e) {}
      }
    }
    return deleteNum;
  }


  /**
   * 删除某一部分属性信息,以及所有数据对象中包含的这些属性信息
   * @param aid[] long 属性信息的编号列表
   * @return int 被删除的属性数量
   * @throws Exception 当删除失败时抛出此异常
   */
  public int deleteAttribute(long aid[]) throws Exception {
    Connection cn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    int deleteNum = 0;
    try {
      cn = DBConnection.getConnection();
      cn.setAutoCommit(false);
      for (int i = 0; i < aid.length; i++) {
        deleteNum = deleteNum + deleteAttribute(aid[i], cn);
      }
      cn.commit();
      cn.setAutoCommit(true);
    } catch (Exception ex) {
      if (Debug.isJavaBeanDebug) {
        ex.printStackTrace();
      }
      try {
        if (cn != null && !cn.getAutoCommit()) {
          cn.rollback();
          cn.setAutoCommit(true);
        }

      } catch (Exception e) {}
      throw ex;
    } finally {
      if (rs != null) {try {rs.close();
      rs = null;
      } catch (Exception e) {}
      }
      if (ps != null) {try {ps.close();
      ps = null;
      } catch (Exception e) {}
      }
      if (cn != null) {try {cn.close();
      cn = null;
      } catch (Exception e) {}
      }
    }
    return deleteNum;
  }


  /**
   * 删除某一属性信息,以及所有数据对象中包含的此项属性信息
   * 此方法将有可能参与一个事务
   * @param aid long 属性编号
   * @param conn Connection 数据库连接
   * @return int 被删除的属性数量
   * @throws Exception 当删除失败时抛出此异常
   */
  public int deleteAttribute(long aid, Connection conn) throws Exception {
    Connection cn = null;
    PreparedStatement ps = null;
    int deleteNum = 0;
    try {
      cn = conn;
      ps = cn.prepareStatement("DELETE FROM DataAttribute WHERE AId = ?");
      ps.setLong(1, aid);
      deleteNum = ps.executeUpdate();
      ps.close();
      ps = null;
      ps = cn.prepareStatement("DELETE FROM CharAttribute WHERE AId = ?");
      ps.setLong(1, aid);
      ps.executeUpdate();
      ps.close();
      ps = null;
      ps = cn.prepareStatement("DELETE FROM DoubleAttribute WHERE AId = ?");
      ps.setLong(1, aid);
      ps.executeUpdate();
      ps.close();
      ps = null;
      ps = cn.prepareStatement("DELETE FROM IntegerAttribute WHERE AId = ?");
      ps.setLong(1, aid);
      ps.executeUpdate();
      ps.close();
      ps = null;
      ps = cn.prepareStatement("DELETE FROM BooleanAttribute WHERE AId = ?");
      ps.setLong(1, aid);
      ps.executeUpdate();
      ps.close();
      ps = null;
      ps = cn.prepareStatement("DELETE FROM ClobAttribute WHERE AId = ?");
      ps.setLong(1, aid);
      ps.executeUpdate();
      ps.close();
      ps = null;
    } catch (Exception ex) {
      if (Debug.isJavaBeanDebug) {
        ex.printStackTrace();
      }
      throw ex;
    } finally {
      if (ps != null) {try {ps.close();
      ps = null;
      } catch (Exception e) {}
      }
    }
    return deleteNum;
  }


  /**
   * 得到某一属性信息
   * @param aid long 属性编号
   * @return AttributeInfo 属性信息,当出现错误或未找到数据时返回空对象
   */
  public AttributeInfo getAttributeInfo(long aid) {
    Connection cn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    AttributeInfo info = null;
    try {
      cn = DBConnection.getConnection();
      ps = cn.prepareStatement("SELECT * FROM DataAttribute WHERE AId = ?");
      ps.setLong(1, aid);
      rs = ps.executeQuery();
      if (rs.next()) {
        info = getInfo(rs);
      }
    } catch (Exception ex) {
      if (Debug.isJavaBeanDebug) {
        ex.printStackTrace();
      }
      info = null;
    } finally {
      if (rs != null) {try {rs.close();
      rs = null;
      } catch (Exception e) {}
      }
      if (ps != null) {try {ps.close();
      ps = null;
      } catch (Exception e) {}
      }
      if (cn != null) {try {cn.close();
      cn = null;
      } catch (Exception e) {}
      }
    }
    return info;
  }


  /**
   * 得到某一对象类型的所有属性列表
   * @param tid long  对象类型的编号
   * @return ArrayList 当出现错误或未找到数据时返回一个长度为0的ArrayList
   */
  public ArrayList getAttributeList(long tid) {
    Connection cn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    ArrayList infoList = new ArrayList();
    try {
      cn = DBConnection.getConnection();
      ps = cn.prepareStatement("SELECT * FROM DataAttribute WHERE TId = ?");
      ps.setLong(1, tid);
      rs = ps.executeQuery();
      while (rs.next()) {
        infoList.add(getInfo(rs));
      }
    } catch (Exception ex) {
      if (Debug.isJavaBeanDebug) {
        ex.printStackTrace();
      }
      infoList.clear();
    } finally {
      if (rs != null) {try {rs.close();
      rs = null;
      } catch (Exception e) {}
      }
      if (ps != null) {try {ps.close();
      ps = null;
      } catch (Exception e) {}
      }
      if (cn != null) {try {cn.close();
      cn = null;
      } catch (Exception e) {}
      }
    }
    return infoList;
  }


  /**
   * 得到某一数据类型下的所有属性的编号
   * @param tid long 类型编号
   * @return long[]
   */
  public ArrayList getAids(long tid) {
    Connection cn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    ArrayList aids = new ArrayList();
    try {
      cn = DBConnection.getConnection();
      ps = cn.prepareStatement("SELECT Aid FROM DataAttribute WHERE Tid = ?");
      ps.setLong(1, tid);
      rs = ps.executeQuery();
      while (rs.next()) {
        aids.add(rs.getString("Aid"));
      }
    } catch (Exception ex) {
      if (Debug.isJavaBeanDebug) {
        ex.printStackTrace();
      }
      aids.clear();
    } finally {
      if (rs != null) {try {rs.close();
      rs = null;
      } catch (Exception e) {}
      }
      if (ps != null) {try {ps.close();
      ps = null;
      } catch (Exception e) {}
      }
      if (cn != null) {try {cn.close();
      cn = null;
      } catch (Exception e) {}
      }
    }
    return aids;
  }


  /**
   * 根据结果集得到对象信息
   * @param rs ResultSet
   * @return AttributeInfo
   */
  private AttributeInfo getInfo(ResultSet rs) {
    AttributeInfo info = null;
    ResultSetMetaData rsmd = null;
    try {
      rsmd = rs.getMetaData();
      info = new AttributeInfo();
      for (int i = 0; i < rsmd.getColumnCount(); i++) {
        String column = rsmd.getColumnName(i + 1).toLowerCase();
        if (column.equals("aid")) {
          info.setAid(rs.getLong("Aid"));
          break;
        } else if (column.equals("tid")) {
          info.setTid(rs.getLong("Tid"));
          break;
        } else if (column.equals("name")) {
          info.setName(rs.getString("Name"));
          break;
        } else if (column.equals("attributetype")) {
          info.setType(rs.getString("AttributeType"));
          break;
        } else {
          break;
        }
      }
    } catch (Exception ex) {
      info = null;
    }
    return info;
  }
}

⌨️ 快捷键说明

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