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 org.me.service;
- import javax.ws.rs.core.Context;
- import javax.ws.rs.core.UriInfo;
- import javax.ws.rs.PathParam;
- import javax.ws.rs.POST;
- import javax.ws.rs.Consumes;
- import javax.ws.rs.Produces;
- import javax.ws.rs.GET;
- import javax.ws.rs.Path;
- import javax.ws.rs.core.MediaType;
- import javax.ws.rs.core.Response;
- import java.sql.*;
- import java.util.logging.Logger;
- /**
- * REST Web Service
- *
- * @author biar
- */
- @Path("/ps")
- public class ProfessorsResource {
- private final static Logger LOGGER = Logger.getLogger(ProfessorsResource.class.getName());
- public static int currentKey = 3;
- @Context
- private UriInfo context;
- /**
- * Creates a new instance of ProfessorsResource
- */
- public ProfessorsResource() {
- }
- /**
- * Retrieves representation of an instance of org.me.service.ProfessorsResource
- * @return an instance of java.lang.String
- */
- @GET
- @Produces(MediaType.TEXT_PLAIN)
- public String getText() {
- Connection c = null;
- Statement stmt = null;
- String result="";
- String id="";
- String name="";
- String surname="";
- try {
- Class.forName("org.sqlite.JDBC");
- c = DriverManager.getConnection("jdbc:sqlite:/home/biar/RESTWS.db");
- c.setAutoCommit(false);
- LOGGER.info("Opened database successfully");
- stmt = c.createStatement();
- ResultSet rs = stmt.executeQuery( "SELECT * FROM professor;" );
- while ( rs.next() ) {
- id=rs.getString("id");
- name=rs.getString("name");
- surname= rs.getString("surname");
- result+= id+ " : "+name+" "+surname+"\n";
- }
- rs.close();
- stmt.close();
- c.close();
- } catch ( Exception e ) {
- System.err.println( e.getClass().getName() + ": " + e.getMessage() );
- System.exit(0);
- }
- LOGGER.info("Operation done successfully");
- return result;
- }
- /**
- * POST method for creating an instance of ProfessorResource
- * @param content representation for the new resource
- * @return an HTTP response with content of the created resource
- */
- @POST
- @Consumes(MediaType.TEXT_PLAIN)
- @Produces(MediaType.TEXT_PLAIN)
- public Response postText(String content) {
- currentKey = currentKey + 1;
- LOGGER.info("POST invoked ...");
- String[] parts = content.split("-");
- String part1 = parts[0]; // 004
- String part2 = parts[1]; // 034556
- Connection c = null;
- Statement stmt = null;
- String result="";
- String id="";
- String name="";
- String surname="";
- try {
- Class.forName("org.sqlite.JDBC");
- c = DriverManager.getConnection("jdbc:sqlite:/home/biar/RESTWS.db");
- c.setAutoCommit(false);
- LOGGER.info("Opened database successfully");
- stmt = c.createStatement();
- stmt.executeUpdate("drop table if exists professor;");
- stmt.executeUpdate("create table professor (id, name, surname);");
- PreparedStatement prep = c.prepareStatement(
- "insert into professor values (?, ?, ?);");
- prep.setString(1, Integer.toString(currentKey));
- prep.setString(2, part1);
- prep.setString(3, part2);
- prep.addBatch();
- c.setAutoCommit(false);
- prep.executeBatch();
- c.setAutoCommit(true);
- ResultSet rs = stmt.executeQuery("select * from professor;");
- while ( rs.next() ) {
- id=rs.getString("id");
- name=rs.getString("name");
- surname= rs.getString("surname");
- result+= id+ " : "+name+" "+surname+"\n";
- }
- rs.close();
- stmt.close();
- c.close();
- } catch ( Exception e ) {
- System.err.println( e.getClass().getName() + ": " + e.getMessage() );
- System.exit(0);
- }
- LOGGER.info("... added a new result. Now the data are " +result);
- return Response.created(context.getAbsolutePath()).build();
- }
- /**
- * Sub-resource locator method for {id}
- */
- @Path("{id}")
- public ProfessorResource getProfessorResource(@PathParam("id") String id) {
- return ProfessorResource.getInstance(id);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement