📄 manager.java
字号:
package chatweb;
import java.sql.*;
import java.io.*;
public class manager
{
String sDBdrive="sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr="jdbc:odbc:chatdb";
String user="sasa";
String passwd="3825";
Connection connect=null;
ResultSet rs=null;
public manager()
{
try
{
Class.forName(sDBdrive).newInstance();
}
catch(Exception e)
{
System.err.println("forname:"+e.getMessage());
}
}
public ResultSet executeQuery(String sql)
{
rs=null;
try
{
connect=DriverManager.getConnection(sConnStr,user,passwd);
Statement stmt=connect.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println("SQLERROR:"+ex.getMessage());
}
return rs;
}
public boolean executeOrder(String sql)
{
try
{
connect=DriverManager.getConnection(sConnStr,user,passwd);
Statement stmt=connect.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println("executeOr:"+ex.getMessage());
return false;//执行异常时返回false
}
return true;
}
public long getBlocked(String username)
{
ResultSet rs1=null;
String strSQL="select *from block where username='"+username+"'";
rs1=executeQuery(strSQL);
try
{
if(rs1.next())
{
String strSQL2="select perio_diff(freetime,now()) as res from block where username='"+username+"';";;
ResultSet rs2=executeQuery(strSQL2);
if(rs2.next())
{
long lefttime=rs2.getLong("res");
if(lefttime<=0)
{
free(username);
lefttime=0;
}
return lefttime;
}
}
}
catch(Exception e)
{
System.err.println(e.toString());
return -1;
}
return 0;//0与负数表示未被禁言
}
public boolean block(String username,int blocktime)
{
String strSQL="insertinto block values('"+username+"',date_add(now(),interval "+blocktime+" minute));";
if(getBlocked(username)>0)
{
strSQL=new String("update block set freetime=date_add(now(),interval "+blocktime+" minute) where username='"+username+"';");
}
return executeOrder(strSQL);
}
public boolean free(String username)
{
String strSQL="delete from block where username='"+username+"'";
return executeOrder(strSQL);
}
//删除用户
public boolean deleteUser(String username)
{
if(getBlocked(username)>0)
{
free(username);
}
String strSQL="delete from user where username='"+username+"' and not grade=1";
return executeOrder(strSQL);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -