Advertisement
Guest User

Untitled

a guest
Sep 13th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. public String getIngredients() throws SQLException, ClassNotFoundException {
  2.  
  3.  
  4.  
  5.  
  6. String connectStr="jdbc:mysql://localhost:3306/fooddb";
  7. String username="root";
  8. String password="csci330pass";
  9. String driver="com.mysql.jdbc.Driver";
  10. Class.forName(driver);
  11. Connection con = DriverManager.getConnection(connectStr, username, password);
  12.  
  13.  
  14.  
  15.  
  16. PreparedStatement ps = con.prepareStatement("SELECT id, name, category FROM ingredient ");
  17.  
  18.  
  19. ResultSet rs = ps.executeQuery();
  20.  
  21. String result = "";
  22.  
  23. while(rs.next()){
  24.  
  25. int theId = rs.getInt("id");
  26. String theName = rs.getString("name");
  27. String theCat = rs.getString("category");
  28. result += "id: "+theId+",name:"+theName+"("+theCat+")"+"\n";
  29.  
  30. }
  31.  
  32.  
  33. return result;
  34.  
  35.  
  36. }
  37.  
  38. @Path("/ingredients/{id}")
  39. @GET
  40. @Produces("text/plain")
  41.  
  42. public String getIngredientById(@PathParam("id") String theId) throws SQLException, ClassNotFoundException {
  43. int intId = 0;
  44. String theIng = "";
  45. try {
  46. intId = Integer.parseInt(theId);
  47. }catch(NumberFormatException ne) {
  48. intId = 1;
  49. }
  50.  
  51. String connectStr="jdbc:mysql://localhost:3306/fooddb";
  52. String username="root";
  53. String password="csci330pass";
  54. String driver="com.mysql.jdbc.Driver";
  55. Class.forName(driver);
  56. Connection con = DriverManager.getConnection(connectStr, username, password);
  57.  
  58. PreparedStatement ps1 = con.prepareStatement("SELECT id, name, category FROM ingredient WHERE id=?");
  59.  
  60. ps1.setInt(1, intId);
  61.  
  62. ResultSet rs = ps1.executeQuery();
  63. String result ="";
  64.  
  65. while(rs.next()){
  66. int theId2 = rs.getInt("id");
  67. String theName = rs.getString("name");
  68. String theCategory = rs.getString("category");
  69. result += "id: "+theId2+",name:"+theName+"("+theCategory+")"+"\n";
  70.  
  71. }
  72.  
  73.  
  74. return result;
  75.  
  76. }
  77.  
  78.  
  79. public String getIngredientByName(@QueryParam("name") String theName) throws SQLException, ClassNotFoundException {
  80. String theIng = "";
  81.  
  82. String connectStr="jdbc:mysql://localhost:3306/fooddb";
  83. String username="root";
  84. String password="csci330pass";
  85. String driver="com.mysql.jdbc.Driver";
  86. Class.forName(driver);
  87. Connection con = DriverManager.getConnection(connectStr, username, password);
  88.  
  89.  
  90. PreparedStatement ps2 = con.prepareStatement("SELECT id, name, category FROM ingredient WHERE name=?");
  91.  
  92.  
  93. ps2.setString(1, theName);
  94. ResultSet rs = ps2.executeQuery();
  95.  
  96. String result ="";
  97.  
  98. while(rs.next()){
  99. int theld = rs.getInt("id");
  100. String theName2 = rs.getString("name");
  101. String theCategory = rs.getString("category");
  102. result += "id: "+theld+",name:"+theName2+"("+theCategory +")"+"\n";
  103.  
  104. }
  105.  
  106.  
  107. return result;
  108. }
  109.  
  110.  
  111.  
  112. }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement