井盖程序中获取电信推送数据的java代码

UserInfo.java

package com.huawei.com;

import java.io.IOException;
import java.io.PrintStream;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import com.fasterxml.jackson.core.JsonProcessingException;  
import com.fasterxml.jackson.databind.JsonNode;  
import com.fasterxml.jackson.databind.ObjectMapper;

import java.sql.*;

@Path("UserInfoService")
public class UserInfo
{
  // JDBC 驱动名及数据库 URL
  static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
  static final String DB_URL = "jdbc:mysql://localhost:3306/meiqi4?serverTimezone=UTC";///YAJUN->meiqi4
 
  // 数据库的用户名与密码,需要根据自己的设置
  static final String USER = "root";
  static final String PASS = "Ali_常用密码";
  
	
  @GET
  @Path("/name/{i}")
  @Produces({"application/json"})
  public String userName(@PathParam("i") String i)
  {
    String name = i;
    return "{\"name\":\"" + name + "\"}";
  }
  
  @GET
  @Path("/age/{j}")
  @Produces({"text/xml"})
  public String userAge(@PathParam("j") int j)
  {
    int age = j;
    return "<User><Age>" + age + "</Age>" + "</User>";
  }
  
  @POST
  @Path("/subscriber")
  @Consumes({"application/json"})
  @Produces({"application/json"})
  public String subscriber(DeviceDataChanged k)
  {
    String result = k.getDeviceId();
    return result;
  }
  
  @POST
  @Path("/subscriber1")
  public String subscriber1(String k) throws IOException
  {
    String result = k;
    
    Connection conn = null;
    Statement stmt = null;
    
    String[] display_str_array = new String[10];
    
    //解析数据
    try{
    	System.out.println("\r\ntry\r\n");
    	ObjectMapper mapper = new ObjectMapper();  
        JsonNode rootNode = mapper.readTree(k);//获取整个json字符串的根节点
        JsonNode dataNode = rootNode.path("service").path("data");
        
        display_str_array[0] = dataNode.path("DiZhiMa").asText();
        display_str_array[1] = dataNode.path("QiTiLeiXing").asText();
        display_str_array[2] = dataNode.path("DanWei").asText();
        display_str_array[3] = dataNode.path("TangCeQiZhuangTai").asText();
        display_str_array[4] = dataNode.path("QiTiNongDu").asText();
        display_str_array[5] = dataNode.path("DiXian").asText();
        display_str_array[6] = dataNode.path("GaoXian").asText();
        display_str_array[7] = dataNode.path("DianLiang").asText();
        display_str_array[8] = dataNode.path("WenDuZhengShu").asText();
        display_str_array[9] = dataNode.path("WenDuXiaoShu").asText();
        //System.out.println("\r\np001: " + dataNode.path("p001").asText());
    } catch (Exception e) {  
    	System.out.println(e.toString());
        e.printStackTrace();  
    }
    
    //将获取到的数据输入到table1数据库中
    try {
    	// 注册 JDBC 驱动
    	Class.forName("com.mysql.jdbc.Driver");
    	// 打开链接
    	conn = DriverManager.getConnection(DB_URL,USER,PASS);
    	// 执行插入
    	stmt = conn.createStatement();
    	String sql = "insert into table1_shijian_jiedian (id, gas, DanWei, status, NongDu, DiXian, GaoXian, DianLiang, WenDu, Date) values (" + display_str_array[0] + ", " + display_str_array[1] + ", " + display_str_array[2] +", " + display_str_array[3] +", " + display_str_array[4] +", " + display_str_array[5] +", " + display_str_array[6] +", " + display_str_array[7] +", " + display_str_array[8] +", now());";
    	stmt.execute(sql);
    	
    	stmt.close();
        conn.close();
    }catch(Exception e){
        // 处理 Class.forName 错误
        e.printStackTrace();
    }finally{
        // 关闭资源
        try{
            if(stmt!=null) stmt.close();
        }catch(SQLException se2){
        }// 什么都不做
        try{
            if(conn!=null) conn.close();
        }catch(SQLException se){
            se.printStackTrace();
        }
    }
    
  //将获取到的数据输入到table1_display数据库中
//    try {
//    	// 注册 JDBC 驱动
//    	Class.forName("com.mysql.jdbc.Driver");
//    	// 打开链接
//    	conn = DriverManager.getConnection(DB_URL,USER,PASS);
//    	// 执行插入
//    	stmt = conn.createStatement();
//    	//对指定节点的数据进行更新
//    	String sql = "update table1_display set p002 = " + display_str_array[1] + ", p003 = " + display_str_array[2] + ", p004 = " + display_str_array[3] + " where p001 = " + display_str_array[0] + ";";
//    	stmt.execute(sql);
//    	
//    	stmt.close();
//        conn.close();
//    }catch(Exception e){
//        // 处理 Class.forName 错误
//        e.printStackTrace();
//    }finally{
//        // 关闭资源
//        try{
//            if(stmt!=null) stmt.close();
//        }catch(SQLException se2){
//        }// 什么都不做
//        try{
//            if(conn!=null) conn.close();
//        }catch(SQLException se){
//            se.printStackTrace();
//        }
//    }
//    
    
    
    
    
    //System.out.println("\r\nlungezuishuai\r\n" + k + "end\r\n");//调试的时候会用到
    return result;
  }
}

DeviceDataChanged.java

package com.huawei.com;

import java.io.Serializable;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="DeviceDataChanged")
public class DeviceDataChanged
  implements Serializable
{
  private static final long serialVersionUID = -6005926332718108639L;
  private String notifyType;
  private String requestId;
  private String deviceId;
  private String gatewayId;
  
  public String getNotifyType()
  {
    return this.notifyType;
  }
  
  public void setNotifyType(String notifyType)
  {
    this.notifyType = notifyType;
  }
  
  public String getRequestId()
  {
    return this.requestId;
  }
  
  public void setRequestId(String requestId)
  {
    this.requestId = requestId;
  }
  
  public String getDeviceId()
  {
    return this.deviceId;
  }
  
  public void setDeviceId(String deviceId)
  {
    this.deviceId = deviceId;
  }
  
  public String getGatewayId()
  {
    return this.gatewayId;
  }
  
  public void setGatewayId(String gatewayId)
  {
    this.gatewayId = gatewayId;
  }
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>mvn</groupId>
  <artifactId>SSM</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>SSM Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
  	
  	<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
	<dependency>
	    <groupId>mysql</groupId>
	    <artifactId>mysql-connector-java</artifactId>
	    <version>8.0.11</version>
	</dependency>
  	
  	
	<dependency> 
	    <groupId>javax.ws.rs</groupId> 
	    <artifactId>jsr311-api</artifactId> 
	    <version>1.1.1</version> 
	</dependency>
	
	<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
	<dependency>
	    <groupId>com.fasterxml.jackson.core</groupId>
	    <artifactId>jackson-databind</artifactId>
	    <version>2.9.7</version>
	</dependency>
	
	<dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.9</version>
    </dependency>
	<dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.9</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.9</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-multipart</artifactId>
        <version>1.9</version>
    </dependency>
    
    
    
  </dependencies>
  <build>
    <finalName>SSM</finalName>
	<plugins>
 
		<!-- java编译插件 -->
            <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-compiler-plugin</artifactId>
               <configuration>
                   <source>1.8</source>
                   <target>1.8</target>
                   <encoding>UTF-8</encoding>
               </configuration>
           </plugin>
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-jar-plugin</artifactId>
               <configuration>
                   <archive>
                       <manifest>
                           <addClasspath>true</addClasspath>
                           <classpathPrefix>lib/</classpathPrefix>
                           <mainClass>com.lunge.wangluo.server_test</mainClass>
                       </manifest>
                   </archive>
               </configuration>
           </plugin>
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-dependency-plugin</artifactId>
               <executions>
                   <execution>
                       <id>copy</id>
                       <phase>install</phase>
                       <goals>
                           <goal>copy-dependencies</goal>
                       </goals>
                       <configuration>
                           <outputDirectory>${project.build.directory}/lib</outputDirectory>
                       </configuration>
                   </execution>
               </executions>
           </plugin>
	</plugins>
    
  </build>
</project>

编译步骤
1、右键工程------>run as------->maven clean
2、右键工程------>run as------->maven install

注意

1、由于我的阿里云上运行的是8.0的mysql,所以使用的mysql的java库应该是8.0.11
2、这些代码只是测试代码,里面很多功能都不全,但胜在简洁

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值