ajax实现的聊天室程序

基于ajax的聊天室程序。程序很简单,但是涵盖的内容还是很多的。把做的代码直接帖出来和大家一起分享了。。
数据库我用的是MYSQL
drop   table   if   exists  chat;
create   table  chat(
    chat_id 
int ( 16 not   null  auto_increment,
    chat_name 
varchar ( 64 default   null ,
    start_time Date 
default   null ,
    
primary   key (chat_id)
)
drop   table   if   exists   user ;
create   table   user (
    
user_id   int ( 16 not   null  auto_increment,
    
user_name   varchar ( 64 default   null ,
    start_time Date 
default   null ,
    
primary   key ( user_id )
)
drop   table   if   exists  message;
create   table  message(
    message_id 
int ( 16 not   null  auto_increment,
    chat_id 
int ( 16 not   null   default   ' 0 ' ,
    
user_id   int ( 16 not   null   default   ' 0 ' ,
    
user_name   varchar ( 64 default   null ,
    message 
varchar ( 1024 ),
    post_time Date 
default   null ,
    
primary   key (message_id)
)

insert   into  chat  values ( 1 , ' Beijing ' , '' );
insert   into  chat  values ( 2 , ' Shanghai ' , '' );
insert   into  chat  values ( 3 , ' Guangzhou ' , '' );
package  book.chat.table;

import  java.sql.ResultSet;
import  java.sql.SQLException;
import  java.util.Date;

import  book.chat.util.DbManager;

public   class  Chat  {
    
private int chatID;
    
private String chatName;
    
private Date startTime;
    
    
public Chat(){}
    
public Chat(int id){
        
this.chatID =id ;
        
if(!FromDb())
            
this.chatID=-1;
    }

    
private boolean FromDb() {
        
int row = -1;
        String sql 
="select * from chat where chat_id="+this.chatID;
        ResultSet rs 
= DbManager.getResultSet(sql);
        
try{
            rs.last();
            row 
= rs.getRow();
            
if(row == 1){
                
this.chatID = rs.getInt("CHAT_ID");
                
this.chatName = rs.getString("CHAT_NAME");
                
this.startTime = rs.getDate("START_TIME");
                
return true;
            }
else
                
return false;
        }
catch(SQLException e){
            e.printStackTrace();
            
return false;
        }
finally{
            DbManager.releaseConnection();
        }

    }

    
public int getChatID() {
        
return chatID;
    }

    
public void setChatID(int chatID) {
        
this.chatID = chatID;
    }

    
public String getChatName() {
        
return chatName;
    }

    
public void setChatName(String chatName) {
        
this.chatName = chatName;
    }

    
public Date getStartTime() {
        
return startTime;
    }

    
public void setStartTime(Date startTime) {
        
this.startTime = startTime;
    }



}

package  book.chat.table;

import  java.sql.ResultSet;
import  java.sql.SQLException;
import  java.util.Date;

import  book.chat.util.DbManager;

public   class  Message  {
    
private int messageID;
    
private int chatID;
    
private int userID;
    
private String userName;
    
private String message;
    
private Date postTime;
    
    
public Message(){}
    
    
public Message(int id){
        
this.messageID =id ;
        
if(!FromDb())
            
this.messageID=-1;
    }

    
private boolean FromDb() {
        
// TODO 自动生成方法存根
        int row = -1;
        String sql 
="select * from message where message_id="+this.messageID;
        ResultSet rs 
= DbManager.getResultSet(sql);
        
try{
            rs.last();
            row 
= rs.getRow();
            
if(row == 1){
                
this.messageID=rs.getInt("MESSAGE_ID");
                
this.chatID = rs.getInt("CHAT_ID");
                
this.userID = rs.getInt("USER_ID");
                
this.userName = rs.getString("USER_NAME");
                
this.message = rs.getString("MESSAGE");
                
this.postTime = rs.getDate("POST_TIME");
                
return true;
            }
else
                
return false;
        }
catch(SQLException e){
            e.printStackTrace();
            
return false;
        }
finally{
            DbManager.releaseConnection();
        }

    }

    
    
public int getChatID() {
        
return chatID;
    }

    
public void setChatID(int chatID) {
        
this.chatID = chatID;
    }

    
public String getMessage() {
        
return message;
    }

    
public void setMessage(String message) {
        
this.message = message;
    }

    
public int getMessageID() {
        
return messageID;
    }

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值