📄 smgpdbaccess.java~4~
字号:
package com.pansonlu.smgp;
/**
* <p>Title: SMGPDBAccess</p>
* <p>Description: 湖南电信短信网关通讯程序-->数据库访问类</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: sunrise Ltd.</p>
* @author pansonlu
* @version 1.0
*/
import java.util.*;
import java.sql.*;
import com.pansonlu.common.database.*;
import com.pansonlu.common.util.*;
public class SMGPDBAccess {
private static SMGPDBAccess DBAccess;
public boolean isAvail = false;
/** 上行数据缓冲,当SMGPMOReceiveThread接受到上行数据后,将数据保存在该缓冲中,由
* SMGPMODataSaveThread线程将数据再保存到数据库中 **/
public Vector vctMOData = new Vector(1,1);
/** 下行数据缓冲,当SMGPDBAccess从数据库中取出数据后,保存在该缓冲中,由
* 单连接时的SMGPMOSendThread线程或双连接时的SMGPMTSendThread线程将数据发送到ISMG **/
public Vector vctMTData = new Vector(1,1);
/** 网关回应信息缓冲 **/
public Vector vctRespMsg = new Vector(1,1);
public String inertSql = "";
private SMGPMODataSaveThread moSaveThread;
public SMGPDBAccess() {
if(SMGParameter.DBType==1) inertSql = this.strInsertOracle;
else inertSql = this.strInsertSqlServer;
}
/**取得SMGPMOSocketProcess的唯一实例 **/
public static SMGPDBAccess getInstance(){
if(DBAccess == null){
DBAccess = new SMGPDBAccess();
}
if(!DBAccess.isAvail){
DBAccess.connect();
}
else{
}
return DBAccess;
}
/**
* 连接到数据库
* @return true(成功)or false(失败)
*/
public boolean connect(){
Connection conn = null;
ResultSet rs = null;
try {
conn = ConnectionPool.getConnection();
//从连接池取得连接并测试连接是否成功
String strSql = "";
if(SMGParameter.DBType ==1)
strSql = "SELECT SYSDATE FROM DUAL";
else if(SMGParameter.DBType ==2)
strSql = "select getdate()";
else if(SMGParameter.DBType ==3)
strSql = "select getdate()";
rs = conn.createStatement().executeQuery(strSql);
rs.next();
String ss = rs.getString(1);
Debug.outInfo("[SMGPMaster]"+PublicFunction.getFormatTime()+" 通讯程序连接数据库成功!" + ss);
rs.close();
this.isAvail = true;
if(moSaveThread ==null || !moSaveThread.isAvail){
moSaveThread = new SMGPMODataSaveThread();
moSaveThread.start();
Debug.outInfo("[SMGPMaster]"+PublicFunction.getFormatTime()+" 缓冲数据线程启动...");
}
}
catch (Exception ex) {
System.out.println("[SMGPMaster]连接数据库失败DBAccess.connect() 异常:" + ex);
this.isAvail = false;
}
finally{
discloseconn(conn,null,null); //关闭connection(放回连接池)
}
return this.isAvail;
}
String strInsertOracle
= "insert into smgp_from_ismg (msgid, isreport, msgformat,srctermid, desttermid, msgcontent" +
") values (seq_sms.nextval,";
String strInsertSqlServer
= "insert into smgp_from_ismg (isreport, msgformat,srctermid, desttermid, msgcontent)";
/**
* 将smgpMsg保存到数据库中
* @param smgpMsg
* @throws java.lang.Exception
*/
public void saveMODataFromVector(SMGP smgpMsg) throws Exception{
Connection conn = null;
PreparedStatement pstmt = null;
try{
conn = ConnectionPool.getConnection();
//将状态报告,Deliver消息插入到数据库得SQL语句
SMGP submitMsg = smgpMsg;
if(submitMsg.IsReport == 1){
//当为状态报告时
String strSql = "insert into smgp_report (msg_id,submit_time,dest_terminal_id,src_id,text) ";
strSql+= "values(?,?,?,?,?)";
pstmt = conn.prepareStatement(strSql);
pstmt.setLong(1,submitMsg.SequenceID);
pstmt.setString(2,submitMsg.RecvTime);
pstmt.setString(3,submitMsg.DestTermID);
pstmt.setString(4,submitMsg.SrcTermID);
pstmt.setString(5,submitMsg.MsgContent);
pstmt.execute();
}
else{
//当为Deliver消息时
String strSql = "insert into smgp_from_ismg (isreport, msgformat,srctermid, desttermid, msgcontent ) ";
strSql +="values (?,?,?,?,?)";
pstmt = conn.prepareStatement(strSql);
pstmt.setInt(1,(int)submitMsg.IsReport);
pstmt.setInt(2,(int)submitMsg.MsgFormat);
pstmt.setString(3,submitMsg.SrcTermID);
pstmt.setString(4,submitMsg.DestTermID);
pstmt.setString(5,submitMsg.MsgContent);
pstmt.execute();
}
}
catch(Exception ex){
throw ex;
}
finally{
discloseconn(conn,pstmt,null);
}
}
/**
* 将vctMOData中的MO数据保存到数据库中
* @param vctMOData
* @throws java.lang.Exception
*/
public void saveMODataFromVector(Vector vctData) throws Exception{
Connection conn = null;
PreparedStatement pstmt = null;
try{
conn = ConnectionPool.getConnection();
//将状态报告,Deliver消息插入到数据库得SQL语句
while(vctData.size()>0){
SMGP submitMsg = (SMGP)vctData.elementAt(0);
if(submitMsg.IsReport == 1){
//当为状态报告时
String strSql = "insert into smgp_report (msg_id,submit_time,dest_terminal_id,src_id,text) ";
strSql+= "values(?,?,?,?,?)";
pstmt = conn.prepareStatement(strSql);
pstmt.setLong(1,submitMsg.SequenceID);
pstmt.setString(2,submitMsg.RecvTime);
pstmt.setString(3,submitMsg.DestTermID);
pstmt.setString(4,submitMsg.SrcTermID);
pstmt.setString(5,submitMsg.MsgContent);
pstmt.execute();
}
else{
//当为Deliver消息时
String strSql = "insert into smgp_from_ismg (isreport, msgformat,srctermid, desttermid, msgcontent ) ";
strSql +="values (?,?,?,?,?)";
pstmt = conn.prepareStatement(strSql);
pstmt.setInt(1,(int)submitMsg.IsReport);
pstmt.setInt(2,(int)submitMsg.MsgFormat);
pstmt.setString(3,submitMsg.SrcTermID);
pstmt.setString(4,submitMsg.DestTermID);
pstmt.setString(5,submitMsg.MsgContent);
pstmt.execute();
}
vctData.removeElementAt(0);
}
}
catch(Exception ex){
throw ex;
}
finally{
discloseconn(conn,pstmt,null);
}
}
public void saveRespRelate(SMGP smgpMsg) throws Exception{
Connection conn = null;
Statement stmt = null;
try{
conn = ConnectionPool.getConnection();
StringBuffer sbSql = new StringBuffer("");
sbSql.append("insert into smgp_report_relate ( sequence_id, report_msgid,date_id )");
sbSql.append("values(?,?,to_char(sysdate,'yyyy-mm-dd'))");
PreparedStatement pstmt = conn.prepareStatement(sbSql.toString());
pstmt.setLong(1,smgpMsg.SequenceID);
pstmt.setLong(2,smgpMsg.BCDMsgID);
pstmt.execute();
pstmt.close();
}
catch(Exception ex){
throw ex;
}
finally{
discloseconn(conn,stmt,null);
}
}
/**
* 将vctRespData中的SubmitResp数据保存到数据库中
* @param vctRespData
* @throws java.lang.Exception
*/
public void dealProccessMsg(Vector vctData) throws Exception{
Connection conn = null;
Statement stmt = null;
try{
conn = ConnectionPool.getConnection();
stmt = conn.createStatement();
conn.setAutoCommit(false);
//将状态报告,Deliver消息插入到数据库得SQL语句
for(int i=0;i<vctData.size();i++){
SMGP submitRespMsg = (SMGP)vctData.elementAt(i);
if(submitRespMsg.Status == 0){
//发送成功时候
String strSql ="delete from smgp_to_ismg where to_ismg_id = "+submitRespMsg.SequenceID;
stmt.addBatch(strSql);
}
else{
//发送失败时候
String strSql ="update smgp_to_ismg set submit_status = "+submitRespMsg.Status+" where to_ismg_id = "+submitRespMsg.SequenceID;
stmt.addBatch(strSql);
// strSql ="update smgp_to_ismg_bak set submit_status = "+submitRespMsg.Status+" where to_ismg_id = "+submitRespMsg.SequenceID;
// stmt.addBatch(strSql);
}
}
stmt.executeBatch();
conn.commit();
conn.setAutoCommit(true);
vctData.removeAllElements();
}
catch(Exception ex){
throw ex;
}
finally{
discloseconn(conn,stmt,null);
}
}
/**
* 将待发送数据放入缓冲
* @return
*/
/** 每秒发送条数 **/
private int SendNumPerSecond = SMGParameter.MTSpeed;
public boolean fectchMTDataToBuffer(){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -