📄 chatroom.java
字号:
package net.chat;
import java.sql.*;
import java.text.SimpleDataFormat;
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 niceName=? 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.close();
}
}
/**************************************************************
*y用户离开聊天室的时候,将用户从在线人员中删除
***************************************************************/
public void logout(Sting userName) throws SQLException,Class NotFoundExcption
{
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();
}Catach(SQLException ex)
{
ex.printStackTrace();
throw ex;
}finally
{
conn.close();
}
}
/**************************************************************
*换房间,将用户从原来的聊天室在线用户表中删除
***************************************************************/
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.excuteUpate();
}catch(SQLException ex)
{
ex.printStackTrace()
throw ex;
}finally
{
conn.close();
}
}
/*
检查用户是不是管理员,如果是管理员,返回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.close();
}
}
/*
将用户踢出聊天室
*/
public void kicUser(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 SimpleDaeFormat("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(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;
}finaly
{
conn.close();
}
/*
删除用户
*/
public void deleteUser(String userName)throws SQLException,ClassNotFoundException
{ BaseConn conn=null;
try
{
conn=new BaseConn();
String sql="delete from online 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;
}finally
{
conn.close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -