Серверный сокет получил данные, которые не удалось вставить в базу данных MySQL - PullRequest
0 голосов
/ 18 октября 2019

Я сделал программу Server Socket на Java, в которой я использую поток для прослушивания данных через сокет клиента. Но клиентский запрос приходит через каждые 2 секунды, а размер данных составляет около 4096 байт. Когда данные попадают в поток, я конвертирую байт в реальную форму и пытаюсь обновить фактические данные в таблицу. Я включил в это служебный класс и записал логику вставки данных, но нужно использовать 10 запросов на обновление. Во всем этом процессе клиентские данные теряются, иногда он вставляет несколько строк, иногда нет.

public static void parseOneOOThreeData(byte[] layerOne, byte[] zoneOne, byte[] layerTwo, byte[] zoneTwo,
            byte[] layerThree, byte[] zoneThree, byte[] layerFour, byte[] zoneFour, byte[] layerFive, byte[] zoneFive,
            byte[] layerSix, byte[] zoneSix, byte[] layerSeven, byte[] zoneSeven, byte[] layerEight, byte[] zoneEight,
            byte[] layerNine, byte[] zoneNine, byte[] layerTen, byte[] zoneTen) {

        DBConnection dbConn = new DBConnection();
        Connection con = dbConn.getCurrentConnection();
        Statement statement = null;
        try {
            statement = con.createStatement();
            String layerOneQuery = getUpdateQuery(layerOne, zoneOne, 1);
            statement.addBatch(getUpdateQuery(layerOne, zoneOne, 1));// inserting Query in stmt
            statement.addBatch(getUpdateQuery(layerTwo, zoneTwo, 2));
            statement.addBatch(getUpdateQuery(layerThree, zoneThree, 3));
            statement.addBatch(getUpdateQuery(layerFour, zoneFour, 4));
            statement.addBatch(getUpdateQuery(layerFive, zoneFive, 5));
            int[] recordsAffected = statement.executeBatch();
            System.out.println("update " + recordsAffected);
        } catch (Exception e) {

        } finally {
            if (statement != null)
                try {
                    statement.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
    }

    public static String getUpdateQuery(byte[] lineData, byte[] zoneData, int id) {
        String updateQuery = null;
        try {
            // Calculated only for int values, offset of first int parameter is 0
            byte[] messageIntByte = new byte[40];
            int j = 0;
            for (int i = 0; i < lineData.length; i++) {
                if (j < 40)
                    messageIntByte[j] = lineData[i];
                j++;
                if (i == 3)
                    i += 16;
                else if (i == 31)
                    i += 24;
                else if (i == 59)
                    i += 52;
                else if (i == 115)
                    i += 68;
                else if (i == 119)
                    i = lineData.length;
            }
            ByteBuffer bufferInt = ByteBuffer.wrap(messageIntByte);
            IntBuffer ib = bufferInt.asIntBuffer();
            int[] intValues = new int[10];
            ib.get(intValues);

            // Sorted all short values & stored into byte array, offset of first short
            // parameter is 4
            byte[] messageShortByte = new byte[128];
            int l = 0;
            for (int k = 4; k < lineData.length; k++) {
                if (l < 128)
                    messageShortByte[l] = lineData[k];
                l++;
                if (k == 19) {
                    k += 28;
                } else if (k == 55) {
                    k += 20;
                } else if (k == 79) {
                    k += 16;
                } else if (k == 111) {
                    k += 48;
                } else if (k == 183) {
                    k += 16;
                } else if (k == 207) {
                    k += 28;
                } else if (k == 257) {
                    k += 12;
                }
            }
            ByteBuffer buffer = ByteBuffer.wrap(messageShortByte);
            ShortBuffer shortBuffer = buffer.asShortBuffer();
            short[] shortValues = new short[64];
            shortBuffer.get(shortValues);

            // Sorted all char values & stored into byte array, offset of first short
            // parameter is 4
            byte[] messageCharByte = new byte[135];
            int n = 0;
            for (int o = 32; o < lineData.length; o++) {
                if (n < 135)
                    messageCharByte[n] = lineData[o];
                n++;
                if (o == 47) {
                    o += 12;
                } else if (o == 75) {
                    o += 4;
                } else if (o == 95) {
                    o += 20;
                } else if (o == 159) {
                    o += 48;
                } else if (o == 235) {
                    o += 22;
                } else if (o == 269) {
                    o = lineData.length;
                }
            }
            ByteBuffer byteBufferChar = ByteBuffer.wrap(messageCharByte);
            CharBuffer charBuffer = Charset.forName("ISO-8859-1").decode(byteBufferChar);

            byte[] zoneCharByte = new byte[7];
            int y = 0;
            for (int x = 0; x < zoneData.length; x++) {
                zoneCharByte[y] = zoneData[x];
                y++;
            }
            ByteBuffer byteBufferZoneChar = ByteBuffer.wrap(zoneCharByte);
            CharBuffer zoneCharBuffer = Charset.forName("ISO-8859-1").decode(byteBufferZoneChar);

            char[] charValues = charBuffer.array();
            char[] zoneValues = zoneCharBuffer.array();

            updateQuery = "UPDATE tbl_tc_1003 set seq_key='" + intValues[0] + "', coil='" + shortValues[0] + "', cp='"
                    + shortValues[1] + "', wp='" + shortValues[2] + "', ip='" + shortValues[3] + "', gcp='"
                    + shortValues[4] + "', dp='" + shortValues[5] + "', fhcp='" + shortValues[6] + "', pad01='"
                    + shortValues[7] + "', primhot='" + intValues[1] + "', primcol='" + intValues[2] + "', primill='"
                    + intValues[3] + "', hcolno_1='" + charValues[0] + "', hcolno_2='" + charValues[1] + "', hcolno_3='"
                    + charValues[2] + "', hcolno_4='" + charValues[3] + "', hcolno_5='" + charValues[4]
                    + "', hcolno_6='" + charValues[5] + "', hcolno_7='" + charValues[6] + "', hcolno_8='"
                    + charValues[7] + "', hcolno_9='" + charValues[8] + "', hcolno_10='" + charValues[9]
                    + "', hcolno_11='" + charValues[10] + "', hcolno_12='" + charValues[11] + "', hcolno_13='"
                    + charValues[12] + "', pad02_1='" + charValues[13] + "', pad02_2='" + charValues[14]
                    + "', pad02_3='" + charValues[15] + "', hthick='" + shortValues[8] + "', hwidth='" + shortValues[9]
                    + "', hlength='" + shortValues[10] + "', hspare='" + shortValues[11] + "', hweight='" + intValues[4]
                    + "', steel_n_1='" + charValues[16] + "', steel_n_2='" + charValues[17] + "', steel_n_3='"
                    + charValues[18] + "', steel_n_4='" + charValues[19] + "', steel_n_5='" + charValues[20]
                    + "', steel_n_6='" + charValues[21] + "', steel_n_7='" + charValues[22] + "', steel_n_8='"
                    + charValues[23] + "', steel_n_9='" + charValues[24] + "', steel_n_10='" + charValues[25]
                    + "', steel_n_11='" + charValues[26] + "', steel_n_12='" + charValues[27] + "', steel_n_13='"
                    + charValues[28] + "', steel_n_14='" + charValues[29] + "', nextp_1='" + charValues[30]
                    + "', nextp_2='" + charValues[31] + "', yp='" + shortValues[12] + "', mpayp='" + shortValues[13]
                    + "', ccolno_1='" + charValues[32] + "', ccolno_2='" + charValues[33] + "', ccolno_3='"
                    + charValues[34] + "', ccolno_4='" + charValues[35] + "', ccolno_5='" + charValues[36]
                    + "', ccolno_6='" + charValues[37] + "', ccolno_7='" + charValues[38] + "', ccolno_8='"
                    + charValues[39] + "', ccolno_9='" + charValues[40] + "', ccolno_10='" + charValues[41]
                    + "', ccolno_11='" + charValues[42] + "', ccolno_12='" + charValues[43] + "', ccolno_13='"
                    + charValues[44] + "', pad04_1='" + charValues[45] + "', pad04_2='" + charValues[46]
                    + "', pad04_3='" + charValues[47] + "', cthick='" + shortValues[14] + "', cwidth='"
                    + shortValues[15] + "', owidth='" + shortValues[16] + "', totclen='" + shortValues[17]
                    + "', flhdst='" + shortValues[18] + "', fhcpcnt='" + shortValues[19] + "', fhcplen='"
                    + shortValues[20] + "', hctemp='" + shortValues[21] + "', tiwei='" + intValues[5] + "', hcspmym_1='"
                    + charValues[48] + "', hcspmym_2='" + charValues[49] + "', stcod_1='" + charValues[50]
                    + "', stcod_2='" + charValues[51] + "', user_n_1='" + charValues[52] + "', user_n_2='"
                    + charValues[53] + "', user_n_3='" + charValues[54] + "', user_n_4='" + charValues[55]
                    + "', user_n_5='" + charValues[56] + "', user_n_6='" + charValues[57] + "', trimcd_1='"
                    + charValues[58] + "', trimcd_2='" + charValues[59] + "', nextp_n_1='" + charValues[60]
                    + "', nextp_n_2='" + charValues[61] + "', nextp_n_3='" + charValues[62] + "', pcourse_n_1='"
                    + charValues[63] + "', pcourse_n_2='" + charValues[64] + "', pcourse_n_3='" + charValues[65]
                    + "', pcourse_n_4='" + charValues[66] + "', pcourse_n_5='" + charValues[67] + "', pcourse_n_6='"
                    + charValues[68] + "', pcourse_n_7='" + charValues[69] + "', pcourse_n_8='" + charValues[70]
                    + "', pcourse_n_9='" + charValues[71] + "', pcourse_n_10='" + charValues[72] + "', pcourse_n_11='"
                    + charValues[73] + "', pcourse_n_12='" + charValues[74] + "', pcourse_n_13='" + charValues[75]
                    + "', pcourse_n_14='" + charValues[76] + "', pcourse_n_15='" + charValues[77] + "', pcourse_n_16='"
                    + charValues[78] + "', pcourse_n_17='" + charValues[79] + "', pcourse_n_18='" + charValues[80]
                    + "', pcourse_n_19='" + charValues[81] + "', pcourse_n_20='" + charValues[82] + "', pcourse_n_21='"
                    + charValues[83] + "', pcourse_n_22='" + charValues[84] + "', pcourse_n_23='" + charValues[85]
                    + "', pcourse_n_24='" + charValues[86] + "', user_1='" + charValues[87] + "', user_2='"
                    + charValues[88] + "', user_3='" + charValues[89] + "', user_4='" + charValues[90]
                    + "', padding01='" + charValues[91] + "', cpoint='" + shortValues[22] + "', color='"
                    + shortValues[23] + "', lot='" + shortValues[24] + "', cseqno='" + shortValues[25] + "', porno='"
                    + shortValues[26] + "', fstwid='" + shortValues[27] + "', osample='" + shortValues[28]
                    + "', masd_flg='" + shortValues[29] + "', insp_flg='" + shortValues[30] + "', forcefgc='"
                    + shortValues[31] + "', finish='" + shortValues[32] + "', blink_index='" + shortValues[33]
                    + "', sink_count='" + intValues[6] + "', o1pwor='" + intValues[7] + "', osomax='" + intValues[8]
                    + "', ojngmax='" + intValues[9] + "', matcode='" + shortValues[34] + "', porflag='"
                    + shortValues[35] + "', new_sg_flag='" + shortValues[36] + "', slv_use_flg='" + shortValues[37]
                    + "', spec_cd_1='" + charValues[92] + "', spec_cd_2='" + charValues[93] + "', spec_cd_3='"
                    + charValues[94] + "', spec_cd_4='" + charValues[95] + "', sface_1='" + charValues[96]
                    + "', pad06_1='" + charValues[97] + "', luser_1='" + charValues[98] + "', luser_2='"
                    + charValues[99] + "', luser_3='" + charValues[100] + "', luser_4='" + charValues[101]
                    + "', luser_5='" + charValues[102] + "', luser_6='" + charValues[103] + "', luser_7='"
                    + charValues[104] + "', stdbunho_1='" + charValues[105] + "', stdbunho_2='" + charValues[106]
                    + "', stdbunho_3='" + charValues[107] + "', stdbunho_4='" + charValues[108] + "', stdbunho_5='"
                    + charValues[109] + "', stdbunho_6='" + charValues[110] + "', stdbunho_7='" + charValues[111]
                    + "', stdbunho_8='" + charValues[112] + "', stdbunho_9='" + charValues[113]
                    + "', ord_pdt_itds_cd_n_1='" + charValues[114] + "', ord_pdt_itds_cd_n_2='" + charValues[115]
                    + "', ord_pdt_itds_cd_n_3='" + charValues[116] + "', ord_pdt_itds_cd_n_4='" + charValues[117]
                    + "', padding02_1='" + charValues[118] + "', padding02_2='" + charValues[119] + "', owthmin='"
                    + shortValues[38] + "', owthmax='" + shortValues[39] + "', fhvasflag='" + shortValues[40]
                    + "', othick='" + shortValues[41] + "', fhcalfail='" + shortValues[42] + "', strimflg='"
                    + shortValues[43] + "', hactr_flag='" + shortValues[44] + "', cr_inni_hr_rgs_yp='" + shortValues[45]
                    + "', m_inni_hr_rgs_yp='" + shortValues[46] + "', wpd_cnt='" + shortValues[47] + "', wpdover_flg='"
                    + shortValues[48] + "', steel_1='" + charValues[120] + "', steel_2='" + charValues[121]
                    + "', steel_3='" + charValues[122] + "', steel_4='" + charValues[123] + "', steel_5='"
                    + charValues[124] + "', steel_6='" + charValues[125] + "', steel_7='" + charValues[126]
                    + "', steel_8='" + charValues[127] + "', steel_9='" + charValues[128] + "', steel_10='"
                    + charValues[129] + "', steel_11='" + charValues[130] + "', pad09='" + charValues[131]
                    + "', space02_1='" + shortValues[49] + "', space02_2='" + shortValues[50] + "', space02_3='"
                    + shortValues[51] + "', space02_4='" + shortValues[52] + "', space02_5='" + shortValues[53]
                    + "', zone_1='" + zoneValues[0] + "', zone_2='" + zoneValues[1] + "', zone_3='" + zoneValues[2]
                    + "', zone_4='" + zoneValues[3] + "', zone_5='" + zoneValues[4] + "', zone_6='" + zoneValues[5]
                    + "', zone_7='" + zoneValues[6] + "' where id=" + id;
        } catch (Exception e) {

        }
        return updateQuery;
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...