Advertisement
Guest User

ProfessorsResources.java

a guest
Jun 16th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.67 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 org.me.service;
  7.  
  8. import javax.ws.rs.core.Context;
  9. import javax.ws.rs.core.UriInfo;
  10. import javax.ws.rs.PathParam;
  11. import javax.ws.rs.POST;
  12. import javax.ws.rs.Consumes;
  13. import javax.ws.rs.Produces;
  14. import javax.ws.rs.GET;
  15. import javax.ws.rs.Path;
  16. import javax.ws.rs.core.MediaType;
  17. import javax.ws.rs.core.Response;
  18. import java.sql.*;
  19. import java.util.logging.Logger;
  20.  
  21. /**
  22.  * REST Web Service
  23.  *
  24.  * @author biar
  25.  */
  26. @Path("/ps")
  27. public class ProfessorsResource {
  28.     private final static Logger LOGGER = Logger.getLogger(ProfessorsResource.class.getName());
  29.     public static int currentKey = 3;
  30.     @Context
  31.     private UriInfo context;
  32.  
  33.     /**
  34.      * Creates a new instance of ProfessorsResource
  35.      */
  36.     public ProfessorsResource() {
  37.     }
  38.  
  39.     /**
  40.      * Retrieves representation of an instance of org.me.service.ProfessorsResource
  41.      * @return an instance of java.lang.String
  42.      */
  43.     @GET
  44.     @Produces(MediaType.TEXT_PLAIN)
  45.     public String getText() {
  46.         Connection c = null;
  47.         Statement stmt = null;
  48.         String result="";
  49.         String id="";
  50.         String name="";
  51.         String surname="";
  52.         try {
  53.              Class.forName("org.sqlite.JDBC");
  54.              c = DriverManager.getConnection("jdbc:sqlite:/home/biar/RESTWS.db");
  55.              c.setAutoCommit(false);
  56.              LOGGER.info("Opened database successfully");
  57.  
  58.             stmt = c.createStatement();
  59.             ResultSet rs = stmt.executeQuery( "SELECT * FROM professor;" );
  60.            
  61.             while ( rs.next() ) {
  62.               id=rs.getString("id");
  63.               name=rs.getString("name");
  64.               surname= rs.getString("surname");
  65.               result+= id+ " : "+name+" "+surname+"\n";
  66.             }
  67.             rs.close();
  68.             stmt.close();
  69.             c.close();
  70.         } catch ( Exception e ) {
  71.       System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  72.       System.exit(0);
  73.      }
  74.         LOGGER.info("Operation done successfully");
  75.         return result;
  76.    
  77.   }
  78.    
  79.  
  80.     /**
  81.      * POST method for creating an instance of ProfessorResource
  82.      * @param content representation for the new resource
  83.      * @return an HTTP response with content of the created resource
  84.      */
  85.     @POST
  86.     @Consumes(MediaType.TEXT_PLAIN)
  87.     @Produces(MediaType.TEXT_PLAIN)
  88.     public Response postText(String content) {
  89.         currentKey = currentKey + 1;
  90.         LOGGER.info("POST invoked ...");
  91.         String[] parts = content.split("-");
  92.         String part1 = parts[0]; // 004
  93.         String part2 = parts[1]; // 034556
  94.        
  95.         Connection c = null;
  96.         Statement stmt = null;
  97.         String result="";
  98.         String id="";
  99.         String name="";
  100.         String surname="";
  101.         try {
  102.              Class.forName("org.sqlite.JDBC");
  103.              c = DriverManager.getConnection("jdbc:sqlite:/home/biar/RESTWS.db");
  104.              c.setAutoCommit(false);
  105.              LOGGER.info("Opened database successfully");
  106.  
  107.             stmt = c.createStatement();
  108.             stmt.executeUpdate("drop table if exists professor;");
  109.             stmt.executeUpdate("create table professor (id, name, surname);");
  110.             PreparedStatement prep = c.prepareStatement(
  111.                     "insert into professor values (?, ?, ?);");
  112.            
  113.             prep.setString(1, Integer.toString(currentKey));
  114.             prep.setString(2, part1);
  115.             prep.setString(3, part2);
  116.             prep.addBatch();
  117.             c.setAutoCommit(false);
  118.             prep.executeBatch();
  119.             c.setAutoCommit(true);
  120.             ResultSet rs = stmt.executeQuery("select * from professor;");
  121.              while ( rs.next() ) {
  122.               id=rs.getString("id");
  123.              name=rs.getString("name");
  124.              surname= rs.getString("surname");
  125.               result+= id+ " : "+name+" "+surname+"\n";
  126.             }
  127.             rs.close();
  128.             stmt.close();
  129.             c.close();
  130.         } catch ( Exception e ) {
  131.       System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  132.       System.exit(0);
  133.      }
  134.         LOGGER.info("... added a new result. Now the data are " +result);
  135.         return Response.created(context.getAbsolutePath()).build();
  136.     }
  137.  
  138.     /**
  139.      * Sub-resource locator method for {id}
  140.      */
  141.     @Path("{id}")
  142.     public ProfessorResource getProfessorResource(@PathParam("id") String id) {
  143.         return ProfessorResource.getInstance(id);
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement