package com.lbc.msg.dao.impl;
import com.lbc.msg.dao.BaseDao;
import com.lbc.msg.dao.MessageDao;
import com.lbc.msg.entity.Message;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class MessageDaoImpl extends BaseDao implements MessageDao {
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
public int addMessage(Message message) {
// TODO Auto-generated method stub
String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
String sql="insert into [message] values(?,?,?,?,?)";
String [] getValue={message.getNote(),message.getSendUname(),message.getReceiveUname(),time,0+""};
return this.executeSQL(sql, getValue);
}
public List findMessage(Message message) {
// TODO Auto-generated method stub
String sql="select * from [message] where receiveUname=? order by postTime desc";
List list=new ArrayList();
try {
conn=this.getConn();
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, message.getReceiveUname());
rs=pstmt.executeQuery();
while (rs.next()) {
message=new Message();
message.setId(rs.getInt("id"));
message.setNote(rs.getString("note"));
message.setSendUname(rs.getString("sendUname"));
message.setReadSign(rs.getInt("readSign"));
list.add(message);
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("findMessage方法出异常");
e.printStackTrace();
}finally{
this.closeAll(conn, pstmt, rs);
}
return list;
}
public Message findMessage(int id) {
// TODO Auto-generated method stub
String sql="select * from [message] where id=?";
Message message=null;
try {
conn=this.getConn();
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, id);
rs=pstmt.executeQuery();
while (rs.next()) {
message=new Message();
message.setId(rs.getInt("id"));
message.setNote(rs.getString("note"));
message.setPostTime(rs.getString("postTime"));
message.setReadSign(rs.getInt("readSign"));
message.setReceiveUname(rs.getString("receiveUname"));
message.setSendUname(rs.getString("sendUname"));
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("通过ID的findMessage方法出异常");
e.printStackTrace();
}finally{
this.closeAll(conn, pstmt, rs);
}
return message;
}
public int deleteMessage(int id) {
// TODO Auto-generated method stub
String sql="delete from [message] where id=?";
String [] getValue={id+""};
return this.executeSQL(sql, getValue);
}
public int updateMessage(int id) {
// TODO Auto-generated method stub
String sql="update [message] set readSign=1 where id=?";
String[] getValue={id+""};
return this.executeSQL(sql, getValue);
}
}