?? chatroom.java
字號(hào):
package net.chat;
import java.sql.*;
import java.text.SimpleDateFormat;
public class ChatRoom {
/**
* 判斷用戶(hù)是否在聊天室被踢出聊天室
* */
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();
}
}
/**
* 用戶(hù)離開(kāi)聊天室的時(shí)候,將用戶(hù)從在線人員表中刪除
* */
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();
}
}
/**
* 換房間,將用戶(hù)從原來(lái)的聊天室在線用戶(hù)表中刪除
* */
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();
}
}
/**
* 檢查用戶(hù)是不是管理員,如果是管理員返回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();
}
}
/**
* 將用戶(hù)踢出聊天室
* */
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());
//在聊天信息表中添加一條踢人的系統(tǒng)公告
sql = "insert into msgInfo(chatRoom,msgFrom,msgTo,chatTime,msgContent) values(?,?,?,?,?)";
ps = conn.preparedStatement(sql);
ps.setString(1,chatRoom);
ps.setString(2,"system notice");
ps.setString(3,"all people");
ps.setString(4,time);
ps.setString(5,"<font color=red>"+userName+"</font>is kicked out of chatroom by admin !!!");
conn.executeUpdate();
}catch(SQLException ex)
{
ex.printStackTrace();
throw ex;
}catch(ClassNotFoundException ex)
{
ex.printStackTrace();
throw ex;
}finally
{
conn.closeDB();
}
}
/**
* 刪除用戶(hù)
* */
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();
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -