📄 functions.java
字号:
package news;
/**
* @author WineFox
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
import news.sql_data;
import java.sql.*;
public class functions {
sql_data sqlbean=new sql_data();
int idd;
public static void main (String [] args) {
System.out.println("functions.java compiled successfully!");}
public ResultSet sql_search(String field,String kt,String keyword)
{String sql=null;
if (field.equals("all"))
{
if (kt.equals("all")){//判断是否使在所有的类别中检索
sql=("select * from news where news like '%"+keyword+"%' or title like '%"+keyword+"%' order by id desc");
}
else{
sql=("select * from news where (news like '%"+keyword+"%' or title like '%"+keyword+"%') and parentclass='"+kt+"' order by id desc");
}
}
if (field.equals("title"))
{if (kt.equals("all")){//判断是否使在所有的类别中检索
sql=("select * from news where title like '%"+keyword+"%' order by id desc");
}
else{
sql=("select * from news where ( title like '%"+keyword+"%') and parentclass='"+kt+"' order by id desc");
}
}
if (field.equals("content"))
{if (kt.equals("all")){//判断是否使在所有的类别中检索
sql=("select * from news where news like '%"+keyword+"%' order by id desc");
}
else{
sql=("select * from news where (news like '%"+keyword+"%' ) and parentclass='"+kt+"' order by id desc");
}
}
ResultSet rs=sqlbean.executeQuery(sql);
return rs;
}
public void del_class(String action,String id,String sort)
{
if (action.equals("delsec")) {//当要删除的是二级分类时
idd = Integer.parseInt(id);
String sql="delete from class where id="+idd;
sqlbean.executeDelete(sql);
//调用sqlbean的executeDelete方法在class表中删除指定类别
sql="delete from news where class='"+sort+"'";
sqlbean.executeDelete(sql);
//调用sqlbean的executeDelete方法在news表中删除指定类别的所有记录
}
if (action.equals("delroot")) {//当要删除的为一级分类
idd = Integer.parseInt(id);
//取得提交来的参数值
String sql="delete from class where parentid="+idd+" or id="+idd;
sqlbean.executeDelete(sql);
//调用sqlbean的executeDelete方法在class表中删除指定一级分类
sqlbean.executeDelete("delete from news where parentclass='"+sort+"'");
//调用sqlbean的executeDelete方法在news表中删除指定一级类别的所有记录
sqlbean.executeDelete("delete from admin where authority='"+sort+"'");
// 调用sqlbean的executeDelete方法在admin表中删除本类的管理员
}
}
public void edit_class(String rootid,String id,String curentclass,String parent){
try{
if (rootid.equals("0"))//判断是要添加的是一级分类还是二级分类
{int idd = Integer.parseInt(id);
String sql="update class set class='"+curentclass+"' where id="+idd+"";
//修改一级分类的SQL语句
sqlbean.executeUpdate(sql);
sqlbean.executeUpdate("update admin set authority='"+curentclass+"' where authority='"+parent+"'");
//修改本类管理员信息
sql="update class set parentclass='"+curentclass+"' where parentid="+idd+"";
//修改其下属的二级分类的SQL语句
sqlbean.executeUpdate(sql);
//调用sqlbean的executeInsert方法将记录更新到数据库中
sql="update news set parentclass= '"+curentclass+"' where parentclass='"+parent+"'";
sqlbean.executeUpdate(sql);
// 将记录中的类别属性加以更改
// out.println(request.getParameter("parentclass"));
// out.println(request.getParameter("curentclass"));
}
else{
//如果修改的是二级分类
String parentid=null;//声明变量
int idd = Integer.parseInt(rootid);
// 定义int型变量idd,并将rootid转换为Int类型赋值给idd
int idddd = Integer.parseInt(id);
// 定义int型变量idd,并将id转换为Int类型赋值给idddd
ResultSet rsclass = sqlbean.executeQuery("select * from class where id="+idd+"");
// 调用sqlbean的executeQuery方法找到类别标识为idd值的记录,并将结果赋予rsclass这个记录集
rsclass.first(); //如果返回的记录集不为空
parentid=rsclass.getString("id");
String parentclass=rsclass.getString("class");
// 取出所属的一级分类的信息
rsclass.close();
int iddd = Integer.parseInt(parentid);//将要添加的类的父类的ID转换为int类型
rsclass.close();//关闭记录集对象
String sql="update class set parentclass= '"+parentclass+"',parentid="+iddd+",class='"+curentclass+"' where id="+idddd+"";
sqlbean.executeUpdate(sql);
// 将要添加的分类更新到数据库中
sql="update news set parentclass= '"+parentclass+"',class='"+curentclass+"' where classid="+idddd+"";
sqlbean.executeUpdate(sql);
// 将记录中的类别属性加以更改
// out.println(parentclass);
// out.println(curentclass);
// out.println(idddd);
}}
catch(SQLException ex)
{System.err.println("sql_data.executeUpdate:"+ex.getMessage());
} }
public void add_class(String rootid,String curentclass){
try{
if (rootid.equals("0"))//判断是要添加的是一级分类还是二级分类
{String sql="INSERT INTO class (parentid,parentclass,class) Values (0, '0','"+curentclass+"')";
//添加一级分类的SQL语句
sqlbean.executeInsert(sql);
//调用sqlbean的executeInsert方法将记录添加如数据库中
}
else{//如果添加的是二级分类
String parentid=null,parentclass=null;//声明变量
idd = Integer.parseInt(rootid);
// 定义int型变量idd,并将rootid转换为Int类型赋值给idd
ResultSet rsclass = sqlbean.executeQuery("select * from class where id="+idd+"");
// 调用sqlbean的executeQuery方法找到类别标识为idd值的记录,并将结果赋予rsclass这个记录集
while(rsclass.next()){ //如果返回的记录集不为空
parentid=rsclass.getString("id");
parentclass=rsclass.getString("class");
// 取出所属的一级分类的信息
}
int iddd = Integer.parseInt(parentid);//将要添加的类的父类的ID转换为int类型
rsclass.close();//关闭记录集对象
String sql="INSERT INTO class (parentid,parentclass,class) Values ("+iddd+",'"+parentclass+"','"+curentclass+"')";
sqlbean.executeInsert(sql);
// 将要添加的分类添加如数据库中
}
}catch(SQLException ex)
{System.err.println("sql_data.executeUpdate:"+ex.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -