Advertisement
Guest User

Untitled

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