Advertisement
rafamod

With ApiRequest modelSearch

Aug 19th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. /**
  2. @author Rafamod
  3.  
  4. */
  5.  
  6. package com.tecnology.ddsolutions;
  7.  
  8. //Import JAX-WS Web Services lib.
  9. import javax.ws.rs.GET;
  10. import javax.ws.rs.POST;
  11. import javax.ws.rs.Consumes;
  12. import javax.ws.rs.Path;
  13. import javax.ws.rs.Produces;
  14. import javax.ws.rs.core.Context;
  15. import javax.ws.rs.core.Response;
  16. import javax.servlet.http.HttpServletRequest;
  17.  
  18. //Import JSON Utilities.
  19. import com.google.gson.Gson;
  20. import com.google.gson.GsonBuilder;
  21. import org.json.JSONObject;
  22.  
  23. //Import Other clases.
  24. import com.tecnology.ddsolutions.Models;
  25.  
  26. //Import Java Utils.
  27. import java.util.ArrayList;
  28.  
  29. //Import DB.
  30. import java.sql.*;
  31.  
  32.  
  33. @Path("/Articulos")
  34. public class Articulos {
  35.     @GET
  36.     @POST
  37.     @Produces("application/json")
  38.     @Consumes("application/json")
  39.     public Response articulos(@Context HttpServletRequest request, Models.ApiRequest.Articulos model) throws Exception{
  40.        
  41.         //Create Json Object.
  42.         JSONObject jsonObject = new JSONObject();
  43.         Connection conn = null;
  44.        
  45.         //If theres an error, catch it on the Exception.
  46.         try{
  47.            
  48.             //Get the drivers for the connection DB. Then the URL to connect.
  49.             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  50.             String url = "jdbc:sqlserver://localhost:1433;"+"databaseName=TGoma;user=sa;password=raddil12";
  51.             conn = DriverManager.getConnection(url);
  52.             Statement stmt = conn.createStatement();
  53.             ResultSet rs;
  54.            
  55.             //SQL Query. This is where the code is to get into the DB.
  56.             String SQL = "USE TGoma SELECT art_name, art_stock FROM Articulos WHERE art_name LIKE '"+model.search +"'";
  57.             rs = stmt.executeQuery(SQL);
  58.            
  59.             //Arrays of Items.
  60.             ArrayList<Models.Db.Articulo> articulos = new ArrayList<Models.Db.Articulo>();
  61.            
  62.             while (rs.next()) {
  63.                Models.Db.Articulo articulo = new Models.Db.Articulo();
  64.                articulo.name = rs.getString("art_name");
  65.                articulo.stock = rs.getString("art_stock");
  66.                articulos.add(articulo);
  67.             }
  68.             Gson gson = new GsonBuilder().create();
  69.             return Response.status(200).entity(gson.toJson(articulos)).build();
  70.         }
  71.        
  72.         catch (Exception e){
  73.            
  74.             //Pass the error.
  75.             jsonObject.put("Got an Exception!",e);
  76.             String result = jsonObject +"\n\n";
  77.             return Response.status(422).entity(result).build();
  78.         }
  79.        
  80.         //Close connection DB.
  81.         finally{
  82.             if(conn!=null)
  83.                 conn.close();
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement