📄 categories.java
字号:
package Information;
import java.sql.*;
import java.util.*;
import comm.*;
public class Categories {
private int CateId; // 分类编号
private String CateTitle; // 分类名称
// 设置、获取分类属性
public void setCateId(int CateId){
this.CateId = CateId;
}
public int getCateId(){
return this.CateId;
}
public void setCateTitle(String CateTitle){
this.CateTitle = CateTitle;
}
public String getCateTitle(){
return this.CateTitle;
}
// 定义字符串操作对象
StringOper so = new StringOper();
// 判断是否存在此分类,如果存在则结果为true,并返回分类详细信息
public boolean getCategory()throws Exception
{
int conditionNo = 0;
// 连接数据库
DBOper o_DBOper = new DBOper();
ResultSet rs = null;
// 设定查询语句
String sql = "Select * From Categories";
String condition = " where";
try
{
// 设置条件语句
if(CateId != 0)
{
condition += " CateId="+CateId;
conditionNo++;
}
if(CateTitle != null)
{
if(conditionNo > 0)
{
condition += " and";
}
condition += " CateTitle='"+CateTitle+"'";
conditionNo++;
}
if(conditionNo > 0)
{
sql += condition;
}
// 执行查询
rs = o_DBOper.getResultSet(sql);
// 如果存在结果则返回true和分类信息
if(rs.next())
{
setCateId(rs.getInt("CateId"));
setCateTitle(so.ReplaceNull(rs.getString("CateTitle"))); // 如果分类名称为null则转换为“--”
return true;
}
}// 抛出异常
catch(Exception e)
{
throw new Exception(e.getMessage());
}// 关闭数据库
finally
{
try {o_DBOper.close();} catch (Exception e) { System.out.print(e.toString());}
}
return false;
}
// 得到多个分类信息的对象数组
public Vector getMoreCategories()throws Exception
{
Vector v_Category = new Vector(); // 定义数组变量
int conditionNo = 0; // 定义查询条件个数变量
// 连接数据库
DBOper o_DBOper = new DBOper();
ResultSet rs = null;
// 查询语句
String sql = "Select * From Categories";
String condition = " where ";
try
{
// 如果编号不为0,则增加查询条件
if(CateId != 0)
{
condition += "CateId="+CateId;
conditionNo++;
}
// 如果名称不为空,则增加查询条件
if(CateTitle != null)
{
if(conditionNo > 0)
{
condition += " and";
}
condition += " CateTitle='"+CateTitle+"'";
conditionNo++;
}
// 当条件个数大于0时,连接查询语句
if(conditionNo > 0)
{
sql += condition;
}
sql += " order by CateId";
// 执行数据库查询操作,返回结果集
rs = o_DBOper.getResultSet(sql);
// 如果结果集不为空,则将信息放入对象中
while(rs.next())
{
Categories o_Cate2 = new Categories();
o_Cate2.setCateId(rs.getInt("CateId"));
o_Cate2.setCateTitle(so.ReplaceNull(rs.getString("CateTitle")));
// 把对象放入Vector数组中
v_Category.add(o_Cate2);
}
}
// 如果有错误发生,则抛出异常
catch(Exception e)
{
throw new Exception(e.getMessage());
}
finally
{
try {o_DBOper.close();} catch (Exception e) { System.out.print(e.toString());}
}
return v_Category;
}
//删除分类
public void DeleteCategory()throws Exception
{
// 连接数据库
DBOper o_DBOper = new DBOper();
ResultSet rs = null;
// 删除语句
String sql_dlt = "Delete From Categories where CateId =" + CateId ;
try
{
// 执行删除操作
o_DBOper.getResultSet(sql_dlt);
}
catch(Exception e)
{
throw new Exception(e.getMessage());
}
finally
{
try {o_DBOper.close();} catch (Exception e) { System.out.print(e.toString());}
}
}
//插入分类
public void CreateCategory()throws Exception
{
// 建立数据库对象
DBOper o_DBOper = new DBOper();
ResultSet rs = null;
// 定义INSERT语句,使用序列S_CID自动生成CateId的字段值
String sql = "Insert into Categories(CateId,CateTitle) values(S_CId.nextval,'" + CateTitle + "')";
try {
// 执行操作
o_DBOper.getResultSet(sql);
}
// 如果有错误,则抛出异常处理
catch(Exception e) {
throw new Exception(e.getMessage());
}
finally{
// 关闭数据库连接
try {o_DBOper.close();} catch (Exception e) { System.out.print(e.toString());}
}
}
// 更新类别
public void UpdateCategory()throws Exception
{
// 连接数据库
DBOper o_DBOper = new DBOper();
ResultSet rs = null;
// 定义更新语句
String sql = "Update Categories Set CateTitle='" + CateTitle + "' where CateId=" + CateId ;
// System.out.println(sql);
try{
// 执行SQL语句
o_DBOper.DataUpdate(sql);
}
catch(Exception e){
throw new Exception(e.getMessage());
}
finally{
try {o_DBOper.close();} catch (Exception e) { System.out.print(e.toString());}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -