📄 dbnews.java
字号:
package electric.dbs;
import java.sql.*;
import javax.servlet.http.*;
import electric.*;
import java.text.SimpleDateFormat;
import electric.electricUtils.*;
public class Dbnews
{
//将类中的SQL定义为final
private static final String LOAD_NEWS_BY_ID="SELECT * FROM[NEWS] WHERE [ID]=?";
//执行插入操作的预执行语句
private static final String INSERT_NEWS=
"INSERT INTO[NEWS] ([Id],[Motif],[Kithepeople],[Content],[Createdate],[Createpeople],[Status]) VALUES"
+"(?,?,?,?,?,?,?)";
//执行更新操作的预执行语句
private static final String UPDATE_NEWS=
"UPDATE [NEWS] SET[Motif]=?,[Kithepeople]=?,[Content]=?,[Createdate]=?,"
+"[Createpeople]=?,[Status]=? WHERE [Id]=?";
//执行删除操作的预编译语句
private static final String DEL_NEWS=
"UPDATE [NEWS] SET [Status]=? WHERE [Id]=?";
//声明类中使用的变量名称
private int Id;
private String Motif;
private String Kithepeople;
private String Content;
private String Createdate;
private int Createpeople;
private int Status;
//使用类中的setXXX()及getXXX()方法对数据信息进行提取时的暂时保存
public int getId()
{
return this.Id;
}
public String getMotif()
{
return this.Motif;
}
public String getKithepeople()
{
return this.Kithepeople;
}
public String getContent()
{
return this.Content;
}
public String getCreatedate()
{
return this.Createdate;
}
public int getCreatepeople()
{
return this.Createpeople;
}
public int getStatus()
{
return this.Status;
}
public void setId(int Id)
{
this.Id=Id;
saveToDb();
}
public void setMotif(String Motif)
{
this.Motif=Motif;
saveToDb();
}
public void setKithepeople(String Kithepeople)
{
this.Kithepeople=Kithepeople;
saveToDb();
}
public void setContent(String Content)
{
this.Content=Content;
saveToDb();
}
public void setCreatedate(String Createdate)
{
this.Createdate=Createdate;
saveToDb();
}
public void setCreatepeople(int Createpeople)
{
this.Createpeople=Createpeople;
saveToDb();
}
public void setStatus(int Status)
{
this.Status=Status;
saveToDb();
}
public Dbnews()
{}
//通过类中的同样的构造方法,不同的参数信息,执行不同的功能操作
//使用HttpServletRequest对象,进行提取前台信息,执行插入操作
public Dbnews(HttpServletRequest request)
{
//使用类DbSequenceManager生成自动编号
this.Id=DbSequenceManager.nextID(FinalConstants.T_NEWS);
//对页面信息进行提取,去除不合法的html代码
this.Motif=ParamUtils.getEscapeHTMLParameter(request,"motif");
this.Kithepeople=ParamUtils.getEscapeHTMLParameter(request,"kithepeople");
this.Content=ParamUtils.getEscapeHTMLParameter(request,"content");
//对时间进行格式化
SimpleDateFormat simpleDate=new SimpleDateFormat("yyyyMMdd");
String shiftDateToDate=simpleDate.format(new java.util.Date());
this.Createdate=shiftDateToDate;
this.Createpeople=this.Id;
this.Status=FinalConstants.STATUS_NORMAL;
insertIntoDb();
}
//传递信息的Id执行特定检索信息的操作
public Dbnews(int Id) throws NewsNotFoundException
{
this.Id=Id;
if(Id<=0)
{
return;
}
loadFromDb();
}
//执行删除操作的方法
protected void delete()
{
setStatus(FinalConstants.STATUS_DELETE);
}
//使用类中的方法insertIntoDb,对信息进行插入操作
private void insertIntoDb()
{
Connection con=null;
PreparedStatement pstmt=null;
try
{
con=DbConnectionManager.getConnection();
pstmt=con.prepareStatement(INSERT_NEWS);
pstmt.setInt(1,this.Id);
pstmt.setString(2,StringUtils.toChinese(this.Motif));;
pstmt.setString(3,StringUtils.toChinese(this.Kithepeople));
pstmt.setString(4,StringUtils.toChinese(this.Content));
pstmt.setString(5,StringUtils.toChinese(this.Createdate));
pstmt.setInt(6,this.Createpeople);
pstmt.setInt(7,this.Status);
pstmt.executeUpdate();
}
catch(SQLException sqle)
{
System.err.println("错误位置:DbNews:insertIntoDb()-"+sqle);
sqle.printStackTrace();
}
//关闭连接,释放资源
finally
{
try
{
pstmt.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
//使用类中的方法loadFromDb,进行特定检索的信息
private void loadFromDb() throws NewsNotFoundException
{
Connection con=null;
PreparedStatement pstmt=null;
try
{
con=DbConnectionManager.getConnection();
pstmt=con.prepareStatement(LOAD_NEWS_BY_ID);
pstmt.setInt(1,Id);
ResultSet rs=pstmt.executeQuery();
if(!rs.next())
{
throw new NewsNotFoundException("从数据表[NEWS]中读取用户数据失败,欲读取的用户ID:["
+Id+"+]!");
}
this.Id=rs.getInt("Id");
this.Motif=rs.getString("Motif");
this.Kithepeople=rs.getString("Kithepeople");
this.Content=rs.getString("Content");
this.Createdate=rs.getString("Createdate");
this.Createpeople=rs.getInt("Createpeople");
this.Status=rs.getInt("Status");
}
catch(SQLException sqle)
{
System.err.println("错误位置:DbNews:insertIntoDb()-"+sqle);
sqle.printStackTrace();
}
finally
{
try
{
pstmt.close();
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
//使用方法saveToDb,将数据信息进行更新操作
private void saveToDb()
{
Connection con=null;
PreparedStatement pstmt=null;
try
{
con=DbConnectionManager.getConnection();
pstmt=con.prepareStatement(UPDATE_NEWS);
pstmt.setString(1,StringUtils.toChinese(this.Motif));
pstmt.setString(2,StringUtils.toChinese(this.Kithepeople));
pstmt.setString(3,StringUtils.toChinese(this.Content));
pstmt.setString(4,StringUtils.toChinese(this.Createdate));
pstmt.setInt(5,this.Createpeople);
pstmt.setInt(6,this.Status);
pstmt.setInt(7,this.Id);
pstmt.executeUpdate();
}
catch(SQLException sqle)
{
System.err.println("错误位置:DbNews.java:saveToDb()-"+sqle);
sqle.printStackTrace();
}
finally
{
try
{
pstmt.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
//modify()方法,用于存储前台提交的修改信息
public void modify(HttpServletRequest request)
{
this.Id=ParamUtils.getIntParameter(request,"Id");
this.Motif=ParamUtils.getEscapeHTMLParameter(request,"Motif");
this.Kithepeople=ParamUtils.getEscapeHTMLParameter(request,"Kithepeople");
this.Content=ParamUtils.getEscapeHTMLParameter(request,"Content");
this.Createpeople=this.Id;
this.Status=FinalConstants.STATUS_NORMAL;
saveToDb();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -