📄 syslog.java
字号:
package com.csbook.documentsystem;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
import java.util.*;
public class SysLog{
Context ctx=null;
DataSource ds=null;
SysLog log=null;
ChangeEncoding ce=null;
//构造函数
public SysLog()
{
//从连接池中获取数据库连接
try{
ctx = new InitialContext();
ds = (DataSource) ctx.lookup("documents");
}
catch(NamingException e){
e.printStackTrace();
}
ce=new ChangeEncoding();
}
//添加系统日志
//operator为进行操作的用户,operation为进行的操作,table为操作的表
public void addLog(String operator,String operation,String table)
{
Connection con = null;
PreparedStatement ps = null;
try {
String sqlupdate = "insert into syslog(operator,operation,tablename) values(?,?,?)";
con=ds.getConnection();
ps=con.prepareStatement(sqlupdate);
ps.setString(1,operator);
ps.setString(2,operation);
ps.setString(3,table);
ps.executeUpdate();
}
catch(SQLException e){
e.printStackTrace();
}
finally{
if (ps != null) try {ps.close();}
catch (SQLException ignore) {}
if (con != null) try {con.close();}
catch (SQLException ignore) {}
}
}
//添加与用户的交互信息
//from为消息的建立者,如有系统自动生成,则输入“system”
//to为消息的接收者,message为消息内容
//state为该条消息的状态,1表示消息接受者尚未阅读该消息,0表示已读
public void addMsg(String from,String to,String message,int state)
{
Connection con = null;
PreparedStatement ps = null;
try{
String sqlInsert="insert into messages(fromUser,toUser,message,state) values(?,?,?,?)";
con=ds.getConnection();
ps=con.prepareStatement(sqlInsert);
ps.setString(1,from);
ps.setString(2,to);
ps.setString(3,message);
ps.setInt(4,1);
ps.executeUpdate();
}
catch(SQLException e){
e.printStackTrace();
}
finally{
if (ps != null) try {ps.close();}
catch (SQLException ignore) {}
if (con != null) try {con.close();}
catch (SQLException ignore) {}
}
}
//获取消息内容
//to为消息的接受者,state为消息的状态
public ArrayList getMsg(String to,int state)
{
Connection con = null;
PreparedStatement ps = null;
ResultSet rs=null;
ArrayList message=new ArrayList();
try{
String sqlQuery="select * from messages where toUser=? and state=?";
con=ds.getConnection();
ps=con.prepareStatement(sqlQuery);
ps.setString(1,to);
ps.setInt(2,state);
rs=ps.executeQuery();
String temp;
while(rs.next()){
temp=rs.getString("id")+" "+ce.changeCharset(rs.getString("message"));
message.add(temp);
}
}
catch(SQLException e){
e.printStackTrace();
}
finally{
if (rs != null) try {rs.close();}
catch (SQLException ignore) {}
if (ps != null) try {ps.close();}
catch (SQLException ignore) {}
if (con != null) try {con.close();}
catch (SQLException ignore) {}
}
return message;
}
//设置消息状态为“已读”
//id为消息编号
public void setMsgOver(int id)
{
Connection con = null;
PreparedStatement ps = null;
try{
String sqlUpdate="update messages set state=0 where id=?";
con=ds.getConnection();
ps=con.prepareStatement(sqlUpdate);
ps.setInt(1,id);
ps.executeUpdate();
}
catch(SQLException e){
e.printStackTrace();
}
finally{
if (ps != null) try {ps.close();}
catch (SQLException ignore) {}
if (con != null) try {con.close();}
catch (SQLException ignore) {}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -