Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.67 KB | None | 0 0
  1. package giojejjere;
  2. import javax.annotation.PostConstruct;
  3. //import javax.activation.DataSource;
  4. import javax.annotation.Resource;
  5. import javax.ws.rs.*;
  6. import javax.ws.rs.GET;
  7. import javax.ws.rs.Path;
  8. import javax.ws.rs.core.Context;
  9.  
  10.  
  11. import jdk.nashorn.internal.objects.annotations.Getter;
  12.  
  13. import java.sql.*;
  14. import javax.sql.*;
  15.  
  16.  
  17.  
  18.  
  19. //@Resource(name="mariadb",lookup="jboss/mariadb");
  20.  
  21.  
  22. @Path("service")
  23. public class orafoRest{
  24.  
  25.     @Resource(lookup="java:/PostgresXADS") private DataSource ds;
  26.     private Connection conn;
  27.     /*
  28.     String url = "jdbc:postgresql://127.0.0.1:5433/test";
  29.     Connection conn;
  30.     */
  31.     String error = "";
  32.    
  33.  
  34.     public RestService(){
  35.         try {
  36.            
  37.             //DriverManager.registerDriver(new com.mariadb.jdbc.Driver ());
  38.             //Class.forName("com.postgresql.jdbc.Driver").newInstance();
  39.             //conn = DriverManager.getConnection(url,"root","root");
  40.             //error = "connesso";
  41.         } catch (Exception e) {
  42.             error = e.getMessage();
  43.         }
  44.     }
  45.    
  46.  
  47.    
  48.     @GET
  49.     @Path("insertneworder/{details}/{type_id}/{size}/{quantity}/{price}")
  50.     public String insert_record(@PathParam("details") String cusDet,
  51.                                 @PathParam("type_id") int type_id,
  52.                                 @PathParam("size") int size,
  53.                                 @PathParam("quantity") int quantity,
  54.                                 @PathParam("price") int price){
  55.         try {
  56.             conn = ds.getConnection();
  57.             String insertNewUserSQL = "INSERT INTO \"customerOrder\" "+
  58.             "(orderDetails, type_id, size, quantity, price)"+
  59.             "VALUES (?, ?, ?, ?, ?)";
  60.             PreparedStatement pstmt = conn.prepareStatement(insertNewUserSQL);
  61.             pstmt.setString(1, cusDet);
  62.             pstmt.setInt(2,type_id);
  63.             pstmt.setInt(3,size);
  64.             pstmt.setInt(4,quantity);
  65.             pstmt.setInt(5,price);
  66.            
  67.             pstmt.executeUpdate();
  68.  
  69.             return "<h1>DONE</h1>";
  70.         }catch (Exception e){
  71.            
  72.             return "<h1> an error has occurred :  <br>"+e.getMessage()+"</h1>";
  73.         }
  74.     }
  75.    
  76.    
  77.     @GET
  78.     @Path("getbaseprice/{type_id}")
  79.     public String show_users(@PathParam("type_id") int id){
  80.     String getBasePriceSQL = "SELECT * FROM TypePrice WHERE \"type_id\" = "+id;
  81.         //\"basePrice\"
  82.         try{
  83.            
  84.  
  85.             conn = ds.getConnection();
  86.             Statement t = conn.createStatement();
  87.             ResultSet rs = t.executeQuery(getBasePriceSQL);
  88.  
  89.             String tmp="<table>";
  90.             while (rs.next()) {
  91.  
  92.                 tmp+="<tr>";    
  93.                 tmp+="<td>"+rs.getInt("type_id")+"</td>";
  94.                 tmp+="<td>"+rs.getInt("basePrice")+"</td>";
  95.                 tmp+="</tr>";
  96.             }
  97.             tmp+="</table>";
  98.  
  99.             return tmp;
  100.  
  101.         }catch (Exception e){
  102.             return e.getMessage();
  103.         }
  104.     }
  105.    
  106.  
  107.     @GET
  108.     @Path("getsizemultiplier/{size}")
  109.     public String sayHello(@PathParam("size") int s) {
  110.  
  111.         String tmp = "";
  112.  
  113.         try {
  114.             conn = ds.getConnection();
  115.             Statement t = conn.createStatement();
  116.             ResultSet rs = t.executeQuery("SELECT multiplier FROM \"user\" WHERE size = "+s);
  117.  
  118.             while (rs.next()) {
  119.                 String txt = rs.getInt("multiplier")+"<br/>";
  120.                 System.out.println(txt);
  121.                 tmp += txt;
  122.             }    
  123.         } catch (Exception e) {
  124.             error+=e.getMessage();
  125.         }
  126.        
  127.  
  128.         return "<h1>"+error+" -- "+tmp+"</h1>";
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement