Example usage for javax.json.stream JsonParser getString

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

Introduction

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

Prototype

String getString();

Source Link

Document

Returns a String for the name in a name/value pair, for a string value or a number value.

Usage

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();
            break;
        case VALUE_STRING:

            value = parser.getString();//from   w  ww  .  j  a  va  2 s . co  m
            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();
            break;
        case VALUE_STRING:
            value = parser.getString();//from   www. j  a  v a2 s .com
            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);/*from   www .j a v a2 s  .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 a v  a 2s  .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("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)/*www.  java 2s.  c  o 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.products.Pro_details.java

@PUT
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)/*from ww  w  .  ja va 2 s  .  co m*/
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)//from  ww w.  j a v a  2s .  c o m
@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";
    }

}

From source file:com.Assignment4.Assign.java

@PUT
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)/*from   www  .  j a  v a  2s . 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.products.ProductResource.java

@PUT
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)//from   w  w w.j a  va  2  s.co m
public String putProduct(@PathParam("id") int id, String str) throws SQLException {
    JsonParser parser = Json.createParser(new StringReader(str));
    Map<String, String> productMap = 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();
            productMap.put(key, val);
            break;
        case VALUE_NUMBER:
            val = parser.getString();
            productMap.put(key, val);
            break;
        default:
            break;
        }
    }
    if (co == null) {
        return "Not connected";
    } else {
        String query = "UPDATE product SET  name = ?, description = ?, quantity = ? WHERE product_id =?";
        PreparedStatement stmt = co.prepareStatement(query);
        stmt.setString(1, productMap.get("name"));
        stmt.setString(2, productMap.get("description"));
        stmt.setInt(3, Integer.parseInt(productMap.get("quantity")));
        stmt.setInt(4, id);
        stmt.executeUpdate();
        return "row has been updated into the database";
    }

}

From source file:com.Assignment4.Prod.java

@POST
@Consumes(MediaType.APPLICATION_JSON)/*w  w  w . java 2s .  c o  m*/
@Produces(MediaType.TEXT_PLAIN)
public String postProduct(String str) throws SQLException {
    JsonParser parser = Json.createParser(new StringReader(str));
    Map<String, String> pMap = new LinkedHashMap<String, String>();
    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 = "INSERT INTO product (product_id,name,description,quantity) VALUES (?,?,?,?)";
        PreparedStatement prepstmt = connect.prepareStatement(query);
        prepstmt.setInt(1, Integer.parseInt(pMap.get("product_id")));
        prepstmt.setString(2, pMap.get("name"));
        prepstmt.setString(3, pMap.get("description"));
        prepstmt.setInt(4, Integer.parseInt(pMap.get("quantity")));
        prepstmt.executeUpdate();
        return "row has been inserted into the database";
    }

}