package net.chat;
import java.sql.*;
import java.text.SimpleDateFormat;
public class ChatRoom {
/*
* 判断用户是否在聊天室踢出
*/
public boolean denyUser(String userName,String chatRoom) throws Exception{
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 e) {
e.printStackTrace();
throw e ;
}catch (ClassNotFoundException e) {
e.printStackTrace();
throw e ;
}
finally{
conn.closeDB() ;
}
}
/*
* 用户离开聊天室的时候,将用户从在线人员表中删除
*/
public void logout(String userName) throws Exception{
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 e) {
e.printStackTrace();
throw e ;
}catch (ClassNotFoundException e) {
e.printStackTrace();
throw e ;
}
finally{
conn.closeDB() ;
}
}
/*
* 换房间,将用户从原来的聊天室在线用户表中删除
*/
public void changeRoom(String userName,String chatRoom) throws Exception{
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 e) {
e.printStackTrace();
throw e ;
}catch (ClassNotFoundException e) {
e.printStackTrace();
throw e ;
}
finally{
conn.closeDB() ;
}
}
/*
* 检查用户是不是管理员,如果是管理员,返回true,否则返回false
*/
public boolean checkAdmin(String userName) throws Exception{
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 e) {
e.printStackTrace();
throw e ;
}catch (ClassNotFoundException e) {
e.printStackTrace();
throw e ;
}
finally{
conn.closeDB() ;
}
}
/*
* 将用户踢出聊天室
*/
public void kickUser(String userName,String chatRoom) throws Exception{
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()) ;
//在msgInfo表中添加一条公告,用户被管理员踢出聊天室
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 e) {
e.printStackTrace();
throw e ;
}catch (ClassNotFoundException e) {
e.printStackTrace();
throw e ;
}
finally{
conn.closeDB() ;
}
}
/*
* 删除用户
*/
public void deleteUser(String userName) throws Exception{
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 where nickName=?";
ps = conn.preparedStatement(sql) ;
ps.setString(1, userName) ;
conn.executeUpdate();
}catch (SQLException e) {
e.printStackTrace();
throw e ;
}catch (ClassNotFoundException e) {
e.printStackTrace();
throw e ;
}
finally{
conn.closeDB() ;
}
}
}
- 1
- 2
- 3
前往页