📄 cmppdbaccess.java
字号:
package com.zhanghao.cmpp;
/**
* <p>Title:CMPPDBAccess</p>
* <p>Description: 湖南移动短信网关通讯程序</p>
* <p>数据库访问类</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: sunun tech ltd.</p>
* @author zhanghao
* @version 1.0
*/
import java.util.*;
import java.sql.*;
import com.zhanghao.common.database.*;
import com.zhanghao.common.util.*;
public class CMPPDBAccess {
private static CMPPDBAccess DBAccess;
public boolean isAvail = false;
/** 上行数据缓冲,当CMPPMOReceiveThread接受到上行数据后,将数据保存在该缓冲中,由
* CMPPMODataSaveThread线程将数据再保存到数据库中 **/
public Vector vctMOData = new Vector(1,1);
/** 下行数据缓冲,当CMPPDBAccess从数据库中取出数据后,保存在该缓冲中,由
* 单连接时的CMPPMOSendThread线程或双连接时的CMPPMTSendThread线程将数据发送到ISMG **/
public Vector vctMTData = new Vector(1,1);
/** 网关回应信息缓冲 **/
public Vector vctRespMsg = new Vector(1,1);
public String inertSql = "";
private CMPPMODataSaveThread moSaveThread;
public CMPPDBAccess() {
if(CMPParameter.DBType==1) inertSql = this.strInsertOracle;
else inertSql = this.strInsertSqlServer;
}
/**取得CMPPMOSocketProcess的唯一实例 **/
public static CMPPDBAccess getInstance(){
if(DBAccess == null){
DBAccess = new CMPPDBAccess();
}
if(!DBAccess.isAvail){
DBAccess.connect();
}
else{
}
return DBAccess;
}
/**
* 连接到数据库
* @return true(成功)or false(失败)
*/
public boolean connect(){
Connection conn = null;
ResultSet rs = null;
try {
// Class.forName(DBDriver);
// conn = DriverManager.getConnection(Connect_URL, DBUser, DBPass);
conn = ConnectionPool.getConnection();
//从连接池取得连接并测试连接是否成功
String strSql = "";
if(CMPParameter.DBType ==1)
strSql = "SELECT SYSDATE FROM DUAL";
else if(CMPParameter.DBType ==2)
strSql = "select getdate()";
else if(CMPParameter.DBType ==3)
strSql = "select getdate()";
rs = conn.createStatement().executeQuery(strSql);
rs.next();
rs.getString(1);
Debug.outInfo("[CMPPMaster]"+PublicFunction.getFormatTime()+" 通讯程序连接数据库成功!");
rs.close();
this.isAvail = true;
moSaveThread = new CMPPMODataSaveThread();
moSaveThread.start();
Debug.outInfo("[CMPPMaster]"+PublicFunction.getFormatTime()+" 缓冲数据线程启动...");
}
// catch (ClassNotFoundException cnfe) {
// disconnect();
// System.out.println("Not Found Database Connect Driver " + cnfe);
// }
catch (Exception ex) {
System.out.println("[CMPPMaster]连接数据库失败DBAccess.connect() 异常:" + ex);
this.isAvail = false;
}
finally{
discloseconn(conn,null,null); //关闭connection(放回连接池)
}
return this.isAvail;
}
String strInsertOracle
= "insert into cmpp_from_ismg ( from_ismg_id, deliver_msg_id, deliver_destnation_id," +
"deliver_service_id, deliver_msg_fmt, deliver_src_terminal_id, deliver_registered_delivery,"+
"deliver_msg_length, deliver_msg_content values(seq_sms.nextval,";
String strInsertSqlServer
= "insert into cmpp_from_ismg (deliver_msg_id, deliver_destnation_id," +
"deliver_service_id, deliver_msg_fmt, deliver_src_terminal_id, deliver_registered_delivery,"+
"deliver_msg_length, deliver_msg_content values(";
/**
* 将smgpMsg保存到数据库中
* @param smgpMsg
* @throws java.lang.Exception
*/
public void saveMOData(CMPP cmppMsg) throws Exception {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = ConnectionPool.getConnection();
//将状态报告,Deliver消息插入到数据库得SQL语句
CMPP submitMsg = cmppMsg;
if (submitMsg.Registered_Delivery == 1) {
//当为状态报告时
String strSql = "insert into cmpp_report (Msg_Id,Stat,Submit_time,Done_time,Dest_terminal_Id,SMSC_sequence) ";
strSql += "values(?,?,?,?,?,?)";
pstmt = conn.prepareStatement(strSql);
pstmt.setLong(1, submitMsg.Sequence_Id);
pstmt.setString(2, submitMsg.Report_Stat);
pstmt.setString(3, submitMsg.Report_Submit_time);
pstmt.setString(4, submitMsg.Report_Done_time);
pstmt.setString(5, submitMsg.Report_Dest_terminal_Id);
pstmt.setInt(6, submitMsg.Report_SMSC_sequence);
pstmt.execute();
pstmt.close();
}
else {
//当为Deliver消息时
StringBuffer sbSql = new StringBuffer("");
//CMPP3.0适应性修改,修改了表结构
sbSql.append("insert into cmpp30_from_ismg (");
sbSql.append(
"msg_id,isreport, service_code,src_number, src_number_type");
sbSql.append(
"tp_pid,tp_udhi,dest_number,msg_fmt, msg_content,");
sbSql.append(
"sys_time,link_id ) values (?,?,?,?,?,?,?,?,?,?,sysdate,?)");
pstmt = conn.prepareStatement(sbSql.toString());
pstmt.setString(1, String.valueOf(submitMsg.Msg_Id));
pstmt.setString(2, String.valueOf(submitMsg.Registered_Delivery));
pstmt.setString(3, submitMsg.Service_Id);
pstmt.setString(4, submitMsg.Src_terminal_Id);
pstmt.setByte(5, submitMsg.Src_terminal_type);
pstmt.setByte(6, submitMsg.TP_pid);
pstmt.setByte(7, submitMsg.TP_udhi);
pstmt.setString(8, submitMsg.Dest_terminal_Id);
pstmt.setByte(9, submitMsg.Msg_Fmt);
pstmt.setString(10, submitMsg.Msg_Content);
pstmt.setString(11, submitMsg.LinkID);
pstmt.execute();
pstmt.close();
}
}
catch (Exception ex) {
throw ex;
}
finally {
discloseconn(conn, pstmt, null);
}
}
/**
* 将vctMOData中的MO数据保存到数据库中
* @param vctMOData
* @throws java.lang.Exception
*/
public void saveMOData(Vector vctMOData) throws Exception{
Connection conn = null;
PreparedStatement pstmt = null;
try{
conn = ConnectionPool.getConnection();
//将状态报告,Deliver消息插入到数据库得SQL语句
while(vctMOData.size()>0){
CMPP submitMsg = (CMPP)vctMOData.elementAt(0);
if(submitMsg.Registered_Delivery == 1){
//当为状态报告时
String strSql = "insert into cmpp_report (Msg_Id,Stat,Submit_time,Done_time,Dest_terminal_Id,SMSC_sequence) ";
strSql+= "values(?,?,?,?,?,?)";
pstmt = conn.prepareStatement(strSql);
//System.out.println("Report "+ submitMsg.Report_Msg_Id);
pstmt.setLong(1,submitMsg.Report_Msg_Id);
pstmt.setString(2,submitMsg.Report_Stat);
pstmt.setString(3,submitMsg.Report_Submit_time);
pstmt.setString(4,submitMsg.Report_Done_time);
pstmt.setString(5,submitMsg.Report_Dest_terminal_Id);
pstmt.setInt(6,submitMsg.Report_SMSC_sequence);
pstmt.execute();
pstmt.close();
}
else{
//当为Deliver消息时
StringBuffer sbSql = new StringBuffer("");
// sbSql.append("insert into cmpp_from_ismg ( from_ismg_id, deliver_msg_id, deliver_destnation_id,");
// sbSql.append("deliver_service_id, deliver_msg_fmt, deliver_src_terminal_id, deliver_registered_delivery,");
// sbSql.append("deliver_msg_length, deliver_msg_content");
// sbSql.append(") values (seq_sms.nextval,");
//
// sbSql.append("" +submitMsg.Msg_Id+",");
// sbSql.append("'"+submitMsg.Dest_terminal_Id+"',");
// sbSql.append("'"+submitMsg.Service_Id+"',");
// sbSql.append("" +submitMsg.Msg_Fmt+",");
// sbSql.append("'"+submitMsg.Src_terminal_Id+"',");
// sbSql.append("'"+submitMsg.Registered_Delivery+"',");
// sbSql.append("'"+submitMsg.Msg_Length+"',");
// sbSql.append("'"+submitMsg.Msg_Content+"'");
// sbSql.append(")");
// stmt.addBatch(sbSql.toString());
//CMPP3.0适应性修改,修改了表结构
sbSql.append("insert into cmpp_mo_hn ( msg_id,dest_number,service_code,msg_fmt,tp_pid,tp_udhi,src_number_type,src_number,");
sbSql.append("isreport,msg_content, sys_time, link_id ) ");;
sbSql.append("values (?,?,?,?,?,?,?,?,?,?,sysdate,?)");
pstmt = conn.prepareStatement(sbSql.toString());
pstmt.setString(1,String.valueOf(submitMsg.Msg_Id));
pstmt.setString(2,submitMsg.Dest_terminal_Id);
pstmt.setString(3,submitMsg.Service_Id);
pstmt.setByte(4,submitMsg.Msg_Fmt);
pstmt.setByte(5,submitMsg.TP_pid);
pstmt.setByte(6,submitMsg.TP_udhi);
pstmt.setByte(7,submitMsg.Src_terminal_type);
pstmt.setString(8,submitMsg.Src_terminal_Id);
pstmt.setByte(9,submitMsg.Registered_Delivery);
pstmt.setString(10,submitMsg.Msg_Content);
pstmt.setString(11,submitMsg.LinkID);
pstmt.execute();
pstmt.close();
}
vctMOData.removeElementAt(0);
}
}
catch(Exception ex){
CMPP deliverMsg = (CMPP)vctMOData.elementAt(0);
Debug.outInfo("\n[ERROR]"+PublicFunction.getFormatTime()+" 保存数据失败:("+deliverMsg.Src_terminal_Id+") ServiceID:"
+deliverMsg.Service_Id + " SrcUserType:"+deliverMsg.Src_terminal_type+" MsgContent:"+deliverMsg.Msg_Content+ " LinkId:"
+deliverMsg.LinkID + " DestID:"+ deliverMsg.Dest_terminal_Id);
vctMOData.removeElementAt(0);
throw ex;
}
finally{
discloseconn(conn,pstmt,null);
}
}
/**
* 将待发送数据放入缓冲
* @return
*/
public boolean fectchMTDataToBuffer(){
String strSql = "";
// if(CMPParameter.DBType ==1)
// strSql = "select * from (select rownum as my_rownum,a.* from( "+
// "SELECT * FROM cmpp_to_ismg WHERE submit_status = 1"+
// ") a where rownum<= 10 )where my_rownum> 0";
// else
// strSql = "select top 10 * from cmpp_to_ismg where submit_status = 1";
if(CMPParameter.DBType ==1)
strSql = "select * from (select rownum as my_rownum,a.* from( "+
"SELECT * FROM cmpp_mt_hn WHERE status = -1 and sys_time < sysdate"+
") a where rownum<= 10 )where my_rownum> 0";
else
strSql = "select top 10 * from cmpp_mt_hn where status = -1";
ResultSet rs = null;
Statement stmt = null;
Connection conn = null;
try {
//System.out.println("[CMPPMaster]读取待发送信息到缓冲..." + strSql);
conn = ConnectionPool.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(strSql);
}
catch (Exception ex) {
this.isAvail = false;
Debug.outWarn("[CMPPMaster]Fetch data from cmpp_mt_hn table failed: " + ex) ;
return false;
}
byte[] msg;
try {
int num = 0; //取出待发送数据的计数器
String content = "";
while( rs.next()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -