📄 topicbean.java
字号:
package oa.bean;
import java.util.*;
import java.sql.*;
import oa.main.*;
public class TopicBean extends ParentBean {
int type = 0;
String id = "-1";
//设置记录唯一编号
public void setID(String i) {
id = i;
}
//得到专题名称
//取得数据表内容
public Vector getData() {
Vector vect = new Vector();
String sql = "";
sql = "select * from news.NB_TOPIC order by MENUID1,JBXH";
ResultSet rs = selectRecord(sql);
Statement stmt = null;
try {
//取得列数和列名
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
while (rs.next()) {
Hashtable hash = new Hashtable();
for (int i = 1; i <= cols; i++) {
String field = ds.toString(rsmd.getColumnName(i));
String value = ds.toString(rs.getString(i));
hash.put(field, value);
}
vect.add(hash);
}
} catch (Exception e) {
System.out.println("运行时出错:" + e);
} finally {
if (rs != null)
try {
stmt = rs.getStatement();
rs.close();
} catch (Exception e) {
System.out.println("关闭记录集rs时出错" + e);
}
if (stmt != null)
try {
stmt.close();
} catch (Exception e) {
System.out.println("关闭声明时statement出错" + e);
}
}
return vect;
}
//按条件取得数据表内容
public Vector getData(String name) {
Vector vect = new Vector();
String sql = "";
/*
* sql = "select * from news.NB_TOPIC where TYPE='"+name+"' and status=0
* order by TYPE,JBXH"; ResultSet rs = selectRecord(sql);
*
* //取得列数和列名 ResultSetMetaData rsmd = rs.getMetaData(); int cols =
* rsmd.getColumnCount(); while(rs.next()) { Hashtable hash = new
* Hashtable(); for(int i=1;i <=cols;i++) { String field =
* ds.toString(rsmd.getColumnName(i)); String value =
* ds.toString(rs.getString(i)); hash.put(field,value); }
* vect.add(hash); } rs.close(); Statement stmt = rs.getStatement();
* if(stmt!=null) stmt.close(); return vect;
*/
int menuid1 = toID("MENU", "MENU1", "ID1", name);
return getData(menuid1);
}
public Vector getData(int menuid1) {
Vector vect = new Vector();
String sql = "";
sql = "select * from news.NB_TOPIC where MENUID1='" + menuid1
+ "' and status=0 order by JBXH";
ResultSet rs = selectRecord(sql);
Statement stmt = null;
try {
//取得列数和列名
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
while (rs.next()) {
Hashtable hash = new Hashtable();
for (int i = 1; i <= cols; i++) {
String field = ds.toString(rsmd.getColumnName(i));
String value = ds.toString(rs.getString(i));
hash.put(field, value);
}
vect.add(hash);
}
} catch (Exception e) {
System.out.println("运行时出错:" + e);
} finally {
if (rs != null)
try {
stmt = rs.getStatement();
rs.close();
} catch (Exception e) {
System.out.println("关闭记录集rs时出错" + e);
}
if (stmt != null)
try {
stmt.close();
} catch (Exception e) {
System.out.println("关闭声明时statement出错" + e);
}
}
return vect;
}
//取得数据表中的某一条记录
public Hashtable getOneData() {
Hashtable hash = new Hashtable();
String sql = "";
sql = "select * from news.NB_TOPIC where ID = " + id + "";
ResultSet rs = selectRecord(sql);
Statement stmt = null;
try {
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
if (rs.next()) {
for (int i = 1; i <= cols; i++) {
String field = ds.toString(rsmd.getColumnName(i));
String value = ds.toString(rs.getString(i));
hash.put(field, value);
}
}
} catch (Exception e) {
System.out.println("运行时出错:" + e);
} finally {
if (rs != null)
try {
stmt = rs.getStatement();
rs.close();
} catch (Exception e) {
System.out.println("关闭记录集rs时出错" + e);
}
if (stmt != null)
try {
stmt.close();
} catch (Exception e) {
System.out.println("关闭声明时statement出错" + e);
}
}
return hash;
}
//增加专题列表信息
public int addTopic(Hashtable hash) {
//System.out.println("hash:"+hash);
int intId = db.makeID("NB_TOPIC", "ID", "", "", true);
String strName = ds.toString((String) hash.get("NAME"));
String strType = ds.toString((String) hash.get("TYPE"));
String strIntroduce = ds.toString((String) hash.get("INTRODUCE"));
String strAddTime = ds.toString((String) hash.get("ADDTIME"));
String strStatus = ds.toString((String) hash.get("STATUS"));
String strJBXH = ds.toString((String) hash.get("JBXH"));
String strMenuID1 = toName("MENU", "MENU1", "ID1", strType);
//String strType = toName("MENU","ID1","MENU1",strMenuID1);
String sql = "";
sql = "select * from news.nb_topic where MENUID1 =" + strMenuID1
+ " and NAME = '" + strName + "'";
ResultSet rs = selectRecord(sql);
Statement stmt = null;
try {
if (rs.next())
return -5;//已经存在此专题
} catch (Exception e) {
System.out.println("运行时出错:" + e);
} finally {
if (rs != null)
try {
stmt = rs.getStatement();
rs.close();
} catch (Exception e) {
System.out.println("关闭记录集rs时出错" + e);
}
if (stmt != null)
try {
stmt.close();
} catch (Exception e) {
System.out.println("关闭声明时statement出错" + e);
}
}
int intJBXH = setJBXH(strMenuID1, strJBXH);
Vector vect = new Vector();
vect.add("NB_TOPIC");
vect.add(addVector("ID", String.valueOf(intId), "NUM"));
vect.add(addVector("NAME", strName, "CHAR"));
vect.add(addVector("TYPE", strType, "CHAR"));
vect.add(addVector("INTRODUCE", strIntroduce, "CHAR"));
vect.add(addVector("ADDTIME", strAddTime, "CHAR"));
vect.add(addVector("STATUS", strStatus, "NUM"));
vect.add(addVector("JBXH", String.valueOf(intJBXH), "NUM"));
vect.add(addVector("MENUID1", String.valueOf(strMenuID1), "NUM"));
return insertRecord(vect);
}
//新增的时候设置级别序号
public int setJBXH(String strMenuID1, String jbxh) {
int intJBXH = makeID("NB_TOPIC", "MENUID1", "JBXH", strMenuID1, false);
if (jbxh.equals(""))
jbxh = "0";
int intxh = Integer.parseInt(jbxh);
if (intxh > 0 && intxh < intJBXH) {
intJBXH = intxh;
String sql = "";
sql = "update nb_topic set JBXH=JBXH+1 where MENUID1 = '"
+ strMenuID1 + "' and JBXH>=" + intxh;
deleteRecord(sql);
}
return intJBXH;
}
//修改专题列表信息
public int modTopic(Hashtable hash) {
String strName = ds.toString((String) hash.get("NAME"));
String strType = ds.toString((String) hash.get("TYPE"));
String strIntroduce = ds.toString((String) hash.get("INTRODUCE"));
String strAddTime = ds.toString((String) hash.get("ADDTIME"));
String strStatus = ds.toString((String) hash.get("STATUS"));
String strJBXH = ds.toString((String) hash.get("JBXH"));
String strMenuID1 = toName("MENU", "MENU1", "ID1", strType);
// String strType = toName("MENU","ID1","MENU1",strMenuID1);
String sql = "";
String stroName = "";
String stroType = "";
String stroMENUID1 = "";
sql = "select * from news.nb_topic where ID = " + id + "";
ResultSet rs1 = selectRecord(sql);
Statement stmt = null;
try {
if (rs1.next()) {
stroName = ds.toString(rs1.getString("NAME"));//取得原来的专题名称
//stroType = ds.toString(rs1.getString("TYPE"));//取得原来的专题类型
stroMENUID1 = ds.toString(rs1.getString("MENUID1"));
}
} catch (Exception e) {
System.out.println("运行时出错:" + e);
} finally {
if (rs1 != null)
try {
stmt = rs1.getStatement();
rs1.close();
} catch (Exception e) {
System.out.println("关闭记录集rs时出错" + e);
}
if (stmt != null)
try {
stmt.close();
} catch (Exception e) {
System.out.println("关闭声明时statement出错" + e);
}
}
//被修改名称
if (!stroName.equals(strName)) {
sql = "select * from news.nb_topic where MENUID1 = '" + strMenuID1
+ "' and NAME = '" + strName + "'";
ResultSet rs = selectRecord(sql);
try {
if (rs.next())
return -5;
} catch (Exception e) {
System.out.println("运行时出错:" + e);
} finally {
if (rs != null)
try {
stmt = rs.getStatement();
rs.close();
} catch (Exception e) {
System.out.println("关闭记录集rs时出错" + e);
}
if (stmt != null)
try {
stmt.close();
} catch (Exception e) {
System.out.println("关闭声明时statement出错" + e);
}
}
}
if (strMenuID1.equals(stroMENUID1))//strType.equals(stroType)
{
int intJBXH = setJBXH(id, strMenuID1, strJBXH);//取得级别序号strType
//System.out.println("取得级别序号:"+intJBXH);
Vector vect = new Vector();
vect.add("NB_TOPIC");
vect.add(addVector("NAME", strName, "CHAR"));
vect.add(addVector("TYPE", strType, "CHAR"));
vect.add(addVector("INTRODUCE", strIntroduce, "CHAR"));
vect.add(addVector("ADDTIME", strAddTime, "CHAR"));
vect.add(addVector("STATUS", strStatus, "NUM"));
vect.add(addVector("JBXH", String.valueOf(intJBXH), "NUM"));
vect.add(addVector("MENUID1", stroMENUID1, "NUM"));
vect.add("ID=" + id + "");
//System.out.println("修改vect:"+vect);
return updateRecord(vect);
} else {
if (articleExist(id))
return -5;
addTopic(hash);
return delData();
}
}
//修改的时候设置级别序号
public int setJBXH(String id, String strMenuID1, String jbxh) {
int intJBXH = 0;
intJBXH = makeID("NB_TOPIC", "MENUID1", "JBXH", strMenuID1, false);
String strpjbxh = "1";
String sql = "";
sql = "select * from news.nb_topic where ID=" + id + "";
ResultSet rs = selectRecord(sql);
Statement stmt = null;
try {
if (rs.next())
strpjbxh = ds.toString(rs.getString("JBXH"));
} catch (Exception e) {
System.out.println("运行时出错:" + e);
} finally {
if (rs != null)
try {
stmt = rs.getStatement();
rs.close();
} catch (Exception e) {
System.out.println("关闭记录集rs时出错" + e);
}
if (stmt != null)
try {
stmt.close();
} catch (Exception e) {
System.out.println("关闭声明时statement出错" + e);
}
}
int intpjbxh = Integer.parseInt(strpjbxh);//原来的序号
if (jbxh.equals(""))
jbxh = "0";
int intxh = Integer.parseInt(jbxh);
if (intxh <= 0 || intxh >= intJBXH) {
intxh = intJBXH - 1;//修改后序号
}
//序号向前移动
if (intpjbxh > intxh) {
sql = "update nb_topic set jbxh=jbxh+1 where MENUID1 = '"
+ strMenuID1 + "' and JBXH>=" + intxh + " and JBXH<"
+ intpjbxh + "";
deleteRecord(sql);
}
//序号向后移动
if (intpjbxh < intxh) {
sql = "update nb_topic set jbxh=jbxh-1 where MENUID1 = '"
+ strMenuID1 + "' and JBXH>" + intpjbxh + " and JBXH<="
+ intxh + "";
deleteRecord(sql);
}
return intxh;
}
//删除专题列表信息
public int delData() {
if (articleExist(id))
return -5;
String sql = "";
String strTYPE = "";
String strJBXH = "0";
String strMenuID1 = "";
int intJBXH = Integer.parseInt(strJBXH);
sql = "select * from news.nb_topic where ID = " + id + "";
ResultSet rs = selectRecord(sql);
Statement stmt = null;
try {
if (rs.next()) {
strJBXH = ds.toString(rs.getString("JBXH"));
//strTYPE = ds.toString(rs.getString("TYPE"));
strMenuID1 = ds.toString(rs.getString("MENUID1"));
}
} catch (Exception e) {
System.out.println("运行时出错:" + e);
} finally {
if (rs != null)
try {
stmt = rs.getStatement();
rs.close();
} catch (Exception e) {
System.out.println("关闭记录集rs时出错" + e);
}
if (stmt != null)
try {
stmt.close();
} catch (Exception e) {
System.out.println("关闭声明时statement出错" + e);
}
}
intJBXH = Integer.parseInt(strJBXH);
sql = "update news.nb_topic set JBXH = JBXH-1 where MENUID1 = '"
+ strMenuID1 + "' and JBXH>" + intJBXH + "";
deleteRecord(sql);
sql = "delete from news.NB_TOPIC where ID = " + id + "";
return deleteRecord(sql);
}
//所有所属类别
public Vector getSort() {
Vector vect = new Vector();
String sql = "";
int i = 0;
sql = "select distinct MENU1,id1 from news.MENU order by id1";
ResultSet rs = selectRecord(sql);
Statement stmt = null;
try {
while (rs.next()) {
Hashtable hash = new Hashtable();
String field = ds.toString(String.valueOf(i));
String value = ds.toString(rs.getString("MENU1"));
String id = ds.toString(rs.getString("ID1"));
hash.put(field, value);
hash.put("MENUID1", id);
vect.add(hash);
i++;
}
} catch (Exception e) {
System.out.println("运行时出错:" + e);
} finally {
if (rs != null)
try {
stmt = rs.getStatement();
rs.close();
} catch (Exception e) {
System.out.println("关闭记录集rs时出错" + e);
}
if (stmt != null)
try {
stmt.close();
} catch (Exception e) {
System.out.println("关闭声明时statement出错" + e);
}
}
return vect;
}
//判断专题下是否有文章
public boolean articleExist(String id) {
String sql = "";
sql = "select * from news.article where TOPICID=" + id + "";
ResultSet rs = selectRecord(sql);
Statement stmt = null;
try {
if (rs.next())
return true;
} catch (Exception e) {
System.out.println("运行时出错:" + e);
} finally {
if (rs != null)
try {
stmt = rs.getStatement();
rs.close();
} catch (Exception e) {
System.out.println("关闭记录集rs时出错" + e);
}
if (stmt != null)
try {
stmt.close();
} catch (Exception e) {
System.out.println("关闭声明时statement出错" + e);
}
}
return false;
}
public static void main(String args[]) {
TopicBean mb = new TopicBean();
//mb.setMain(hash);
//mb.setPic(hash);
System.out.println(mb.articleExist("1"));
System.out.println("\r\n\r\nend");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -