Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. package com.cs330;
  2.  
  3. import javax.ws.rs.GET;
  4. import javax.ws.rs.Path;
  5. import javax.ws.rs.Produces;
  6. import javax.ws.rs.PathParam;
  7. import javax.ws.rs.QueryParam;
  8.  
  9. import javax.ws.rs.Consumes;
  10. import javax.ws.rs.POST;
  11. import javax.ws.rs.core.MultivaluedMap;
  12. import javax.ws.rs.core.MediaType;
  13. import javax.ws.rs.core.Response;
  14. import javax.ws.rs.core.Response.ResponseBuilder;
  15.  
  16. import java.sql.Connection;
  17. import java.sql.DriverManager;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.sql.Statement;
  21. import java.sql.PreparedStatement;
  22.  
  23. import java.util.Arrays;
  24. import com.google.gson.Gson;
  25.  
  26. @Path("ws2")
  27. public class IngredientServices{
  28.  
  29. @Path("/ingredients")
  30. @POST
  31. @Produces("text/plain")
  32. @Consumes("application/x-www-form-urlencoded")
  33.  
  34. public String createIngredient(MultivaluedMap<String,String> formFields) throws SQLException, ClassNotFoundException {
  35.  
  36. System.out.println("In create Ingredient");
  37.  
  38. String newName = formFields.getFirst("name");
  39.  
  40. String newCategory = formFields.getFirst("category");
  41.  
  42. String connectStr="jdbc:mysql://localhost:3306/fooddb";
  43. String username="root";
  44. String password="csci330pass";
  45. String driver="com.mysql.jdbc.Driver";
  46. Class.forName(driver);
  47. Connection con = DriverManager.getConnection(connectStr, username, password);
  48.  
  49. PreparedStatement createStmt = con.prepareStatement("INSERT INTO ingredient (name, category) VALUES (?,?)");
  50. createStmt.setString(1,newName);
  51. createStmt.setString(2,newCategory);
  52.  
  53. int res = createStmt.executeUpdate();
  54.  
  55. System.out.println("Result is: "+res);
  56.  
  57. if(res==1){
  58. PreparedStatement retrieveStmt = con.prepareStatement("Select * from ingredient WHERE name=? and category=?");
  59. retrieveStmt.setString(1, newName);
  60. retrieveStmt.setString(2, newCategory);
  61. ResultSet rs = retrieveStmt.executeQuery();
  62.  
  63. String result = "";
  64. int count = 0;
  65. int MAX=100;
  66. Ingredients[] ingArray = new Ingredients[MAX];
  67.  
  68. while(rs.next()) {
  69.  
  70. int theId = rs.getInt("id");
  71. String theName = rs.getString("name");
  72. String theCategory = rs.getString("category");
  73.  
  74. Ingredients ing = new Ingredients(theId, theName, theCategory);
  75. System.out.println(ing);
  76. ingArray[count] = ing;
  77. count++;
  78. }// end while
  79.  
  80. ingArray = Arrays.copyOf(ingArray,count);
  81.  
  82. Gson theGsonObj = new Gson();
  83.  
  84. result= theGsonObj.toJson(ingArray);
  85. System.out.println("the json: \n"+result);
  86. return result;
  87.  
  88. /* for later
  89. ResponseBuilder rb = Response.ok(result, MediaType.TEXT_PLAIN);
  90. rb.status(201);
  91. return rb.build();
  92. */
  93.  
  94. }// end if
  95.  
  96. else { //ingredient not inserted
  97.  
  98. Gson theGsonObj = new Gson();
  99. Ingredients[] blankIngArray = new Ingredients[1];
  100. blankIngArray[0] = new Ingredients(0,"none","none");
  101. String blankResult= theGsonObj.toJson(blankIngArray);
  102. return blankResult;
  103.  
  104. /* for later
  105. return Response.status(700).build();
  106. */
  107.  
  108. }//end else
  109.  
  110. } // end method
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. @Path("/ingredients")
  120. @GET
  121. @Produces("text/plain")
  122.  
  123. public String getIngredients() throws SQLException, ClassNotFoundException {
  124.  
  125.  
  126.  
  127.  
  128. String connectStr="jdbc:mysql://localhost:3306/fooddb";
  129. String username="root";
  130. String password="csci330pass";
  131. String driver="com.mysql.jdbc.Driver";
  132. Class.forName(driver);
  133. Connection con = DriverManager.getConnection(connectStr, username, password);
  134.  
  135.  
  136.  
  137.  
  138. PreparedStatement ps = con.prepareStatement("SELECT id, name, category FROM ingredient ");
  139.  
  140.  
  141. ResultSet rs = ps.executeQuery();
  142.  
  143. String result = "";
  144.  
  145. Object [] ingredient = new Object [5];
  146.  
  147. Gson aGsonObject = new Gson();
  148.  
  149. int i = 0;
  150.  
  151. while(rs.next()){
  152.  
  153. int theId = rs.getInt("id");
  154. String theName = rs.getString("name");
  155. String theCat = rs.getString("category");
  156. Ingredients ing = new Ingredients(theId, theName, theCat);
  157.  
  158.  
  159. ingredient[i] = ing;
  160. i++;
  161.  
  162. }
  163.  
  164. return aGsonObject.toJson(ingredient);
  165. //return Arrays.toString(ingredient);
  166.  
  167.  
  168. }
  169.  
  170. @Path("/ingredients/{id}")
  171. @GET
  172. @Produces("text/plain")
  173.  
  174. public String getIngredientById(@PathParam("id") String theId) throws SQLException, ClassNotFoundException {
  175. int intId = 0;
  176. String theIng = "";
  177. try {
  178. intId = Integer.parseInt(theId);
  179. }catch(NumberFormatException ne) {
  180. intId = 1;
  181. }
  182.  
  183. String connectStr="jdbc:mysql://localhost:3306/fooddb";
  184. String username="root";
  185. String password="csci330pass";
  186. String driver="com.mysql.jdbc.Driver";
  187. Class.forName(driver);
  188. Connection con = DriverManager.getConnection(connectStr, username, password);
  189.  
  190. PreparedStatement ps1 = con.prepareStatement("SELECT id, name, category FROM ingredient WHERE id=?");
  191.  
  192. ps1.setInt(1, intId);
  193.  
  194. ResultSet rs = ps1.executeQuery();
  195. String result ="";
  196.  
  197. Object [] ingredient1 = new Object [1];
  198.  
  199. Gson aGsonObject1 = new Gson();
  200.  
  201. int i = 0;
  202.  
  203. while(rs.next()){
  204. int theId2 = rs.getInt("id");
  205. String theName = rs.getString("name");
  206. String theCat = rs.getString("category");
  207.  
  208. Ingredients ing1 = new Ingredients(theId2, theName, theCat);
  209.  
  210.  
  211.  
  212. ingredient1[i] = ing1;
  213.  
  214. i++;
  215.  
  216. }
  217.  
  218. return aGsonObject1.toJson(ingredient1);
  219. //return Arrays.toString(ingredient1);
  220.  
  221. }
  222.  
  223. @Path("/ingredients/ingredient")
  224. @GET
  225. @Produces("text/plain")
  226.  
  227.  
  228. public String getIngredientByName(@QueryParam("name") String theName) throws SQLException, ClassNotFoundException {
  229. String theIng = "";
  230.  
  231. String connectStr="jdbc:mysql://localhost:3306/fooddb";
  232. String username="root";
  233. String password="csci330pass";
  234. String driver="com.mysql.jdbc.Driver";
  235. Class.forName(driver);
  236. Connection con = DriverManager.getConnection(connectStr, username, password);
  237.  
  238.  
  239. PreparedStatement ps2 = con.prepareStatement("SELECT id, name, category FROM ingredient WHERE name='" + theName + "'");
  240.  
  241.  
  242.  
  243. ResultSet rs = ps2.executeQuery();
  244.  
  245. Object [] ingredient2 = new Object [1];
  246.  
  247. Gson aGsonObject2 = new Gson();
  248.  
  249. int i = 0;
  250.  
  251. String result ="";
  252.  
  253. while(rs.next()){
  254. int theld = rs.getInt("id");
  255. String theName2 = rs.getString("name");
  256. String theCat = rs.getString("category");
  257.  
  258. Ingredients ing2 = new Ingredients(theld, theName2, theCat);
  259.  
  260.  
  261. ingredient2[i] = ing2;
  262. i++;
  263.  
  264. }
  265.  
  266. return aGsonObject2.toJson(ingredient2);
  267. //return Arrays.toString(ingredient2);
  268. }
  269.  
  270.  
  271.  
  272. }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement