// 更新弧光次数 // 获取数据库中是否有当天的弧光次数 protected void updateHgcishu(String Sample_ID, int hg_high, int hg_low) throws SQLException { int hg_count = hg_high + hg_low; SimpleDateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd"); String checkHgCount = "select count(*) from Hgcishu where Datetime = CONVERT(varchar(100), GETDATE(), 23) and Sample_ID = '" + Sample_ID + "'"; // 查询数据库中的弧光次数 String getHgCount = "select Qianghu,Ruohu,Total,HgFlag from Hgcishu where Sample_ID = '" + Sample_ID + "' and Datetime = CONVERT(varchar(100), GETDATE(), 23)"; // 获取数据库中标志位 // String getFlag = "select HgFlag from Hgcishu where Sample_ID = '"+ Sample_ID // +"' and Datetime = CONVERT(varchar(100), GETDATE(), 23)"; ResultSet rs = null; rs = ds.select(checkHgCount); int count = 0; if (rs != null) { if (rs.next()) count = rs.getInt(1); } if (count == 0) { // 向数据库中添加一条当天的弧光次数记录 String insertHgCount = "insert into Hgcishu(Sample_ID,Qianghu,Ruohu,Total,Datetime) values('" + Sample_ID + "'," + hg_high + "," + hg_low + "," + hg_count + ",'" + dfDate.format(new Date()) + "')"; ds.update(insertHgCount); } else { if (hg_count != 0) { rs = ds.select(getHgCount); if (rs.next()) { int hg_high_old = rs.getInt(1); int hg_low_old = rs.getInt(2); int hg_count_old = rs.getInt(3); // System.out.println(hg_count_old); // System.out.println(hg_count); hg_high += hg_high_old; hg_low += hg_low_old; hg_count += hg_count_old; // System.out.println(hg_count); // 更新数据库中的弧光次数 String updateHgCount = "update Hgcishu set Qianghu =" + hg_high + ",Ruohu=" + hg_low + ",Total=" + hg_count + " where Sample_ID = '" + Sample_ID + "' and Datetime = CONVERT(varchar(100), GETDATE(), 23)"; ds.update(updateHgCount); } } } }
protected void receiveHg(byte[] returnData, int returnLenght, String Sample_ID) throws SQLException, IOException { ResultSet rs = null; String device_Address = ""; device_Address = bs.bytesToHexString(returnData[0]); int hg_count = 0; int hg_high = 0; int hg_low = 0; SimpleDateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd"); hg_count = (returnData[3] & 0xFF); // 总次数 hg_high = (returnData[5] & 0xFF); // 强弧次数 hg_low = hg_count - hg_high; // 弱弧次数 int hg_recent1_time = 0; int hg_recent1_date = 0; String hg_1_date = ""; hg_recent1_time = (returnData[7] & 0xFF); hg_recent1_date = ((returnData[8] << 8) & 0xFF00) | (returnData[9] & 0xFF); Date d = new Date(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); hg_1_date = df.format(new Date(d.getTime() - hg_recent1_date * 1000)); int hg_recent2_time = 0; int hg_recent2_date = 0; String hg_2_date = ""; hg_recent2_time = (returnData[11] & 0xFF); hg_recent2_date = ((returnData[12] << 8) & 0xFF00) | (returnData[13] & 0xFF); hg_2_date = df.format(new Date(d.getTime() - hg_recent2_date * 1000)); int hg_recent3_time = 0; int hg_recent3_date = 0; String hg_3_date = ""; hg_recent3_time = (returnData[15] & 0xFF); hg_recent3_date = ((returnData[16] << 8) & 0xFF00) | (returnData[17] & 0xFF); hg_3_date = df.format(new Date(d.getTime() - hg_recent3_date * 1000)); int hg_recent4_time = 0; int hg_recent4_date = 0; String hg_4_date = ""; hg_recent4_time = (returnData[19] & 0xFF); hg_recent4_date = ((returnData[20] << 8) & 0xFF00) | (returnData[21] & 0xFF); hg_4_date = df.format(new Date(d.getTime() - hg_recent4_date * 1000)); int hg_recent5_time = 0; int hg_recent5_date = 0; String hg_5_date = ""; hg_recent5_time = (returnData[23] & 0xFF); hg_recent5_date = ((returnData[24] << 8) & 0xFF00) | (returnData[25] & 0xFF); hg_5_date = df.format(new Date(d.getTime() - hg_recent5_date * 1000)); int hg_recent6_time = 0; int hg_recent6_date = 0; String hg_6_date = ""; hg_recent6_time = (returnData[27] & 0xFF); hg_recent6_date = ((returnData[28] << 8) & 0xFF00) | (returnData[29] & 0xFF); hg_6_date = df.format(new Date(d.getTime() - hg_recent6_date * 1000)); int hg_recent7_time = 0; int hg_recent7_date = 0; String hg_7_date = ""; hg_recent7_time = (returnData[31] & 0xFF); hg_recent7_date = ((returnData[32] << 8) & 0xFF00) | (returnData[33] & 0xFF); hg_7_date = df.format(new Date(d.getTime() - hg_recent7_date * 1000)); // String insertHgNum_count="insert into AlarmLogArc(Sample_ID,AlarmNum,AlarmType,Odate) // values('" // +Sample_ID+"','"+hg_count+"','0','"+df.format(new Date())+"')"; // String insertHgNum_high="insert into AlarmLogArc(Sample_ID,AlarmNum,AlarmType,Odate) // values('" // +Sample_ID+"','"+hg_high+"','1','"+df.format(new Date())+"')"; String insertRecentHg = "insert into recentHg(Sample_ID,hg_recent1_time,hg_recent1_date," + "hg_recent2_time,hg_recent2_date,hg_recent3_time,hg_recent3_date," + "hg_recent4_time,hg_recent4_date,hg_recent5_time,hg_recent5_date," + "hg_recent6_time,hg_recent6_date,hg_recent7_time,hg_recent7_date) " + "values('" + Sample_ID + "'," + hg_recent1_time + ",'" + hg_1_date + "'," + hg_recent2_time + ",'" + hg_2_date + "'," + hg_recent3_time + ",'" + hg_3_date + "'," + hg_recent4_time + ",'" + hg_4_date + "'," + hg_recent5_time + ",'" + hg_5_date + "'," + hg_recent6_time + ",'" + hg_6_date + "'," + hg_recent7_time + ",'" + hg_7_date + "')"; String updateRecentHg = "update recentHg set hg_recent1_time=" + hg_recent1_time + ",hg_recent1_date='" + hg_1_date + "',hg_recent2_time=" + hg_recent2_time + ",hg_recent2_date='" + hg_2_date + "',hg_recent3_time=" + hg_recent3_time + ",hg_recent3_date='" + hg_3_date + "',hg_recent4_time=" + hg_recent4_time + ",hg_recent4_date='" + hg_4_date + "',hg_recent5_time=" + hg_recent5_time + ",hg_recent5_date='" + hg_5_date + "',hg_recent6_time=" + hg_recent6_time + ",hg_recent6_date='" + hg_6_date + "',hg_recent7_time=" + hg_recent7_time + ",hg_recent7_date='" + hg_7_date + "' where Sample_ID='" + Sample_ID + "'"; String checkRecentHg = "select count(*) from recentHg where Sample_ID='" + Sample_ID + "'"; // String getRecentHg= "select hg_recent1_time,hg_recent1_date from recentHg where Sample_ID = // '"+Sample_ID+"'"; // int f1=ds.update(insertHgNum_count); // int f2=ds.update(insertHgNum_high); // 更新最近弧光 int countRecentHg = 0; if (hg_recent1_time != 0) { rs = ds.select(checkRecentHg); if (rs != null) { while (rs.next()) { countRecentHg = rs.getInt(1); } } if (countRecentHg == 0) { ds.update(insertRecentHg); } else { // rs=ds.select(getRecentHg); // if(rs!=null) // { // if(rs.next()) // { // int recentHgTimeOld1 = rs.getInt(1); // if(recentHgTimeOld1 != hg_recent1_time)ds.update(updateRecentHg); // } // } if (hg_count != 0) { ds.update(updateRecentHg); } } } // System.out.println("step into updateHgcishu"); updateHgcishu(Sample_ID, hg_high, hg_low); // System.out.println("step out updateHgcishu"); // rs.close(); System.gc(); }