将报文解析的数据存入mysql数据库的过程
1.逻辑
解析报文数据为各字段,存入数组中,将数组存入mysql的表中。
2.解析代码
package sql_ato_mmi;
public class jiexi{
public String[] jie(String p_data){
//数据类型转换
// StringBuffer sb = new StringBuffer();
// for(int i = 0; i < bytes.length; i++) {
// String hex = Integer.toHexString(bytes[i] & 0xFF);
// if(hex.length() < 2){
// sb.append(0);
// }
// sb.append(hex);
// }
// String p_data=sb.toString();
String[] a= new String[100];
int GMsgLength = Integer.parseInt(p_data.substring(14,18), 16);// 报文中描述的应用层数据包的长度
String HGMsg = p_data.substring(0,18);//通用层加应用层报头的长度
//报文类型、源设备、目的设备、长度
int index =0;
a[0]=HGMsg.substring(index, index+2);//2
index +=2;
a[1]=HGMsg.substring(index, index+2);//2
index +=2;
a[2]=HGMsg.substring(index, index+2);//2
index +=2;
a[3]=HGMsg.substring(index, index+4);//4
index +=4;
a[4]=HGMsg.substring(index, index+4);//4,应用层的类型
index +=4;
a[5]=HGMsg.substring(index, index+4);//4
index +=4;
if(a[4].equals("0103")){
String AMsg = p_data.replaceFirst(HGMsg, "");//应用层数据
//int TMsgLength = AMsg.length() / 2;//报文中应用层的长度实际的
int index1 =0;
a[6]=AMsg.substring(index1, index1+2);//2
index1 +=2;
a[7]=AMsg.substring(index1, index1+2);//2
index1 +=2;
a[8]=AMsg.substring(index1, index1+8);//8
index1 +=8;
a[9]=AMsg.substring(index1, index1+8);//8
index1 +=8;
a[10]=AMsg.substring(index1, index1+8);//8
index1 +=8;
a[11]=AMsg.substring(index1, index1+2);//2
index1 +=2;
a[12]=AMsg.substring(index1, index1+2);//2
index1 +=2;
a[13]=AMsg.substring(index1, index1+2);//2
index1 +=2;
a[14]=AMsg.substring(index1, index1+4);//4
index1 +=4;
a[15]=AMsg.substring(index1, index1+4);//4
index1 +=4;
a[16]=AMsg.substring(index1, index1+8);//8
index1 +=8;
a[17]=AMsg.substring(index1, index1+4);//4
index1 +=4;
a[18]=AMsg.substring(index1, index1+4);//4
index1 +=4;
a[19]=AMsg.substring(index1, index1+2);//2
index1 +=2;
a[20]=AMsg.substring(index1, index1+2);//2
index1 +=2;
a[21]=AMsg.substring(index1, index1+8);//8
index1 +=8;
a[22]=AMsg.substring(index1, index1+2);//2
}else{ //mmitoato
String AMsg = p_data.replaceFirst(HGMsg, "");//应用层数据
int index1 =0;
a[6]=AMsg.substring(index1, index1+2);//2
}
return a;
}
}
3.连接数据库存储代码
package sql_ato_mmi;
//数据库操作类
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class sqlcon {
//连接数据库
// public static void lianjie(){
// Connection conn;
// String driver = "com.mysql.cj.jdbc.Driver";
// String url = "jdbc:mysql://localhost:3306/baowen?serverTimezone=UTC";
// String user = "root";
// String password = "123456";
// try{
// Class.forName(driver); //用class加载动态链接库——驱动程序???
// conn = DriverManager.getConnection(url,user,password); //利用信息链接数据库
// if(!conn.isClosed()){
// System.out.println("Succeeded connecting to the Database!");
// }
// conn.close();
// }catch(ClassNotFoundException e){ //catch不同的错误信息,并报错
// System.out.println("Sorry,can`t find the Driver!");
// e.printStackTrace();
// }catch(SQLException e){
// e.printStackTrace();
// }finally{
// System.out.println("数据库连接成功!");
// }
// }
//插入数据
public void insertdata1(Connection conn,String[] array) throws SQLException {
try{
String tt = "insert into atotommi(Msgtype,Sendid,Receiveid,Msglength,Mtype,Mlength,ATOWork,ATSCommandType,M_ATSGROUP_CO,M_DATA_VERSION,M_CHECK_ERRCODE,M_START_ERRCODE,M_FORCE_STATUS,M_DOORMODE,NID_VEHICLE,NID_TRAIN,NID_DESTINATION,NID_DRIVER,M_RUNTIME_STATUS,M_SKIP_STATUS,M_TASK_STATUS,NID_NEXT_PLATFORM,M_BRAKE_STATUS)"+"values("+"\""+array[0]+"\""+","+"\""+array[1]+"\""+","+"\""+array[2]+"\""+","+"\""+array[3]+"\""+","+"\""+array[4]+"\""+","+"\""+array[5]+"\""+","+"\""+array[6]+"\""+","+"\""+array[7]+"\""+","+"\""+array[8]+"\""+","+"\""+array[9]+"\""+","+"\""+array[10]+"\""+","+"\""+array[11]+"\""+","+"\""+array[12]+"\""+","+"\""+array[13]+"\""+","+"\""+array[14]+"\""+","+"\""+array[15]+"\""+","+"\""+array[16]+"\""+","+"\""+array[17]+"\""+","+"\""+array[18]+"\""+","+"\""+array[19]+"\""+","+"\""+array[20]+"\""+","+"\""+array[21]+"\""+","+"\""+array[22]+"\""+")";
System.out.println(tt);
PreparedStatement psql = conn.prepareStatement(tt);
psql.executeUpdate(); //参数准备后执行语句
psql.close();
}catch(SQLException e){
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally{
System.out.println("数据库数据插入成功!"+"\n");
}
}
public void insertdata2(Connection conn,String[] array) throws SQLException {
try{
String tt = "insert into atotommi(Msgtype,Sendid,Receiveid,Msglength,Mtype,Mlength,NID_DRIVER)"+"values("+"\""+array[0]+"\""+","+"\""+array[1]+"\""+","+"\""+array[2]+"\""+","+"\""+array[3]+"\""+","+"\""+array[4]+"\""+","+"\""+array[5]+"\""+","+"\""+array[6]+"\""+")";
System.out.println(tt);
PreparedStatement psql = conn.prepareStatement(tt);
psql.executeUpdate(); //参数准备后执行语句
psql.close();
}catch(SQLException e){
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally{
System.out.println("数据库数据插入成功!"+"\n");
}
}
//查询语句
public void fetchdata(Connection conn,String[] array) throws SQLException {
try
{
Statement statement = conn.createStatement(); //用statement 来执行sql语句
String sql = "select * from atotommi"; //b是sql语句中的查询某个表,注意后面的emp是表名!!!
ResultSet rs = statement.executeQuery(sql); //用于返回结果
System.out.println("---------------------------------------------------");
System.out.println("表中的数据有:");
System.out.println("---------------------------------------------------");
String job = null;
String id = null;
String date = null;
String Msgtype = null;
String Sendid = null;
String Receiveid = null;
String Msglength= null;
String Mtype= null;
String Mlength= null;
String ATOWork= null;
String ATSCommandType= null;
String M_ATSGROUP_CO= null;
String M_DATA_VERSION= null;
String M_CHECK_ERRCODE= null;
String M_START_ERRCODE= null;
String M_FORCE_STATUS= null;
String M_DOORMODE= null;
String NID_VEHICLE= null;
String NID_TRAIN= null;
String NID_DESTINATION= null;
String NID_DRIVER= null;
String M_RUNTIME_STATUS= null;
String M_SKIP_STATUS= null;
String M_TASK_STATUS= null;
String NID_NEXT_PLATFORM= null;
String M_BRAKE_STATUS= null;
while(rs.next()){
System.out.println(id + "\t\t" + job + "\t" + date);
Msgtype = rs.getString("Msgtype");
Sendid = rs.getString("Sendid");
Receiveid = rs.getString("Receiveid");
Msglength = rs.getString("Msglength");
ATOWork = rs.getString("ATOWork");
ATSCommandType = rs.getString("ATSCommandType");
M_ATSGROUP_CO = rs.getString("M_ATSGROUP_CO");
M_DATA_VERSION = rs.getString("M_DATA_VERSION");
M_CHECK_ERRCODE = rs.getString("M_CHECK_ERRCODE");
M_START_ERRCODE = rs.getString("M_START_ERRCODE");
M_FORCE_STATUS = rs.getString("M_FORCE_STATUS");
M_DOORMODE = rs.getString("M_DOORMODE");
NID_VEHICLE = rs.getString("NID_VEHICLE");
NID_TRAIN = rs.getString("NID_TRAIN");
NID_DESTINATION = rs.getString("NID_DESTINATION");
NID_DRIVER = rs.getString("NID_DRIVER");
M_RUNTIME_STATUS = rs.getString("M_RUNTIME_STATUS");
M_SKIP_STATUS = rs.getString("M_SKIP_STATUS");
M_TASK_STATUS = rs.getString("M_TASK_STATUS");
NID_NEXT_PLATFORM = rs.getString("NID_NEXT_PLATFORM");
M_BRAKE_STATUS = rs.getString("M_BRAKE_STATUS");
}
rs.close();
}catch(SQLException e){
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally{
System.out.println("数据库数据读取成功!"+"\n");
}
}
}