Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.cs330;
- import javax.ws.rs.GET;
- import javax.ws.rs.Path;
- import javax.ws.rs.Produces;
- import javax.ws.rs.PathParam;
- import javax.ws.rs.QueryParam;
- import javax.ws.rs.Consumes;
- import javax.ws.rs.POST;
- import javax.ws.rs.core.MultivaluedMap;
- import javax.ws.rs.core.MediaType;
- import javax.ws.rs.core.Response;
- import javax.ws.rs.core.Response.ResponseBuilder;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.sql.PreparedStatement;
- import java.util.Arrays;
- import com.google.gson.Gson;
- @Path("ws2")
- public class IngredientServices{
- @Path("/ingredients")
- @POST
- @Produces("text/plain")
- @Consumes("application/x-www-form-urlencoded")
- public String createIngredient(MultivaluedMap<String,String> formFields) throws SQLException, ClassNotFoundException {
- System.out.println("In create Ingredient");
- String newName = formFields.getFirst("name");
- String newCategory = formFields.getFirst("category");
- String connectStr="jdbc:mysql://localhost:3306/fooddb";
- String username="root";
- String password="csci330pass";
- String driver="com.mysql.jdbc.Driver";
- Class.forName(driver);
- Connection con = DriverManager.getConnection(connectStr, username, password);
- PreparedStatement createStmt = con.prepareStatement("INSERT INTO ingredient (name, category) VALUES (?,?)");
- createStmt.setString(1,newName);
- createStmt.setString(2,newCategory);
- int res = createStmt.executeUpdate();
- System.out.println("Result is: "+res);
- if(res==1){
- PreparedStatement retrieveStmt = con.prepareStatement("Select * from ingredient WHERE name=? and category=?");
- retrieveStmt.setString(1, newName);
- retrieveStmt.setString(2, newCategory);
- ResultSet rs = retrieveStmt.executeQuery();
- String result = "";
- int count = 0;
- int MAX=100;
- Ingredients[] ingArray = new Ingredients[MAX];
- while(rs.next()) {
- int theId = rs.getInt("id");
- String theName = rs.getString("name");
- String theCategory = rs.getString("category");
- Ingredients ing = new Ingredients(theId, theName, theCategory);
- System.out.println(ing);
- ingArray[count] = ing;
- count++;
- }// end while
- ingArray = Arrays.copyOf(ingArray,count);
- Gson theGsonObj = new Gson();
- result= theGsonObj.toJson(ingArray);
- System.out.println("the json: \n"+result);
- return result;
- /* for later
- ResponseBuilder rb = Response.ok(result, MediaType.TEXT_PLAIN);
- rb.status(201);
- return rb.build();
- */
- }// end if
- else { //ingredient not inserted
- Gson theGsonObj = new Gson();
- Ingredients[] blankIngArray = new Ingredients[1];
- blankIngArray[0] = new Ingredients(0,"none","none");
- String blankResult= theGsonObj.toJson(blankIngArray);
- return blankResult;
- /* for later
- return Response.status(700).build();
- */
- }//end else
- } // end method
- @Path("/ingredients")
- @GET
- @Produces("text/plain")
- public String getIngredients() throws SQLException, ClassNotFoundException {
- String connectStr="jdbc:mysql://localhost:3306/fooddb";
- String username="root";
- String password="csci330pass";
- String driver="com.mysql.jdbc.Driver";
- Class.forName(driver);
- Connection con = DriverManager.getConnection(connectStr, username, password);
- PreparedStatement ps = con.prepareStatement("SELECT id, name, category FROM ingredient ");
- ResultSet rs = ps.executeQuery();
- String result = "";
- Object [] ingredient = new Object [5];
- Gson aGsonObject = new Gson();
- int i = 0;
- while(rs.next()){
- int theId = rs.getInt("id");
- String theName = rs.getString("name");
- String theCat = rs.getString("category");
- Ingredients ing = new Ingredients(theId, theName, theCat);
- ingredient[i] = ing;
- i++;
- }
- return aGsonObject.toJson(ingredient);
- //return Arrays.toString(ingredient);
- }
- @Path("/ingredients/{id}")
- @GET
- @Produces("text/plain")
- public String getIngredientById(@PathParam("id") String theId) throws SQLException, ClassNotFoundException {
- int intId = 0;
- String theIng = "";
- try {
- intId = Integer.parseInt(theId);
- }catch(NumberFormatException ne) {
- intId = 1;
- }
- String connectStr="jdbc:mysql://localhost:3306/fooddb";
- String username="root";
- String password="csci330pass";
- String driver="com.mysql.jdbc.Driver";
- Class.forName(driver);
- Connection con = DriverManager.getConnection(connectStr, username, password);
- PreparedStatement ps1 = con.prepareStatement("SELECT id, name, category FROM ingredient WHERE id=?");
- ps1.setInt(1, intId);
- ResultSet rs = ps1.executeQuery();
- String result ="";
- Object [] ingredient1 = new Object [1];
- Gson aGsonObject1 = new Gson();
- int i = 0;
- while(rs.next()){
- int theId2 = rs.getInt("id");
- String theName = rs.getString("name");
- String theCat = rs.getString("category");
- Ingredients ing1 = new Ingredients(theId2, theName, theCat);
- ingredient1[i] = ing1;
- i++;
- }
- return aGsonObject1.toJson(ingredient1);
- //return Arrays.toString(ingredient1);
- }
- @Path("/ingredients/ingredient")
- @GET
- @Produces("text/plain")
- public String getIngredientByName(@QueryParam("name") String theName) throws SQLException, ClassNotFoundException {
- String theIng = "";
- String connectStr="jdbc:mysql://localhost:3306/fooddb";
- String username="root";
- String password="csci330pass";
- String driver="com.mysql.jdbc.Driver";
- Class.forName(driver);
- Connection con = DriverManager.getConnection(connectStr, username, password);
- PreparedStatement ps2 = con.prepareStatement("SELECT id, name, category FROM ingredient WHERE name='" + theName + "'");
- ResultSet rs = ps2.executeQuery();
- Object [] ingredient2 = new Object [1];
- Gson aGsonObject2 = new Gson();
- int i = 0;
- String result ="";
- while(rs.next()){
- int theld = rs.getInt("id");
- String theName2 = rs.getString("name");
- String theCat = rs.getString("category");
- Ingredients ing2 = new Ingredients(theld, theName2, theCat);
- ingredient2[i] = ing2;
- i++;
- }
- return aGsonObject2.toJson(ingredient2);
- //return Arrays.toString(ingredient2);
- }
- }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement