?? attributemanager.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 + -