?? user.java
字號(hào):
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(); //當(dāng)前時(shí)間串
pStat.setString(1, userId);
pStat.setString(2, loginDatetime);
pStat.executeUpdate();
} catch (SQLException e)
{
e.printStackTrace();
throw new RuntimeException("用戶登陸日志寫入出錯(cuò)");
} 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(
"用戶退出日志寫入出錯(cuò)");
} finally
{
try
{
if (conn != null)
{
conn.close();
}
} catch (SQLException ex)
{
ex.printStackTrace();
}
}
}
/**
* 獲取當(dāng)前時(shí)間字串,以yyyyMMddHHmmss格式返回,如20050505010101
* @return String
*/
private static String getCurrDatetimeStr()
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
return sdf.format(new Date());
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -