📄 news.java
字号:
package ksnb;
import java.sql.*;
import javax.sql.*;
import javax.servlet.http.*;
import java.text.*;
import java.util.*;
public class News {
private conn c=null;
private Connection con = null;
private Statement stm = null;
private Statement stm2=null;
private Statement stm3=null;
private ResultSet rs = null;
private String strSql = null;
private String strSql2=null;
private java.util.Date st2 = null;
private Collection ret=null;
private String date=null;
public News() {
//连接数据库
try {
c = new conn();
con = c.getConncetion();
} catch (connException e) {
System.out.println(e.toString());
}
}
//解决乱码
private static String convert( String str )
{
try
{
byte[] bytesStr=str.getBytes( "ISO-8859-1" ) ;
return new String( bytesStr, "gb2312" ) ;
}
catch( Exception ex)
{
return str ;
}
}
//修改中的查询某条记录的详细信息
public void select(HttpServletRequest request)
{
int id=Integer.parseInt(request.getParameter("cmd1"));
strSql="select * from Shop_News_Info where Id="+id;
try
{
stm = con.createStatement();
rs = stm.executeQuery(strSql);
while (rs.next()) {
NewsBean newbean = new NewsBean();
newbean.setId(rs.getInt("Id"));
newbean.setTypeid(rs.getInt("Type_Id"));
newbean.setTopic(rs.getString("Topic"));
newbean.setContent(rs.getString("Content"));
newbean.setKeywords(rs.getString("Keywords"));
newbean.setClick(rs.getInt("Click"));
newbean.setSource(rs.getString("Source"));
newbean.setDate(rs.getString("Date_time"));
ret.add("newbean");
}
request.setAttribute("s",ret);
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
//查询
public void SelectNews(HttpServletRequest request)
{
ret=new ArrayList();
int Type_Id=Integer.parseInt(request.getParameter("Type_Id"));
String Topic=convert((String)request.getParameter("Topic").trim());
String Content=convert((String)request.getParameter("Content").trim());
String Date_time=convert((String)request.getParameter("Date_time").trim());
strSql="select * from Shop_News_Info where Type_Id="+Type_Id+" and Topic like '%"+Topic+"%' and Content like '%"+Content+"%'";
System.out.println(strSql);
strSql = strSql + "order by Date_time desc";
//执行SQL语句
try {
int i=1;
stm = con.createStatement();
rs = stm.executeQuery(strSql);
while (rs.next()) {
NewsBean newbean=new NewsBean();
newbean.setId(rs.getInt("Id"));
newbean.setTypeid(rs.getInt("Type_Id"));
newbean.setTopic(rs.getString("Topic"));
newbean.setContent(rs.getString("Content"));
newbean.setKeywords(rs.getString("Keywords"));
newbean.setClick(rs.getInt("Click"));
newbean.setSource(rs.getString("Source"));
newbean.setDate(rs.getString("Date_time"));
ret.add(newbean);
System.out.println(i++);
}
request.setAttribute("ss",ret);
System.out.println("select over");
} catch (Exception e) {
System.out.println(e.toString());
}
}
public void insert(HttpServletRequest request)
{
//从页面中提取出值
int Type_Id=Integer.parseInt(request.getParameter("Type_Id"));
String Topic=convert(request.getParameter("Topic").trim());
String Content=convert(request.getParameter("Content").trim());
String Keywords=convert(request.getParameter("Keywords").trim());
String Source=convert(request.getParameter("Source").trim());
String Date_time=convert(request.getParameter("Date_time").trim());
String Img_Name=convert(request.getParameter("Img_Name").trim());
String Img_Address=convert(request.getParameter("Img_Address").trim());
int i=0;
//插入语句
strSql="insert into Shop_News_Info(Type_Id,Topic,Content,Keywords,Source,Date_time) values(?,?,?,?,?,?)";
try {
if (Date_time.equals("") != true) {
//写一个DATETIME对象
} else {
//写一个DATETIME对象
DateFormat dateFormatter = DateFormat.getDateTimeInstance(
DateFormat.LONG, DateFormat.LONG);
// 取得系统时间
java.util.Date utilDate = new java.util.Date();
// 转换成数据库时间
date = (new java.sql.Date(utilDate.getTime())).toString();
}
PreparedStatement stm = con.prepareStatement(strSql);
stm.setInt(1,Type_Id);
stm.setString(2,Topic);
stm.setString(3,Content);
stm.setString(4,Keywords);
stm.setString(5,Source);
stm.setString(6,date);
stm.executeUpdate();
stm.close();
//从Shop_News_Info表中查找出插入的记录的ID
strSql="select Id from Shop_News_Info where Content='"+Content+"'";
stm3=con.createStatement();
rs=stm3.executeQuery(strSql);
while(rs.next())
{
i=rs.getInt("Id");
}
rs.close();
stm3.close();
//将数据插入Shop_News_Imgs
strSql="insert into Shop_News_Imgs(Info_id,Img_Name,Img_Address) values(?,?,?)";
PreparedStatement stm2 = con.prepareStatement(strSql);
stm2.setInt(1,i);
stm2.setString(2,Img_Name);
stm2.setString(3,Img_Address);
stm2.executeUpdate();
} catch (Exception e) {
System.out.println(e.toString());
}
}
public void delete(HttpServletRequest request)
{
int cmd=Integer.parseInt(request.getParameter("cmd1"));
strSql="delete from Shop_News_Info where Id="+cmd;
strSql2="delete from Shop_News_Imgs where Info_id="+cmd;
System.out.println("sql"+strSql);
try
{
stm = con.createStatement();
stm.executeUpdate(strSql2);//删除img表中的与info表中相关联的记录
stm.executeUpdate(strSql);//删除info表中的记录
stm.close();
}
catch(Exception e){System.out.println(e.toString());}
}
/*public void update(HttpServletRequest request)
{
}*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -