⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chatroom.java.bak

📁 网上聊天 很好很强大
💻 BAK
字号:
package net.chat;

import java.sql.*;
import java.text.SimpleDateFormat;

public class ChatRoom {
	
	/**
	 * 判断用户是否在聊天室被踢出聊天室
	 * */
	public boolean denyUser(String userName,String chatRoom)
	throws SQLException,ClassNotFoundException
	{
		BaseConn conn =null;
		try
		{
			conn = new BaseConn();
			String sql = "select * from onlineUser where nickName=? and chatRoom=?";
			PreparedStatement ps = conn.preparedStatement(sql);
			ps.setString(1,userName);
			ps.setString(2,chatRoom);
			ResultSet rs = conn.executeQuery();
			if(!rs.next())
			{
				return false;
			}
			else
				return true;
		}catch(SQLException ex)
		{
			ex.printStackTrace();
			throw ex;
		}catch(ClassNotFoundException ex)
		{
			ex.printStackTrace();
			throw ex;
		}
		finally
		{
			conn.closeDB();
		}
	}	
	/**
	 * 用户离开聊天室的时候,将用户从在线人员表中删除
	 * */
	public void logout(String userName)
	throws SQLException,ClassNotFoundException
	{
		BaseConn conn=null;
		try
		{
		 conn=new BaseConn();
		 String sql = "delete from onlineUser where nickName=?";
		 PreparedStatement ps = conn.preparedStatement(sql);
		 ps.setString(1,userName);
		 conn.executeUpdate();
		}catch(SQLException ex)
		{
			ex.printStackTrace();
			throw ex;
		}catch(ClassNotFoundException ex)
		{
			ex.printStackTrace();
			throw ex;
		}finally
		{
			conn.closeDB();
		}
	}
	
	/**
	 * 换房间,将用户从原来的聊天室在线用户表中删除
	 * */
	public void changeRoom(String userName,String chatRoom)
	throws SQLException,ClassNotFoundException
	{
		BaseConn conn=null;
		try
		{
		 conn=new BaseConn();
		 String sql = "delete from onlineUser where nickName=? and chatRoom=?";
		 PreparedStatement ps = conn.preparedStatement(sql);
		 ps.setString(1,userName);
		 ps.setString(2,chatRoom);
		 conn.executeUpdate();
		}catch(SQLException ex)
		{
			ex.printStackTrace();
			throw ex;
		}catch(ClassNotFoundException ex)
		{
			ex.printStackTrace();
			throw ex;
		}finally
		{
			conn.closeDB();
		}
	}
	
	/**
	 * 检查用户是不是管理员,如果是管理员返回true,如果不是返回false;
	 * */
	public boolean checkAdmin(String userName) throws SQLException,ClassNotFoundException
	{
		BaseConn conn = null;
		try
		{
			conn = new BaseConn();
			String sql="select role from userInfo where nickName=?";
			PreparedStatement ps = conn.preparedStatement(sql);
			ps.setString(1,userName);
			ResultSet rs = conn.executeQuery();
			if(rs.next())
			{
				if(rs.getInt("role")==1)
					return true;
				else
					return false;
			}
			else
				return false;
		}catch(SQLException ex)
		{
			ex.printStackTrace();
			throw ex;
		}catch(ClassNotFoundException ex)
		{
			ex.printStackTrace();
			throw ex;
		}finally
		{
			conn.closeDB();
		}
	}
	
	/**
	 * 将用户踢出聊天室
	 * */
	public void kickUser(String userName,String chatRoom)
	throws SQLException ,ClassNotFoundException
	{
		BaseConn conn = null;
		try
		{
		  conn = new BaseConn();
		  String sql="delete from onlineUser where nickName=? and chatRoom = ?";
		  PreparedStatement ps = conn.preparedStatement(sql);
		  ps.setString(1,userName);
		  ps.setString(2,chatRoom);
		  conn.executeUpdate();
		  SimpleDateFormat cal = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		  String time = cal.format(new java.util.Date());
		  //在聊天信息表中添加一条踢人的系统公告
		  sql = "insert into msgInfo(chatRoom,msgFrom,msgTo,chatTime,msgContent) values(?,?,?,?,?)";
			ps = conn.preparedStatement(sql);
			ps.setString(1,chatRoom);
			ps.setString(2,"系统公告");
			ps.setString(3,"所有人");
			ps.setString(4,time);
			ps.setString(5,"<font color=red>"+userName+"</font>被管理员踢出了聊天室 !!!");
			conn.executeUpdate();
		}catch(SQLException ex)
		{
			ex.printStackTrace();
			throw ex;
		}catch(ClassNotFoundException ex)
		{
			ex.printStackTrace();
			throw ex;
		}finally
		{
			conn.closeDB();
		}
	}
	
	/**
	 * 删除用户
	 * */
	public void deleteUser(String userName)
	throws SQLException,ClassNotFoundException
	{
		BaseConn conn = null;
		try
		{
			conn = new BaseConn();
			String sql="delete from onlineUser where nickName=?";
			PreparedStatement ps = conn.preparedStatement(sql);
			ps.setString(1,userName);
			conn.executeUpdate();
			sql="delete from userInfo where nickName=?";
			ps=conn.preparedStatement(sql);
			ps.setString(1,userName);
			conn.executeUpdate();
		}catch(SQLException ex)
		{
			ex.printStackTrace();
			throw ex;
		}catch(ClassNotFoundException ex)
		{
			ex.printStackTrace();
			throw ex;
		}finally
		{
			conn.closeDB();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -