jsp java打日志,JBuilder2005实战JSP之日志和部署

1. package bookstore;

2. import javax.Servlet.http.HttpSessionBindingListener;

3. import javax.servlet.http.HttpSessionBindingEvent;

4. import java.sql.*;

5. import java.text.SimpleDate表单at;

6. import java.util.Date;

7.

8. public class User implements HttpSessionBindingListener

9. {

10.  …

11.  private String loginDatetime;//用户登录时间

12.  …

13.  public void valueBound(HttpSessionBindingEvent event)

14.  {

15.   Connection conn = null;

16.   String sqlStr = "insert into T_LOGIN_LOG(ID, USER_ID, DT_LOGIN) " +

17.       " values(SEQ_LOGIN_LOG_ID.NEXTVAL,?,? )";

18.   try

19.   {

20.    conn = DBConnection.getConnection();

21.    PreparedStatement pStat = conn.prepareStatement(sqlStr);

22.    loginDatetime = getCurrDatetimeStr(); //当前时间串

23.    pStat.setString(1, userId);

24.    pStat.setString(2, loginDatetime);

25.    pStat.executeUpdate();

26.

27.   } catch (SQLException e)

28.   {

29.    throw new RuntimeException(

30.     "用户登陆日志写入出错");

31.   } finally

32.  {

33.  try

34.  {

35.   if (conn != null)

36.   {

37.    conn.close();

38.   }

39.  } catch (SQLException ex)

40.  {

41.   ex.printStackTrace();

42.  }

43.  }

44. }

45.

46. public void valueUnbound(HttpSessionBindingEvent event)

47. {

48.  Connection conn = null;

49.  String sqlStr = " update T_LOGIN_LOG set DT_LONOUT = ? " +

50.      " where USER_ID=? and DT_LOGIN = ?";

51.  try

52.  {

53.   conn = DBConnection.getConnection();

54.   PreparedStatement pStat = conn.prepareStatement(sqlStr);

55.   pStat.setString(1, getCurrDatetimeStr());

56.   pStat.setString(2, userId);

57.   pStat.setString(3, loginDatetime);

58.   pStat.executeUpdate();

59.

60.  } catch (SQLException e)

61.  {

62.   throw new RuntimeException(

63.    "用户退出日志写入出错");

64.  } finally

65.  {

66.   try

67.   {

68.    if (conn != null)

69.    {

70.     conn.close();

71.    }

72.   } catch (SQLException ex)

73.   {

74.    ex.printStackTrace();

75.   }

76.  }

77. }

78.

79. //获取当前时间字串,以yyyyMMddHHmmss格式返回,如20050505010101

80. private static String getCurrDatetimeStr()

81. {

82.  SimpleDate表单at sdf = new SimpleDate表单at("yyyyMMddHHmmss");

83.  return sdf.表单at(new Date());

84. }

85. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值