📄 news.java
字号:
package com.room;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import com.util.DBConn;
public class News {
public int id;
public String sender;
public String sendername;
public String receiver;
public String createtime;
public String topic;
public String content;
public int flag=1;
public News() {
}
//新增留言
public Boolean Add() throws Exception{
Connection conn=null;
PreparedStatement stmt =null;
ResultSet rs =null;
String sql ="insert into ts_news (sender,receiver,content,createtime,topic) values (?,?,?,?,?)";
boolean result=false;
try{
java.util.Date date=new java.util.Date();
Timestamp tt=new Timestamp(date.getTime());
conn=DBConn.getConn();
stmt= conn.prepareStatement(sql);
stmt.setString(1,this.sender);
stmt.setString(2,this.receiver);
stmt.setString(3,this.content);
stmt.setTimestamp(4,tt);
stmt.setString(5, this.topic);
stmt.executeUpdate();
result=true;
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
return result;
}
//删除留言
public Boolean Del(int a_id) throws Exception{
Connection conn=null;
PreparedStatement stmt =null;
ResultSet rs =null;
String sql ="delete from ts_news where id=?";
boolean result=false;
try{
conn=DBConn.getConn();
stmt= conn.prepareStatement(sql);
stmt.setInt(1,a_id);
stmt.executeUpdate();
result=true;
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
return result;
}
// 按id查看留言
public Boolean ViewById() throws Exception{
Connection conn=null;
PreparedStatement stmt =null;
ResultSet rs =null;
String sql ="select A.sender,A.createtime,A.topic,A.content,B.username,A.flag " +
"from ts_news A join ts_users B on A.sender=B.userid where A.id=?";
boolean result=false;
try{
conn=DBConn.getConn();
stmt= conn.prepareStatement(sql);
stmt.setInt(1,this.id);
rs = stmt.executeQuery();
if (rs.next()){
sender=rs.getString(1).trim();
createtime=rs.getString(2).substring(0,19);
topic=rs.getString(3);
content=rs.getString(4).trim();
sendername=rs.getString(5).trim();
flag=rs.getInt(6);
if(flag==1){
sql="update ts_news set flag=2 where id=?";
stmt= conn.prepareStatement(sql);
stmt.setInt(1,this.id);
stmt.executeUpdate();
}
result=true;
}
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
return result;
}
//查看全部留言
public ArrayList ViewAll() throws Exception{
Connection conn=null;
PreparedStatement stmt =null;
ResultSet rs =null;
String sql ="select A.sender,A.createtime,A.topic,A.flag,B.username,A.id " +
"from ts_news A join ts_users B on A.sender=B.userid where A.receiver=? " +
"order by A.id desc";
ArrayList<News> result=new ArrayList<News>();
try{
conn=DBConn.getConn();
stmt= conn.prepareStatement(sql);
stmt.setString(1,this.receiver);
rs = stmt.executeQuery();
while (rs.next()){
News aa=new News();
aa.sender=rs.getString(1).trim();
aa.createtime=rs.getString(2).substring(0,19);
aa.topic=rs.getString(3);
aa.flag=rs.getInt(4);
aa.sendername=rs.getString(5).trim();
aa.id=rs.getInt(6);
result.add(aa);
}
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -