第12次
package com.nn.Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PreDestroy;
import com.nn.entity.msg;
import com.sun.org.apache.regexp.internal.recompile;
public class msgDao extends BaseDao{
public List<msg> listAll(String sendto) throws SQLException{
List<msg> list =new ArrayList<msg>();
Connection conn =getConnection();
String sql="select * from msg where sendto=?";
PreparedStatement pred =null;
ResultSet rest=null;
try {
pred=conn.prepareStatement(sql);
pred.setString(1, sendto);
rest=pred.executeQuery();
while (rest.next()) {
msg msg=new msg();
msg.setMsgid(rest.getInt("msgid"));
msg.setUsername(rest.getString("username"));
msg.setTitle(rest.getString("title"));
msg.setMsgcontent(rest.getString("msgcontent"));
msg.setState(rest.getInt("state"));
msg.setSendto(rest.getString("sendto"));
msg.setMsg_create_date(rest.getDate("msg_create_date"));
list.add(msg);
}
} catch (Exception e) {
// TODO: handle exception
}
finally{
closeAll(conn, pred, rest);
}
return list;
}
public void changestate(Integer msgid){
Connection conn =getConnection();
String sql="update msg set state=1 where msgid=?";
PreparedStatement pred=null;
try {
pred =conn.prepareStatement(sql);
pred.setInt(1, msgid);
pred.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
closeAll(conn, pred, null);
}
}
public List<msg> showContent(Integer msgid) {
List<msg> list=new ArrayList<msg>();
Connection conn=getConnection();
String sql="select * from msg where msgid=?";
PreparedStatement pred=null;
ResultSet rest=null;
try {
pred = conn.prepareStatement(sql);
pred.setInt(1, msgid);
rest =pred.executeQuery();
while(rest.next()){
msg msg =new msg();
msg.setUsername(rest.getString("username"));
msg.setTitle(rest.getString("title"));
msg.setMsgcontent(rest.getString("msgcontent"));
msg.setMsg_create_date(rest.getDate("msg_create_date"));
list.add(msg);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
closeAll(conn, pred, rest);
}
return list;
}
public void delmsg(Integer msgid){
Connection conn =getConnection();
String sql="delete from msg where msgid=?";
PreparedStatement pred=null;
try {
pred =conn.prepareStatement(sql);
pred.setInt(1, msgid);
pred.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
closeAll(conn, pred, null);
}
}
public void send(msg msg){
Connection conn=getConnection();
String sql="insert into msg(username,title,msgcontent,state,sendto,msg_create_date) values(?,?,?,0,?,?)";
PreparedStatement ps=null;
try {
ps=conn.prepareStatement(sql);
ps.setString(1, msg.getUsername());
ps.setString(2, msg.getTitle());
ps.setString(3, msg.getMsgcontent());
ps.setString(4, msg.getSendto());
ps.setDate(5, new java.sql.Date(new Date().getTime()));
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeAll(conn, ps, null);
}
}
}

浙公网安备 33010602011771号