Advertisement
Guest User

Untitled

a guest
Nov 27th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package webservices;
  7.  
  8. import inventory.Inventory;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.sql.Statement;
  15. import java.util.ArrayList;
  16. import javax.ws.rs.core.Context;
  17. import javax.ws.rs.core.UriInfo;
  18. import javax.ws.rs.Consumes;
  19. import javax.ws.rs.Produces;
  20. import javax.ws.rs.GET;
  21. import javax.ws.rs.Path;
  22. import javax.ws.rs.PUT;
  23. import javax.ws.rs.QueryParam;
  24. import javax.ws.rs.core.GenericEntity;
  25. import javax.ws.rs.core.MediaType;
  26. import javax.ws.rs.core.Response;
  27.  
  28. /**
  29. * REST Web Service
  30. *
  31. * @author Hy
  32. */
  33. @Path("verifyuser")
  34. public class VerifyUser {
  35.  
  36. @Context
  37. private UriInfo context;
  38.  
  39. /**
  40. * Creates a new instance of VerifyUser
  41. */
  42. public VerifyUser() {
  43. }
  44.  
  45. /**
  46. * Retrieves representation of an instance of webservices.VerifyUser
  47. * @param userid
  48. * @param password
  49. * @return an instance of java.lang.String
  50. */
  51. @GET
  52. @Path("VerifyUser")
  53. @Consumes(MediaType.APPLICATION_JSON)
  54. @Produces(MediaType.APPLICATION_JSON)
  55. public Response VerifyUser(@QueryParam("userid") String userid,
  56. @QueryParam("password") String password) {
  57. //TODO return proper representation object
  58. //throw new UnsupportedOperationException();
  59. try{
  60. Class.forName("com.mysql.jdbc.Driver");
  61.  
  62. String connURL ="jdbc:mysql://localhost/onlineShop?user=root&password=12345";
  63.  
  64. Connection conn = DriverManager.getConnection(connURL);
  65.  
  66. Statement stmt = conn.createStatement();
  67. String sqlStr = "SELECT * FROM login WHERE userid=? AND password=?";
  68. PreparedStatement pstmt = conn.prepareStatement(sqlStr);
  69. pstmt.setString(1, userid);
  70. pstmt.setString(2, password);
  71.  
  72. ResultSet rs = pstmt.executeQuery();
  73. boolean result = rs.next();
  74.  
  75. conn.close();
  76. rs.close();
  77. return Response
  78. .status(200)
  79. .entity("" + result)
  80. .build();
  81. } catch (ClassNotFoundException | SQLException e) {
  82. System.out.println("Error" + e.toString());
  83. return null;
  84. }
  85. }
  86.  
  87. @GET
  88. @Path("searchInventory")
  89. @Consumes(MediaType.APPLICATION_JSON)
  90. @Produces(MediaType.APPLICATION_JSON)
  91. public Response searchInventory(@QueryParam("searchString") String searchTxt) {
  92. try {
  93. // Step1: Load JDBC Driver
  94. Class.forName("com.mysql.jdbc.Driver");
  95.  
  96. // Step 2: Define Connection URL
  97. String connURL ="jdbc:mysql://localhost/onlineShop?user=root&password=12345";
  98.  
  99. // Step 3: Establish connection to URL
  100. Connection conn = DriverManager.getConnection(connURL);
  101. // Step 4: Create Statement object
  102. //Statement stmt = conn.createStatement();
  103. // Step 5: Execute SQL Command
  104. String sqlStr = "SELECT * FROM inventory WHERE functions LIKE ? order by brand, model";
  105. PreparedStatement pstmt = conn.prepareCall(sqlStr);
  106. pstmt.setString(1, "%"+searchTxt+"%");
  107.  
  108. ResultSet rs = pstmt.executeQuery();
  109.  
  110. System.out.println(sqlStr);
  111. ArrayList<Inventory> a1 = new ArrayList<Inventory>();
  112. while (rs.next()) {
  113. int id = rs.getInt("id");
  114. String brand = rs.getString("brand");
  115. String model = rs.getString("model");
  116. String functions = rs.getString("functions");
  117. int quantity = rs.getInt("quantity");
  118. Inventory inv = new Inventory(id, brand, model, functions, quantity);
  119. a1.add(inv);
  120. }
  121. conn.close();
  122. rs.close();
  123. System.out.println(a1.size());
  124. GenericEntity<ArrayList<Inventory>> entity = new GenericEntity<ArrayList<Inventory>>(a1){
  125. };
  126. return Response
  127. .status(200)
  128. .entity(entity)
  129. .build();
  130. // Step 7: Close connection
  131.  
  132. } catch (ClassNotFoundException | SQLException e) {
  133. System.out.println("error encountered ... " + e.toString());
  134. return null;
  135. }
  136. }
  137.  
  138. /**
  139. * PUT method for updating or creating an instance of VerifyUser
  140. * @param content representation for the resource
  141. */
  142. @PUT
  143. @Consumes(MediaType.APPLICATION_JSON)
  144. public void putJson(String content) {
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement