Advertisement
Guest User

ProfessorResources.java

a guest
Jun 16th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.61 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 java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.Statement;
  13. import javax.ws.rs.Consumes;
  14. import javax.ws.rs.Produces;
  15. import javax.ws.rs.GET;
  16. import javax.ws.rs.PUT;
  17. import javax.ws.rs.DELETE;
  18. import javax.ws.rs.core.MediaType;
  19. import java.util.logging.Logger;
  20.  
  21. /**
  22.  * REST Web Service
  23.  *
  24.  * @author biar
  25.  */
  26. public class ProfessorResource {
  27.      private final static Logger LOGGER = Logger.getLogger(ProfessorResource.class.getName());
  28.  
  29.     private String id;
  30.  
  31.     /**
  32.      * Creates a new instance of ProfessorResource
  33.      */
  34.     private ProfessorResource(String id) {
  35.         this.id = id;
  36.     }
  37.  
  38.     /**
  39.      * Get instance of the ProfessorResource
  40.      */
  41.     public static ProfessorResource getInstance(String id) {
  42.         // The user may use some kind of persistence mechanism
  43.         // to store and restore instances of ProfessorResource class.
  44.         return new ProfessorResource(id);
  45.     }
  46.  
  47.     /**
  48.      * Retrieves representation of an instance of org.me.service.ProfessorResource
  49.      * @return an instance of java.lang.String
  50.      */
  51.     @GET
  52.     @Produces(MediaType.TEXT_PLAIN)
  53.     public String getText() {
  54.          LOGGER.info("POST invoked ...");
  55.        
  56.         Connection c = null;
  57.         Statement stmt = null;
  58.         String result="";
  59.         String id1="";
  60.         String name="";
  61.         String surname="";
  62.         try {
  63.              Class.forName("org.sqlite.JDBC");
  64.              c = DriverManager.getConnection("jdbc:sqlite:/home/biar/RESTWS.db");
  65.              c.setAutoCommit(false);
  66.              LOGGER.info("Opened database successfully");
  67.              stmt = c.createStatement();
  68.              PreparedStatement prep = c.prepareStatement(
  69.                     "select *  from professor where  id=?;");
  70.            
  71.              prep.setString(1, id);
  72.              ResultSet rs = prep.executeQuery();
  73.              while ( rs.next() ){
  74.                 id1=rs.getString("id");
  75.                 name=rs.getString("name");
  76.                 surname= rs.getString("surname");
  77.                  result+= id1+ " : "+name+" "+surname+"\n";
  78.                 }
  79.             rs.close();
  80.             stmt.close();
  81.             c.close();
  82.         } catch ( Exception e ) {
  83.       System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  84.       System.exit(0);
  85.      }
  86.         LOGGER.info("... Your selection is" +result);
  87.         return result;
  88.     }
  89.  
  90.     /**
  91.      * PUT method for updating or creating an instance of ProfessorResource
  92.      * @param content representation for the resource
  93.      */
  94.     @PUT
  95.     @Consumes(MediaType.TEXT_PLAIN)
  96.     public void putText(String content) {
  97.         LOGGER.info("POST invoked ...");
  98.         String[] parts = content.split("-");
  99.         String part1 = parts[0]; // 004
  100.         String part2 = parts[1]; // 034556
  101.        
  102.         Connection c = null;
  103.         Statement stmt = null;
  104.         String result="";
  105.         String id1="";
  106.         String name="";
  107.         String surname="";
  108.         try {
  109.              Class.forName("org.sqlite.JDBC");
  110.              c = DriverManager.getConnection("jdbc:sqlite:/home/biar/RESTWS.db");
  111.              c.setAutoCommit(false);
  112.              LOGGER.info("Opened database successfully");
  113.  
  114.             stmt = c.createStatement();
  115.             PreparedStatement prep = c.prepareStatement(
  116.                     "insert into professor values (?, ?, ?);");
  117.            
  118.             prep.setString(1, id);
  119.             prep.setString(2, part1);
  120.             prep.setString(3, part2);
  121.             prep.addBatch();
  122.             c.setAutoCommit(false);
  123.             prep.executeBatch();
  124.             c.setAutoCommit(true);
  125.             ResultSet rs = stmt.executeQuery("select * from professor;");
  126.              while ( rs.next() ) {
  127.               id1=rs.getString("id");
  128.              name=rs.getString("name");
  129.              surname= rs.getString("surname");
  130.               result+= id1+ " : "+name+" "+surname+"\n";
  131.             }
  132.             rs.close();
  133.             stmt.close();
  134.             c.close();
  135.         } catch ( Exception e ) {
  136.       System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  137.       System.exit(0);
  138.      }
  139.         LOGGER.info("... added a new result. Now the data are " +result);
  140.     }
  141.    
  142.  
  143.     /**
  144.      * DELETE method for resource ProfessorResource
  145.      */
  146.     @DELETE
  147.     public void delete() {
  148.              LOGGER.info("POST invoked ...");
  149.        
  150.         Connection c = null;
  151.         Statement stmt = null;
  152.         try {
  153.              Class.forName("org.sqlite.JDBC");
  154.              c = DriverManager.getConnection("jdbc:sqlite:/home/biar/RESTWS.db");
  155.              c.setAutoCommit(false);
  156.              LOGGER.info("Opened database successfully");
  157.  
  158.             stmt = c.createStatement();
  159.             PreparedStatement prep = c.prepareStatement(
  160.                     "delete from professor where  id=?;");
  161.            
  162.             prep.setString(1, id);
  163.             prep.addBatch();
  164.             c.setAutoCommit(false);
  165.             prep.executeBatch();
  166.             c.setAutoCommit(true);
  167.            
  168.             stmt.close();
  169.             c.close();
  170.         } catch ( Exception e ) {
  171.       System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  172.       System.exit(0);
  173.      }
  174.         LOGGER.info("... Your selection is deleted");
  175.     }
  176.    
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement