?? chatroom.java
字號(hào):
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用戶離開(kāi)聊天室的時(shí)候,將用戶從在線人員中刪除
***************************************************************/
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();
}
}
/**************************************************************
*換房間,將用戶從原來(lái)的聊天室在線用戶表中刪除
***************************************************************/
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());
//在聊天信息表中添加一條踢人的系統(tǒng)公告
sql="insert into msgInfo(chatRoom,msgFrom,msgTo,chatTime,msgContent)values(?,?,?,?,?)";
ps=conn.preparedStatement(sql);
ps.setString(1,chatRoom);
ps.setString(2,"系統(tǒng)公告");
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();
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -