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 java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import javax.ws.rs.Consumes;
- import javax.ws.rs.Produces;
- import javax.ws.rs.GET;
- import javax.ws.rs.PUT;
- import javax.ws.rs.DELETE;
- import javax.ws.rs.core.MediaType;
- import java.util.logging.Logger;
- /**
- * REST Web Service
- *
- * @author biar
- */
- public class ProfessorResource {
- private final static Logger LOGGER = Logger.getLogger(ProfessorResource.class.getName());
- private String id;
- /**
- * Creates a new instance of ProfessorResource
- */
- private ProfessorResource(String id) {
- this.id = id;
- }
- /**
- * Get instance of the ProfessorResource
- */
- public static ProfessorResource getInstance(String id) {
- // The user may use some kind of persistence mechanism
- // to store and restore instances of ProfessorResource class.
- return new ProfessorResource(id);
- }
- /**
- * Retrieves representation of an instance of org.me.service.ProfessorResource
- * @return an instance of java.lang.String
- */
- @GET
- @Produces(MediaType.TEXT_PLAIN)
- public String getText() {
- LOGGER.info("POST invoked ...");
- Connection c = null;
- Statement stmt = null;
- String result="";
- String id1="";
- 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();
- PreparedStatement prep = c.prepareStatement(
- "select * from professor where id=?;");
- prep.setString(1, id);
- ResultSet rs = prep.executeQuery();
- while ( rs.next() ){
- id1=rs.getString("id");
- name=rs.getString("name");
- surname= rs.getString("surname");
- result+= id1+ " : "+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("... Your selection is" +result);
- return result;
- }
- /**
- * PUT method for updating or creating an instance of ProfessorResource
- * @param content representation for the resource
- */
- @PUT
- @Consumes(MediaType.TEXT_PLAIN)
- public void putText(String content) {
- 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 id1="";
- 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();
- PreparedStatement prep = c.prepareStatement(
- "insert into professor values (?, ?, ?);");
- prep.setString(1, id);
- 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() ) {
- id1=rs.getString("id");
- name=rs.getString("name");
- surname= rs.getString("surname");
- result+= id1+ " : "+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);
- }
- /**
- * DELETE method for resource ProfessorResource
- */
- @DELETE
- public void delete() {
- LOGGER.info("POST invoked ...");
- Connection c = null;
- Statement stmt = null;
- 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();
- PreparedStatement prep = c.prepareStatement(
- "delete from professor where id=?;");
- prep.setString(1, id);
- prep.addBatch();
- c.setAutoCommit(false);
- prep.executeBatch();
- c.setAutoCommit(true);
- stmt.close();
- c.close();
- } catch ( Exception e ) {
- System.err.println( e.getClass().getName() + ": " + e.getMessage() );
- System.exit(0);
- }
- LOGGER.info("... Your selection is deleted");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement