📄 mysession.java
字号:
package msg;
import msg.*;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.sql.*;
import java.text.SimpleDateFormat;
public class MySession{
private String userId=null;
private String sessionId=null;
private String userType=null;
private String start=null;
private String sql="";
private ResultSet rs=null;
private DbConn db=null;
public MySession(String userId){
this.userId=userId;
}
public MySession(String userId,String sessionId,String userType){
if(userId==null || userId.equals(""))
return;
try{
db=new msg.DbConn();
Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String start= formatter1.format(cal.getTime());
sql="update mysession set sessionId=\'"+sessionId+"\',userType=\'"+userType+"\',start=\'"+start+
"\' where userId=\'"+userId+"\'";
if(db.getConnection()==null || db.isClosed())
db.setConnection();
db.setSqlQuery(sql);
if(db.executeUpdate()==0){
sql="insert into mysession values(\'"+userId+"\',\'"+sessionId+"\',\'"+userType+"\',\'"+start+"\',\'\')";
if(db.getConnection()==null || db.isClosed())
db.setConnection();
db.setSqlQuery(sql);
db.executeUpdate();
}
this.userId=userId;
this.sessionId=sessionId;
this.userType=userType;
this.start=start;
if(db.getConnection()!=null && !db.isClosed())
db.closeConnection();
}catch(Exception e){
closeDbConnection();
Log.log("class:MySession method:MySession():"+e.getMessage()+" sql:"+sql);
}
}
public MySession(String userId,String sessionId,String userType,String ip){
if(userId==null || userId.equals(""))
return;
try{
db=new msg.DbConn();
Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String start= formatter1.format(cal.getTime());
sql="update mysession set sessionId=\'"+sessionId+"\',userType=\'"+userType+"\',start=\'"+start+
"\',ip=\'"+ip+"\' where userId=\'"+userId+"\'";
if(db.getConnection()==null || db.isClosed())
db.setConnection();
db.setSqlQuery(sql);
if(db.executeUpdate()==0){
sql="insert into mysession values(\'"+userId+"\',\'"+sessionId+"\',\'"+userType+"\',\'"+start+"\',\'"+ip+"\')";
if(db.getConnection()==null || db.isClosed())
db.setConnection();
db.setSqlQuery(sql);
db.executeUpdate();
}
this.userId=userId;
this.sessionId=sessionId;
this.userType=userType;
this.start=start;
if(db.getConnection()!=null && !db.isClosed())
db.closeConnection();
}catch(Exception e){
closeDbConnection();
Log.log("class:MySession method:MySession():"+e.getMessage()+" sql:"+sql);
}
}
public MySession getMySession(){
if(userId==null || userId.equals(""))
return this;
try{
if(db==null)
db=new msg.DbConn();
sql="select * from mysession where userId=\'"+userId+"\'";
if(db.getConnection()==null || db.isClosed())
db.setConnection();
db.setSqlQuery(sql);
rs=db.getResult();
if(rs!=null && rs.next()){
this.userId=userId;
sessionId=rs.getString("sessionId");
userType=rs.getString("userType");
start=rs.getString("start");
}
if(db.getConnection()!=null && !db.isClosed())
db.closeConnection();
}catch(Exception e){
closeDbConnection();
Log.log("class:MySession method:getMySession():"+e.getMessage()+" sql:"+sql);
}
return this;
}
public MySession getMySession(String ip){
if(userId==null || userId.equals(""))
return this;
try{
if(db==null)
db=new msg.DbConn();
sql="select * from mysession where userId=\'"+userId+"\' and ip=\'"+ip+"\'";
if(db.getConnection()==null || db.isClosed())
db.setConnection();
db.setSqlQuery(sql);
rs=db.getResult();
if(rs!=null && rs.next()){
this.userId=userId;
sessionId=rs.getString("sessionId");
userType=rs.getString("userType");
start=rs.getString("start");
}
if(db.getConnection()!=null && !db.isClosed())
db.closeConnection();
}catch(Exception e){
closeDbConnection();
Log.log("class:MySession method:getMySession():"+e.getMessage()+" sql:"+sql);
}
return this;
}
public void setValues(String sessionId,String userType){
if(userId==null || userId.equals(""))
return;
try{
if(db==null)
db=new msg.DbConn();
Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String start= formatter1.format(cal.getTime());
sql="update mysession set sessionId=\'"+sessionId+"\',userType=\'"+userType+"\',start=\'"+start+
"\' where userId=\'"+userId+"\'";
if(db.getConnection()==null || db.isClosed())
db.setConnection();
db.setSqlQuery(sql);
if(db.executeUpdate()==0){
sql="insert into mysession values(\'"+userId+"\',\'"+sessionId+"\',\'"+userType+"\',\'"+start+"\',\'\')";
if(db.getConnection()==null || db.isClosed())
db.setConnection();
db.setSqlQuery(sql);
db.executeUpdate();
}
this.userId=userId;
this.sessionId=sessionId;
this.userType=userType;
this.start=start;
if(db.getConnection()!=null && !db.isClosed())
db.closeConnection();
}catch(Exception e){
closeDbConnection();
Log.log("class:MySession method:setMySession():"+e.getMessage()+" sql:"+sql);
}
}
public String getSessionId(){
return sessionId;
}
public String getUserType(){
return userType;
}
public String getStart(){
return start;
}
private void closeDbConnection(){
try{
if(db!=null && db.getConnection()!=null && !db.isClosed())
db.closeConnection();
}catch(Exception e){}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -