Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package webservices;
- import inventory.Inventory;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import javax.ws.rs.core.Context;
- import javax.ws.rs.core.UriInfo;
- import javax.ws.rs.Consumes;
- import javax.ws.rs.Produces;
- import javax.ws.rs.GET;
- import javax.ws.rs.Path;
- import javax.ws.rs.PUT;
- import javax.ws.rs.QueryParam;
- import javax.ws.rs.core.GenericEntity;
- import javax.ws.rs.core.MediaType;
- import javax.ws.rs.core.Response;
- /**
- * REST Web Service
- *
- * @author Hy
- */
- @Path("verifyuser")
- public class VerifyUser {
- @Context
- private UriInfo context;
- /**
- * Creates a new instance of VerifyUser
- */
- public VerifyUser() {
- }
- /**
- * Retrieves representation of an instance of webservices.VerifyUser
- * @param userid
- * @param password
- * @return an instance of java.lang.String
- */
- @GET
- @Path("VerifyUser")
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces(MediaType.APPLICATION_JSON)
- public Response VerifyUser(@QueryParam("userid") String userid,
- @QueryParam("password") String password) {
- //TODO return proper representation object
- //throw new UnsupportedOperationException();
- try{
- Class.forName("com.mysql.jdbc.Driver");
- String connURL ="jdbc:mysql://localhost/onlineShop?user=root&password=12345";
- Connection conn = DriverManager.getConnection(connURL);
- Statement stmt = conn.createStatement();
- String sqlStr = "SELECT * FROM login WHERE userid=? AND password=?";
- PreparedStatement pstmt = conn.prepareStatement(sqlStr);
- pstmt.setString(1, userid);
- pstmt.setString(2, password);
- ResultSet rs = pstmt.executeQuery();
- boolean result = rs.next();
- conn.close();
- rs.close();
- return Response
- .status(200)
- .entity("" + result)
- .build();
- } catch (ClassNotFoundException | SQLException e) {
- System.out.println("Error" + e.toString());
- return null;
- }
- }
- @GET
- @Path("searchInventory")
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces(MediaType.APPLICATION_JSON)
- public Response searchInventory(@QueryParam("searchString") String searchTxt) {
- try {
- // Step1: Load JDBC Driver
- Class.forName("com.mysql.jdbc.Driver");
- // Step 2: Define Connection URL
- String connURL ="jdbc:mysql://localhost/onlineShop?user=root&password=12345";
- // Step 3: Establish connection to URL
- Connection conn = DriverManager.getConnection(connURL);
- // Step 4: Create Statement object
- //Statement stmt = conn.createStatement();
- // Step 5: Execute SQL Command
- String sqlStr = "SELECT * FROM inventory WHERE functions LIKE ? order by brand, model";
- PreparedStatement pstmt = conn.prepareCall(sqlStr);
- pstmt.setString(1, "%"+searchTxt+"%");
- ResultSet rs = pstmt.executeQuery();
- System.out.println(sqlStr);
- ArrayList<Inventory> a1 = new ArrayList<Inventory>();
- while (rs.next()) {
- int id = rs.getInt("id");
- String brand = rs.getString("brand");
- String model = rs.getString("model");
- String functions = rs.getString("functions");
- int quantity = rs.getInt("quantity");
- Inventory inv = new Inventory(id, brand, model, functions, quantity);
- a1.add(inv);
- }
- conn.close();
- rs.close();
- System.out.println(a1.size());
- GenericEntity<ArrayList<Inventory>> entity = new GenericEntity<ArrayList<Inventory>>(a1){
- };
- return Response
- .status(200)
- .entity(entity)
- .build();
- // Step 7: Close connection
- } catch (ClassNotFoundException | SQLException e) {
- System.out.println("error encountered ... " + e.toString());
- return null;
- }
- }
- /**
- * PUT method for updating or creating an instance of VerifyUser
- * @param content representation for the resource
- */
- @PUT
- @Consumes(MediaType.APPLICATION_JSON)
- public void putJson(String content) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement