📄 user.java
字号:
package bookstore;
import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionBindingEvent;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class User
implements HttpSessionBindingListener
{
private String userId;
private String password;
private String userName;
private String loginDatetime;
public String getPassword()
{
return password;
}
public String getUserId()
{
return userId;
}
public String getUserName()
{
return userName;
}
public void setPassword(String password)
{
this.password = password;
}
public void setUserId(String userId)
{
this.userId = userId;
}
public void setUserName(String userName)
{
this.userName = userName;
}
public void valueBound(HttpSessionBindingEvent event)
{
Connection conn = null;
String sqlStr = "insert into T_LOGIN_LOG(ID, USER_ID, DT_LOGIN) " +
" values(SEQ_LOGIN_LOG_ID.NEXTVAL,?,? )";
try
{
conn = DBConnection.getConnection();
PreparedStatement pStat = conn.prepareStatement(sqlStr);
loginDatetime = getCurrDatetimeStr(); //当前时间串
pStat.setString(1, userId);
pStat.setString(2, loginDatetime);
pStat.executeUpdate();
} catch (SQLException e)
{
e.printStackTrace();
throw new RuntimeException("用户登陆日志写入出错");
} finally
{
try
{
if (conn != null)
{
conn.close();
}
} catch (SQLException ex)
{
ex.printStackTrace();
}
}
}
public void valueUnbound(HttpSessionBindingEvent event)
{
Connection conn = null;
String sqlStr = " update T_LOGIN_LOG set DT_LONOUT = ? " +
" where USER_ID=? and DT_LOGIN = ?";
try
{
conn = DBConnection.getConnection();
PreparedStatement pStat = conn.prepareStatement(sqlStr);
pStat.setString(1, getCurrDatetimeStr());
pStat.setString(2, userId);
pStat.setString(3, loginDatetime);
pStat.executeUpdate();
} catch (SQLException e)
{
throw new RuntimeException(
"用户退出日志写入出错");
} finally
{
try
{
if (conn != null)
{
conn.close();
}
} catch (SQLException ex)
{
ex.printStackTrace();
}
}
}
/**
* 获取当前时间字串,以yyyyMMddHHmmss格式返回,如20050505010101
* @return String
*/
private static String getCurrDatetimeStr()
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
return sdf.format(new Date());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -