Example usage for javax.json.stream JsonParser hasNext

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

Introduction

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

Prototype

boolean hasNext();

Source Link

Document

Returns true if there are more parsing states.

Usage

From source file:com.oracle.products.ProductResource.java

@PUT
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)/* ww  w  .  ja  v  a 2s  . c om*/
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 (conn == null) {
        return "Not connected";
    } else {
        String query = "UPDATE product SET  name = ?, description = ?, quantity = ? WHERE product_id =?";
        PreparedStatement pstmt = conn.prepareStatement(query);
        pstmt.setString(1, productMap.get("name"));
        pstmt.setString(2, productMap.get("description"));
        pstmt.setInt(3, Integer.parseInt(productMap.get("quantity")));
        pstmt.setInt(4, id);
        pstmt.executeUpdate();
        return "row has been updated into the database";
    }

}

From source file:com.Assignment4.Assign.java

@POST
@Consumes(MediaType.APPLICATION_JSON)//from   w  w w .  j  a v a2s.co  m
@Produces(MediaType.TEXT_PLAIN)
public String postProduct(String str) throws SQLException {
    JsonParser parser = Json.createParser(new StringReader(str));
    Map<String, String> proMap = 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();
            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 = "INSERT INTO product (product_id,name,description,quantity) VALUES (?,?,?,?)";
        PreparedStatement pst = connected.prepareStatement(query);
        pst.setInt(1, Integer.parseInt(proMap.get("product_id")));
        pst.setString(2, proMap.get("name"));
        pst.setString(3, proMap.get("description"));
        pst.setInt(4, Integer.parseInt(proMap.get("quantity")));
        pst.executeUpdate();
        return "row has been inserted into the database";
    }

}

From source file:com.product.Product.java

@PUT
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)/*from w  ww .  j av  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> prodMap = 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();
            prodMap.put(key, val);
            break;
        case VALUE_NUMBER:
            val = parser.getString();
            prodMap.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 prepstmnt = connect.prepareStatement(query);
        prepstmnt.setString(1, prodMap.get("name"));
        prepstmnt.setString(2, prodMap.get("description"));
        prepstmnt.setInt(3, Integer.parseInt(prodMap.get("quantity")));
        prepstmnt.setInt(4, id);
        prepstmnt.executeUpdate();
        return "row has been updated into the database";
    }

}

From source file:com.product.Product.java

@POST
@Path("/products")
@Consumes(MediaType.APPLICATION_JSON)/*from w  w  w. j a  v  a 2  s  .c o m*/
@Produces(MediaType.TEXT_PLAIN)
public String postProduct(String str) throws SQLException {
    JsonParser parser = Json.createParser(new StringReader(str));
    Map<String, String> prodMap = 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();
            prodMap.put(key, val);
            break;
        case VALUE_NUMBER:
            val = parser.getString();
            prodMap.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 prepstmnt = connect.prepareStatement(query);
        prepstmnt.setInt(1, Integer.parseInt(prodMap.get("product_id")));
        prepstmnt.setString(2, prodMap.get("name"));
        prepstmnt.setString(3, prodMap.get("description"));
        prepstmnt.setInt(4, Integer.parseInt(prodMap.get("quantity")));
        prepstmnt.executeUpdate();
        return "row has been inserted into the database";
    }

}

From source file:com.products.ProductResource.java

@POST
@Consumes(MediaType.APPLICATION_JSON)//ww w . j  a v  a 2  s.  c om
@Produces(MediaType.TEXT_PLAIN)
public String postProduct(String str) throws SQLException {
    JsonParser parser = Json.createParser(new StringReader(str));
    Map<String, String> productMap = new LinkedHashMap<String, String>();
    String key = "";
    String value = "";

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

}

From source file:com.oracle.products.ProductResource.java

@POST
@Consumes(MediaType.APPLICATION_JSON)/*from  w w w  .  j a  v  a2 s. c  o  m*/
@Produces(MediaType.TEXT_PLAIN)
public String postProduct(String str) throws SQLException {
    JsonParser parser = Json.createParser(new StringReader(str));
    Map<String, String> productMap = 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();
            productMap.put(key, val);
            break;
        case VALUE_NUMBER:
            val = parser.getString();
            productMap.put(key, val);
            break;
        default:
            break;
        }
    }
    if (conn == null) {
        return "Not connected";
    } else {
        String query = "INSERT INTO product (product_id,name,description,quantity) VALUES (?,?,?,?)";
        PreparedStatement pstmt = conn.prepareStatement(query);
        pstmt.setInt(1, Integer.parseInt(productMap.get("product_id")));
        pstmt.setString(2, productMap.get("name"));
        pstmt.setString(3, productMap.get("description"));
        pstmt.setInt(4, Integer.parseInt(productMap.get("quantity")));
        pstmt.executeUpdate();
        return "row has been inserted into the database";
    }

}

From source file:com.assignment4.productdetails.java

@PUT
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)/*from  w w w .  ja  va 2  s  .c o  m*/
public String putProduct(@PathParam("id") int id, String str) throws SQLException {
    JsonParser parse = Json.createParser(new StringReader(str));
    Map<String, String> pdtmap = new LinkedHashMap<String, String>();
    String key = "";
    String value = "";

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

}

From source file:com.assignment4.productdetails.java

@POST
@Path("/products")
@Consumes(MediaType.APPLICATION_JSON)//from   w  w  w.  ja  v a 2s.  co  m
@Produces(MediaType.TEXT_PLAIN)
public String postProduct(String string) throws SQLException {
    JsonParser parse = Json.createParser(new StringReader(string));
    Map<String, String> pdtmap = new LinkedHashMap<String, String>();
    String key = "";
    String value = "";

    while (parse.hasNext()) {
        JsonParser.Event event = parse.next();
        switch (event) {
        case KEY_NAME:
            key = parse.getString();
            break;
        case VALUE_STRING:
            value = parse.getString();
            pdtmap.put(key, value);
            break;
        case VALUE_NUMBER:
            value = parse.getString();
            pdtmap.put(key, value);
            break;
        default:
            break;
        }
    }
    if (conn == null) {
        return "Not able to  connect";
    } else {
        String query = "INSERT INTO products (product_id,name,decription,quantity) VALUES (?,?,?)";
        PreparedStatement prestmt = conn.prepareStatement(query);
        prestmt.setInt(1, Integer.parseInt(pdtmap.get("product_id")));
        prestmt.setString(2, pdtmap.get("name"));
        prestmt.setString(3, pdtmap.get("description"));
        prestmt.setInt(4, Integer.parseInt(pdtmap.get("quantity")));
        prestmt.executeUpdate();
        return " the row has been inserted into the database";
    }

}