Example usage for javax.json.stream JsonParser next

List of usage examples for javax.json.stream JsonParser next

Introduction

In this page you can find the example usage for javax.json.stream JsonParser next.

Prototype

Event next();

Source Link

Document

Returns the event for the next parsing state.

Usage

From source file:com.products.products.java

@POST
@Consumes("application/json")
public void doPost(String str) {
    JsonParser parser = Json.createParser(new StringReader(str));
    Map<String, String> mapKeyValue = new HashMap<>();
    String key = "", val;
    while (parser.hasNext()) {
        JsonParser.Event evt = parser.next();
        switch (evt) {
        case KEY_NAME:
            key = parser.getString();/* ww w.  ja va  2  s  .  c o  m*/
            break;
        case VALUE_STRING:
            val = parser.getString();
            mapKeyValue.put(key, val);
            break;
        case VALUE_NUMBER:
            val = Integer.toString(parser.getInt());
            mapKeyValue.put(key, val);
            break;
        }
    }
    System.out.println(mapKeyValue);
    doPostOrPutOrDelete("INSERT INTO product (name, description, quantity) VALUES ( ?, ?, ?)",
            mapKeyValue.get("name"), mapKeyValue.get("description"), mapKeyValue.get("quantity"));
}

From source file:com.products.products.java

@PUT
@Path("{id}")
@Consumes("application/json")
public void doPut(@PathParam("id") String id, String str) {
    JsonParser parser = Json.createParser(new StringReader(str));
    Map<String, String> mapKayValue = new HashMap<>();
    String key = "", val;
    while (parser.hasNext()) {
        JsonParser.Event evt = parser.next();
        switch (evt) {
        case KEY_NAME:
            key = parser.getString();//from ww  w . j ava  2  s .  c o  m
            break;
        case VALUE_STRING:
            val = parser.getString();
            mapKayValue.put(key, val);
            break;
        case VALUE_NUMBER:
            val = parser.getString();
            mapKayValue.put(key, val);
            break;
        }
    }
    System.out.println(mapKayValue);
    doPostOrPutOrDelete("UPDATE PRODUCT SET name = ?, description = ?, quantity = ? WHERE productID = ?",
            mapKayValue.get("name"), mapKayValue.get("description"), mapKayValue.get("quantity"), id);

}

From source file:assignment4.ProductServlet.java

@POST
@Consumes("application/json")
public void doPost(String str) {
    JsonParser parser = Json.createParser(new StringReader(str));
    Map<String, String> map = new HashMap<>();
    String name = "", value;

    while (parser.hasNext()) {
        JsonParser.Event evt = parser.next();
        switch (evt) {
        case KEY_NAME:
            name = parser.getString();//from w w  w .  ja  v  a 2 s  .  c o  m
            break;
        case VALUE_STRING:

            value = parser.getString();
            map.put(name, value);
            break;
        case VALUE_NUMBER:
            value = Integer.toString(parser.getInt());
            map.put(name, value);
            break;
        }
    }
    System.out.println(map);
    String str1 = map.get("name");
    String description = map.get("description");
    String quantity = map.get("quantity");
    doUpdate("insert into productdetails ( name, description, quantity) values ( ?, ?, ?)", str1, description,
            quantity);
}

From source file:assignment4.ProductServlet.java

@PUT
@Path("{id}")
@Consumes("application/json")
public void doPut(@PathParam("id") String id, String str) {
    JsonParser parser = Json.createParser(new StringReader(str));
    Map<String, String> map = new HashMap<>();
    String name = "", value;
    while (parser.hasNext()) {
        JsonParser.Event event = parser.next();
        switch (event) {
        case KEY_NAME:
            name = parser.getString();// ww w  .jav a 2s .  c o  m
            break;
        case VALUE_STRING:
            value = parser.getString();
            map.put(name, value);
            break;
        case VALUE_NUMBER:
            value = Integer.toString(parser.getInt());
            map.put(name, value);
            break;
        }
    }
    System.out.println(map);

    String str1 = map.get("name");
    String description = map.get("description");
    String quantity = map.get("quantity");
    doUpdate(
            "update productdetails set productId = ?, name = ?, description = ?, quantity = ? where productID = ?",
            id, str1, description, quantity, id);

}

From source file:Servlets.Product.java

@POST
@Consumes("application/json")
public void doPost(String strValue) {
    JsonParser jsonParserObj = Json.createParser(new StringReader(strValue));
    Map<String, String> map = new HashMap<>();
    String name = "", value;
    while (jsonParserObj.hasNext()) {
        JsonParser.Event event = jsonParserObj.next();
        switch (event) {
        case KEY_NAME:
            name = jsonParserObj.getString();
            break;
        case VALUE_STRING:
            value = jsonParserObj.getString();
            map.put(name, value);// w ww.  j  av  a2s  .c  om
            break;
        case VALUE_NUMBER:
            value = Integer.toString(jsonParserObj.getInt());
            map.put(name, value);
            break;
        }

    }
    System.out.println(map);
    String getName = map.get("name");
    String getDesc = map.get("description");
    String getQuantity = map.get("quantity");

    doUpdate("INSERT INTO product (name,description,quantity) VALUES (?, ?, ?)", getName, getDesc, getQuantity);

}

From source file:Servlets.Product.java

@PUT
@Path("{id}")
@Consumes("application/json")
public void doPut(@PathParam("id") String id, String strValue) {
    JsonParser jsonParserObj = Json.createParser(new StringReader(strValue));
    Map<String, String> map = new HashMap<>();
    String name = "", value;
    while (jsonParserObj.hasNext()) {
        JsonParser.Event event = jsonParserObj.next();
        switch (event) {
        case KEY_NAME:
            name = jsonParserObj.getString();
            break;
        case VALUE_STRING:
            value = jsonParserObj.getString();
            map.put(name, value);//  w w  w .j av a  2s  . com
            break;
        case VALUE_NUMBER:
            value = Integer.toString(jsonParserObj.getInt());
            map.put(name, value);
            break;
        }

    }
    System.out.println(map);
    String getName = map.get("name");
    String getDesc = map.get("description");
    String getQuantity = map.get("quantity");
    doUpdate("update product set ProductID = ?, name = ?, description = ?, quantity = ? where ProductID = ?",
            id, getName, getDesc, getQuantity, id);
}

From source file:com.Assignment4.Prod.java

@PUT
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)//from www. j  a  va  2s. co m
public String putProduct(@PathParam("id") int id, String str) throws SQLException {
    JsonParser parser = Json.createParser(new StringReader(str));
    Map<String, String> pMap = new LinkedHashMap<>();
    String key = "";
    String val = "";

    while (parser.hasNext()) {
        JsonParser.Event event = parser.next();
        switch (event) {
        case KEY_NAME:
            key = parser.getString();
            break;
        case VALUE_STRING:
            val = parser.getString();
            pMap.put(key, val);
            break;
        case VALUE_NUMBER:
            val = parser.getString();
            pMap.put(key, val);
            break;
        default:
            break;
        }
    }
    if (connect == null) {
        return "Not connected";
    } else {
        String query = "UPDATE product SET  name = ?, description = ?, quantity = ? WHERE product_id =?";
        PreparedStatement prepstmt = connect.prepareStatement(query);
        prepstmt.setString(1, pMap.get("name"));
        prepstmt.setString(2, pMap.get("description"));
        prepstmt.setInt(3, Integer.parseInt(pMap.get("quantity")));
        prepstmt.setInt(4, id);
        prepstmt.executeUpdate();
        return "row has been updated into the database";
    }

}

From source file:com.Assignment4.Assign.java

@PUT
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)/*  www.j a v a  2 s.c  o m*/
public String putProduct(@PathParam("id") int id, String str) throws SQLException {
    JsonParser parser = Json.createParser(new StringReader(str));
    Map<String, String> proMap = new LinkedHashMap<>();
    String key = "";
    String val = "";

    while (parser.hasNext()) {
        JsonParser.Event event = parser.next();
        switch (event) {
        case KEY_NAME:
            key = parser.getString();
            break;
        case VALUE_STRING:
            val = parser.getString();
            proMap.put(key, val);
            break;
        case VALUE_NUMBER:
            val = parser.getString();
            proMap.put(key, val);
            break;
        default:
            break;
        }
    }
    if (connected == null) {
        return "Not connected";
    } else {
        String query = "UPDATE product SET  name = ?, description = ?, quantity = ? WHERE product_id =?";
        PreparedStatement pstmt = connected.prepareStatement(query);
        pstmt.setString(1, proMap.get("name"));
        pstmt.setString(2, proMap.get("description"));
        pstmt.setInt(3, Integer.parseInt(proMap.get("quantity")));
        pstmt.setInt(4, id);
        pstmt.executeUpdate();
        return "row has been updated into the database";
    }

}

From source file:com.assignment4.products.Pro_details.java

@PUT
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)//from   ww w  .  j  a  v  a2 s.com
public String putProduct(@PathParam("id") int id, String str) throws SQLException {
    JsonParser p = Json.createParser(new StringReader(str));
    Map<String, String> pm = new LinkedHashMap<String, String>();
    String key = "";
    String val = "";

    while (p.hasNext()) {
        JsonParser.Event event = p.next();
        switch (event) {
        case KEY_NAME:
            key = p.getString();
            break;
        case VALUE_STRING:
            val = p.getString();
            pm.put(key, val);
            break;
        case VALUE_NUMBER:
            val = p.getString();
            pm.put(key, val);
            break;
        default:
            break;
        }
    }
    if (conn == null) {
        return "Not connected";
    } else {
        String q = "UPDATE product SET  name = ?, description = ?, quantity = ? WHERE product_id =?";
        PreparedStatement pstmt = conn.prepareStatement(q);
        pstmt.setString(1, pm.get("name"));
        pstmt.setString(2, pm.get("description"));
        pstmt.setInt(3, Integer.parseInt(pm.get("quantity")));
        pstmt.setInt(4, id);
        pstmt.executeUpdate();
        return "row has been updated into the database";
    }

}

From source file:com.assignment4.products.Pro_details.java

@POST
@Path("/products")
@Consumes(MediaType.APPLICATION_JSON)//www  . ja va2s  .com
@Produces(MediaType.TEXT_PLAIN)
public String postProduct(String str) throws SQLException {
    JsonParser p = Json.createParser(new StringReader(str));
    Map<String, String> pm = new LinkedHashMap<String, String>();
    String key = "";
    String val = "";

    while (p.hasNext()) {
        JsonParser.Event event = p.next();
        switch (event) {
        case KEY_NAME:
            key = p.getString();
            break;
        case VALUE_STRING:
            val = p.getString();
            pm.put(key, val);
            break;
        case VALUE_NUMBER:
            val = p.getString();
            pm.put(key, val);
            break;
        default:
            break;
        }
    }
    if (conn == null) {
        return "Not connected";
    } else {
        String q = "INSERT INTO product (product_id,name,description,quantity) VALUES (?,?,?,?)";
        PreparedStatement ps = conn.prepareStatement(q);
        ps.setInt(1, Integer.parseInt(pm.get("product_id")));
        ps.setString(2, pm.get("name"));
        ps.setString(3, pm.get("description"));
        ps.setInt(4, Integer.parseInt(pm.get("quantity")));
        ps.executeUpdate();
        return "row has been inserted into the database";
    }

}