数据库
1 用户表有 sa aa bb 三个用户
2 信息表里边有 id(标识列) Note (内容) sendUname(发信人) receiveUname(收信人) postTime(发信时间) readSign(读标记 未读为0 已读为1)
Message 类
package javaDao;
import java.util.Date;
public class Message {
private int id;
private String Note;//短消息内容
private String sendUname;//发送者
private String receiveUname;//接收者
private Date postTime;//发关时间
private int readSign;//未读为0,已读为1
public Message(){
}
public Message(int id,String Note,String sendUname,String receiveUname,String postTime,int readSign){
this.id=id;
this.Note=Note;
this.sendUname=sendUname;
this.receiveUname=receiveUname;
this.postTime=new Date();
this.readSign=readSign;
}
public String getNote() {
return Note;
}
public void setNote(String note) {
Note = note;
}
public Date getPostTime() {
return postTime;
}
public void setPostTime(Date postTime) {
this.postTime = postTime;
}
public int getReadSign() {
return readSign;
}
public void setReadSign(int readSign) {
this.readSign = readSign;
}
public String getReceiveUname() {
return receiveUname;
}
public void setReceiveUname(String receiveUname) {
this.receiveUname = receiveUname;
}
public String getSendUname() {
return sendUname;
}
public void setSendUname(String sendUname) {
this.sendUname = sendUname;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
MessageDao 类package dao;
import java.sql.SQLException;
import java.util.*;
import javaDao.*;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MessageDao extends BaseDao {
private Connection conn=null;
private PreparedStatement pStatement=null;
private ResultSet rs=null;
Message message=null;
int num=0;
// 增加短信息
public int save(Message message){
try{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
message=new Message();
conn=getConnection();
String sql="insert into TBL_MESSAGE(Note,sendUname,receiveUname,postTime,readSign) values(?,?,?,?,0)";
pStatement=conn.prepareStatement(sql);
pStatement.setString(1, message.getNote());
pStatement.setString(2, message.getSendUname());
pStatement.setString(3, message.getReceiveUname());
pStatement.setString(4, sdf.format(new Date()));
num=pStatement.executeUpdate();
}catch(SQLException e){
e.printStackTrace();
}finally{
closeAll(conn, pStatement, rs);
}
return num;
}
// 根据ID删除一条短消息
public int delete(int id){
try{
conn=getConnection();
String sql="delete from TBL_MESSAGE where id="+id;
pStatement=conn.prepareStatement(sql);
num=pStatement.executeUpdate();
}catch(SQLException e){
e.printStackTrace();
}finally{
closeAll(conn, pStatement, null);
}
return num;
}
// 修改一条短消息
public int update(Message message){
try{
conn=getConnection();
String sql="update TBL_MESSAGE set readSign=1 where id="+message.getId();
pStatement=conn.prepareStatement(sql);
num=pStatement.executeUpdate();
}catch(SQLException e){
e.printStackTrace();
}
return num;
}
public Message findMessage(int id){
Message message = null;
try {
conn =getConnection();
String sql = "select * from TBL_MESSAGE where id=?";
pStatement = conn.prepareStatement(sql);
pStatement.setInt(1, id);
rs = pStatement.executeQuery();
while(rs.next()) {
message = new Message();
message.setId(rs.getInt("id"));
message.setNote(rs.getString("Note"));
message.setSendUname(rs.getString("sendUname"));
message.setReceiveUname(rs.getString("receiveUname"));
message.setPostTime(new Date());
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
closeAll(conn, pStatement, rs);
}
return message;
}
// 根据用户名查询用户收到的短消息列表
public List listByReceiveUname(String uname){
List list =new ArrayList();
try{
conn=getConnection();
String sql="select * from TBL_MESSAGE where receiveUname=?";
pStatement=conn.prepareStatement(sql);
pStatement.setString(1, uname);
rs=pStatement.executeQuery();
while(rs.next()){
Message message=new Message();
message.setNote(rs.getString("note"));
message.setSendUname(rs.getString("sendUname"));
message.setReceiveUname(rs.getString("receiveUname"));
message.setPostTime(new Date());
message.setReadSign(rs.getInt("readSign"));
list.add(message);
}
}catch(SQLException e){
e.printStackTrace();
}finally{
closeAll(conn, pStatement, rs);
}
return list;
}
}
MessageImpl类
package impl;
import java.text.SimpleDateFormat;
import java.util.*;
import dao.*;
import java.util.Date;
import javaDao.Message;
public class MessageImpl {
MessageDao messageDao=new MessageDao();
public int delete(int id) {
// TODO Auto-generated method stub
return messageDao.delete(id);
}
public Message findMessage(int id) {
// TODO Auto-generated method stub
Message message=messageDao.findMessage(id);
int sign=message.getReadSign();
message.setReadSign(sign);
if(sign==0){
messageDao.update(message);
}
return message;
}
public List listByReceiveUname(String uname) {
// TODO Auto-generated method stub
return messageDao.listByReceiveUname(uname);
}
public int save(Message message) {
// TODO Auto-generated method stub
message.setPostTime(new Date());
return messageDao.save(message);
}
public int update(Message message) {
// TODO Auto-generated method stub
return messageDao.update(message);
}
}
listMsg.jsp页面代码
<%@ page language="java" import="java.util.*,impl.*,dao.*,java.sql.*,javaDao.*" pageEncoding="GBK"%>
<% request.setCharacterEncoding("GBK");
User user =(User)session.getAttribute("TBL_USER");
if(user==null){
response.sendRedirect("index.jsp");
return ;
}
MessageImpl mdl=new MessageImpl();
List list=mdl.listByReceiveUname(user.getName());
%>
<html>
<head>
<title>浏览短消息</title>
</head>
<body>
<FORM name="myForm" action="" method="post">
<TABLE width="100%">
<TR align="center">
<TD>
当前用户<%=user.getName() %>
<a href="index.jsp">[登录]</a> <a href="post.jsp">[发短消息]</a>
</TD>
</TR>
<TR>
<TD align="left">
<h2>我的短消息</h2>
</TD>
</TR>
</TABLE>
</FORM>
<Div align="left" >
<%
for(int i=0;i<list.size();i++){
Message message=(Message)list.get(i);
String Note=message.getNote().length()>7 ? message.getNote().substring(0,7)+"...":message.getNote();
int readSign=message.getReadSign();
%>
<div>
<img src="image/<%=readSign==0? "new.jpg":"old.jpg" %>"></img>
<a href="showMsg.jsp?messageId=<%=message.getId()%>">
[<%=message.getSendUname()%>]: <%=Note%></a>
</div>
<%}%>
</Div>
</body>
</html>
showMsg.jsp显示内容页面代码
<%@ page language="java" import="java.util.*,impl.*,dao.*,java.sql.*,javaDao.*" pageEncoding="GBK"%>
<% request.setCharacterEncoding("GBK");
User user =(User)session.getAttribute("TBL_USER");
if(user==null){
response.sendRedirect("index.jsp");
return ;
}
MessageImpl mdl=new MessageImpl();
List list=mdl.listByReceiveUname(user.getName());
%>
<html>
<head>
<title>浏览短消息</title>
</head>
<body>
<FORM name="myForm" action="" method="post">
<TABLE width="100%">
<TR align="center">
<TD>
当前用户<%=user.getName() %>
<a href="index.jsp">[登录]</a> <a href="post.jsp">[发短消息]</a>
</TD>
</TR>
<TR>
<TD align="left">
<h2>我的短消息</h2>
</TD>
</TR>
</TABLE>
</FORM>
<Div align="left" >
<%
for(int i=0;i<list.size();i++){
Message message=(Message)list.get(i);
String Note=message.getNote().length()>7 ? message.getNote().substring(0,7)+"...":message.getNote();
int readSign=message.getReadSign();
%>
<div>
<img src="image/<%=readSign==0? "new.jpg":"old.jpg" %>"></img>
<a href="showMsg.jsp?messageId=<%=message.getId()%>">
[<%=message.getSendUname()%>]: <%=Note%></a>
</div>
<%}%>
</Div>
</body>
</html>
当我点击查看信息的时候 去显示不了内容
错误提示是: