📄 pushbean.java.bak
字号:
若返回的是-1插入失败
若返回的是1插入成功
若返回的是0,则rs没有一个结果
先连续插入3次,若插入还是不成功,则重连数据库
重新插入,若一个小时仍插入不成功,就重连数据库
*/
public int reconnectInsert(ResultSet rs){
int flag = -1;
int i = 0;
try {
for( i=0;i<3;i++){
this.sleep(i * 10000);
if (this.insertRecord(rs) == 1)
return 1;
}
i=1;
do{
//this.Close();//关闭数据库
while(!this.reconnectDatabase());//重连数据库知道连接成功为止
m_nStartID-=100;//重复上次的查询结果,因此,起始ID要减去100
while(rs == null){
rs = this.reGetRecord();//重查询结果,知道查询出结果为止
}
this.sleep(i*10000);
if(i == 360){
i = 360;
System.out.println("can not insert for long time");
}
i++;
flag = this.insertRecord(rs);
}while(flag == -1);
}catch (InterruptedException ex) {
}
finally{
return flag;
}
}
/*
将StartID写入配置文件
*/
public boolean writeStartID(){
boolean flag = false;
try{
CommonMethods.setConfigString("StartID",String.valueOf(m_nStartID));
System.out.println("CommonMethods.setConfigString finished");
System.out.println(String.valueOf(m_nStartID));
flag = true;
}catch(Exception e){
flag = false;
}
finally{
return flag;
}
}
/*
获得MaxID的值
若查找失败,返回-1;
*/
public int getMaxID() throws SQLException{
ResultSet rs = null;
int i = -1;
try{
rs = stmt2.executeQuery("select max(ID) as m_nMaxID from T_OTA_PUSH ");
rs.next();
i = rs.getInt("m_nMaxID");
}catch (Exception e){
i = -1;//若查找失败,从第一条数据开始,m_nMaxID恢复为1
e.printStackTrace();
}finally{
return i;
}
}
/*
轮询t_ota_push表
true表示pushall执行成功
flag表示pushall执行失败
*/
public boolean pushall() throws SQLException {
ResultSet rs = null;
ArrayList idList = new ArrayList();
boolean flag = false;
int ID = 0;
String sql = null;
try {
sql = "select * from t_ota_push where Act_type = '03' and PUSH_FILE_DATA = '01' and REQUIRE_DATE <= sysdate and NVL(DEAL_DATE, REQUIRE_DATE) >= REQUIRE_DATE";//应该是NVL(DEAL_DATE, REQUIRE_DATE) <= REQUIRE_DATE
rs = this.executeQuery(sql);
if (rs.next()) {//记录所有的ID 和 m_strAid
m_strAid = rs.getString("AID");
do {
ID = rs.getInt("ID");
m_strAid = rs.getString("Aid");
idList.add(String.valueOf(ID));
m_arrm_strAidList.add(m_strAid);
} while(rs.next());
}
else {
throw new Exception("未查找到任何数据");//没有查找到任何数据
}
rs.close();
for(int i = 0; i<idList.size(); i++){
sql = "update t_ota_push set deal_date = sysdate where ID = "+(String)idList.get(i);
stmt.executeUpdate(sql);
System.out.println("UPDATE SUCCESS");
}
flag = true;
}
catch (SQLException e) {
e.printStackTrace();
flag = false;
}
finally{
return flag;
}
}
/*
从m_nStartID开始顺序取出100条数据
*/
public ResultSet getRecord(){
ResultSet rs = null;
String sql = null;
try{
sql = "select * from t_user_info where ID >="
+ m_nStartID + " AND ID < " + (m_nStartID + 100);
rs = stmt.executeQuery(sql);
m_nStartID += 100;
} catch(Exception e){
e.printStackTrace();
rs = null;
System.out.println("Draw Record fail");
}
finally{
return rs;
}
}
/*
出入100条数据,若插入失败则返回false
若插入成功。则返回true
*/
public boolean insertOneRecord(ResultSet rs) {
String phone_num = null;
String sql = null;
int nID = 0;
boolean flag = false;
try{
nID= rs.getInt("ID");
System.out.println(nID);
this.sleep(m_nVec * 1000);
phone_num = rs.getString("PHONE_NUM");
this.setMaxID(this.getMaxID()+1); //在最大的ID后插入数据
sql = "insert into t_ota_push(ID,FROM_NUM,TO_NUM,USER_LEVEL,act_type,Aid,PUSH_FILE_DATA,REQUIRE_DATE,DEAL_DATE,PROCESS_FLAG) values("
+ m_nMaxID + ",'" + phone_num + "','" + phone_num + "'," +
String.valueOf(m_nUserlevel) + ",'" + m_strActtype + "','" + m_strAid +
"','" + m_strPushFileData + "',to_date("
+ m_dateRundate.addSecond(m_nVec*nID).toFigureString() +
",'yyyymmddhh24:mi:ss'),Sysdate,'1')";
stmt2.executeUpdate(sql);
flag = true;
}catch(Exception e){
e.printStackTrace();
flag = false;
}
finally{
return flag;
}
}
/*
输入参数为一个ResultSet 类,
根据这个类插入所有的数据
返回的为一个int型数据,
若返回的是-1插入失败,执行步骤有异常
若返回的是1插入成功
若返回的是0,则rs没有一个结果
*/
public int insertRecord(ResultSet rs){
int flag = -1;
try{
rs.next();
if (rs.isLast())
flag = 0; //若返回的是0,则rs没有一个结果
do{
this.insertOneRecord(rs);
}while(rs.next());
flag = 1;
}catch(Exception e){
e.printStackTrace();
flag = -1;
}
finally{
try {
rs.close();
}
catch (SQLException ex) {
}
return flag;
}
}
/*
从t_ota_push表中删除已经执行的记录,mycurdate为当前时间减去2小时
若返回的是false删除失败
若返回的是true删除成功
*/
public boolean deleteRecord() throws SQLException {
String sql = null;
boolean flag = false;
myDate date = new myDate();
date.setHours(date.getHours()-2);
try{
sql = "delete from t_ota_push where act_type='03' and process_flag='0' and to_date("
+date.toFigureString()+",'yyyymmddhh:mi:ss')> deal_date";
stmt.executeUpdate(sql);
flag = true;
}catch(Exception e){
flag = false;
e.printStackTrace();
}
finally{
return flag;
}
}
public void run() {
short nDeleteCount = 0;//用来计数,当它达到一定的值时,执行DELETE函数,
ResultSet rs = null;
boolean flag = false;
int nInsRes = 0;
getRundate();//获得当前运行时间
try {
if(!getInfoFromInit())
throw new Exception("读取配置文件错误");
if (!connect()) {
while(!reconnectDatabase()); //重连数据库一次
}
while(true){
rs = getRecord();
while(rs == null) {//查询数据库不成功,重新查询
rs = reGetRecord();
};//end if(rs == null)
nInsRes = insertRecord(rs);
if( nInsRes == 0 ) break;//未查到一个结果,则表示该任务执行成功,结束线程
if( nInsRes == -1 ) continue;//插入不成功,则略过,重新来过
//定时执行删除功能
nDeleteCount++;
if (nDeleteCount == 60) {//间隔一个小时删除一次记录
deleteRecord();
nDeleteCount = 0;
writeStartID(); //将StartID的值写入配置文件中
}
}//end while(true)
}//end try
catch (SQLException e2) {
e2.printStackTrace();
}
catch (Exception e3){
e3.printStackTrace();
}
finally{
try {
this.Close();
System.out.println("complete");
}catch (SQLException ex1) {}
}
}
/*
连续执行insertOtapush函数10次,若执行仍然失败,返回false
若执行成功,就返回true
*/
public boolean insertAgain(ResultSet rs){
int i = 0;
boolean flag = false;
try {
while (i < 10) {
if(this.insertOneRecord(rs) == true)
{
flag = true;
break;
}
sleep(10000);//每隔10秒执行一次
}
flag = false;
rs.close();
}
catch (InterruptedException ex) {
flag = false;
}
finally{
return flag;
}
}
//测试使用
public void Test() throws SQLException {
ResultSet rs1 = null;
ResultSet rs2 = null;
String sql = null;
try{
this.connect();
sql = "select max(rownum) as maxid from t_user_info ";
rs1 = stmt.executeQuery(sql);
rs1.next();
// System.out.println(stmt2.executeUpdate("delete from t_user_info where id>10000"));
System.out.println(rs1.getInt("maxid"));
connect.commit();
}catch(Exception e){
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -