?? category.java
字號:
package book.treeview;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
public class Category implements TreeviewElement {
protected int ID;//代表數據庫中category_id列;
protected int parentID;//代表數據庫中parent_category_id列;
protected String categoryName;//代表數據庫中category_name列;
protected String activeStatus="Y";//代表數據庫中active_status列;
public Category() {//無參的構造方法
this.ID = -1;
}
public Category(int id) {//有參的構造方法,參數為portlet_id
this.ID = id;
if (!FromDb())//如果有找到該id的porlet
this.ID = -1;
}
public boolean FromDb() {//從數據庫中讀出,并更新bean
int row = -1;
//讀記錄的sql語句
String sql = "select * from product_category where category_id=" + this.ID
+ " and active_status='Y'";
ResultSet rs = DbManager.getResultSet(sql);//執行sql語句并返回ResultSet
try {
rs.last();//移動到最后一行
row = rs.getRow();//得到總記錄數
if (row == 1) {//如果只查詢到一條記錄,則代表該記錄存在并更新該類的屬性
this.parentID = rs.getInt("PARENT_CATEGORY_ID");
this.categoryName = rs.getString("CATEGORY_NAME");
this.activeStatus = rs.getString("ACTIVE_STATUS");
return true;
} else
return false;
} catch (SQLException e) {
e.printStackTrace();
return false;
} finally {
try {//最后關閉ResutltSet,Statement.并釋放連接
if (rs != null)
rs.close();
if (rs.getStatement() != null)
rs.getStatement().close();
DbManager.releaseConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public String getActiveStatus() {
return activeStatus;
}
public void setActiveStatus(String activeStatus) {
this.activeStatus = activeStatus;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public int getID() {
return ID;
}
public void setID(int id) {
ID = id;
}
public int getParentID() {
return parentID;
}
public void setParentID(int parentID) {
this.parentID = parentID;
}
public String getNodeName() {
return getCategoryName();
}
public boolean canExpand() {
String sql ="select category_id from product_category where parent_category_id="+getID();
ResultSet rs = DbManager.getResultSet(sql);//執行sql語句并返回ResultSet
try {
rs.last();//移動到最后一行
int row = rs.getRow();//得到總記錄數
if (row <= 0)
return false;
else
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
} finally {
try {//最后關閉ResutltSet,Statement.并釋放連接
if (rs != null)
rs.close();
if (rs.getStatement() != null)
rs.getStatement().close();
DbManager.releaseConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public TreeviewElement[] getChildren() {
String sql ="select category_id from product_category where parent_category_id="+getID();
ResultSet rs = DbManager.getResultSet(sql);//執行sql語句并返回ResultSet
try {
rs.last();//移動到最后一行
int row = rs.getRow();//得到總記錄數
if (row <= 0) {//如果沒有子結點
return null;//返回null
}
else{//如果有子結點
Vector vData = new Vector();
rs.beforeFirst();
while (rs.next())
vData.add(""+rs.getInt("CATEGORY_ID"));
TreeviewElement[] children = new TreeviewElement[vData.size()];
for (int i=0;i<vData.size();i++)
{
int id = Integer.parseInt((String)vData.get(i));
children[i] = new Category(id);
}
return children;//返回該記錄的所有子結點
}
} catch (SQLException e) {
e.printStackTrace();
return null;
} finally {
try {//最后關閉ResutltSet,Statement.并釋放連接
if (rs != null)
rs.close();
if (rs.getStatement() != null)
rs.getStatement().close();
DbManager.releaseConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -