Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. package service;
  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 RestService{
  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("insert/{id}/{name}/{age}")
  50. public String insert_record(@PathParam("id") int id, @PathParam("name") String name, @PathParam("age") int age){
  51. try {
  52. conn = ds.getConnection();
  53.  
  54. String insertNewUserSQL = "INSERT INTO \"user\" (id, name, age) VALUES (?, ?, ?)";
  55. PreparedStatement pstmt = conn.prepareStatement(insertNewUserSQL);
  56. pstmt.setInt(1, id);
  57. pstmt.setString(2, name);
  58. pstmt.setInt(3, age);
  59. pstmt.executeUpdate();
  60.  
  61. return "<h1>DONE</h1>";
  62. }catch (Exception e){
  63.  
  64. return "<h1>"+e.getMessage()+"</h1>";
  65. }
  66. }
  67.  
  68.  
  69. @GET
  70. @Path("viewall")
  71. public String show_users(/*@Context UriInfo info non trova classe*/){
  72.  
  73. try{
  74.  
  75.  
  76. String order = "name"; //info.getQueryParameters().getFirst("order");
  77. //return "=====> "+order;
  78.  
  79. String query = "SELECT * FROM \"user\"";
  80. if (order!=""){
  81. query+=" ORDER BY \""+order+"\"";
  82. //return query;
  83. }
  84.  
  85. conn = ds.getConnection();
  86. Statement t = conn.createStatement();
  87. ResultSet rs = t.executeQuery(query);
  88.  
  89. String tmp="<table>";
  90. while (rs.next()) {
  91.  
  92. tmp+="<tr>";
  93. tmp+="<td>"+rs.getInt("id")+"</td>";
  94. tmp+="<td>"+rs.getString("name")+"</td>";
  95. tmp+="<td>"+rs.getInt("age")+"</td>";
  96. tmp+="</tr>";
  97. }
  98. tmp+="</table>";
  99.  
  100. return tmp;
  101.  
  102. }catch (Exception e){
  103. return e.getMessage();
  104. }
  105. }
  106.  
  107.  
  108. @GET
  109. @Path("view/{id}")
  110. public String sayHello(@PathParam("id") int id) {
  111.  
  112. String tmp = "";
  113.  
  114. try {
  115. conn = ds.getConnection();
  116. Statement t = conn.createStatement();
  117. ResultSet rs = t.executeQuery("SELECT name FROM \"user\" WHERE id = "+id);
  118.  
  119. while (rs.next()) {
  120. String txt = rs.getString("name")+"<br/>";
  121. System.out.println(txt);
  122. tmp += txt;
  123. }
  124. } catch (Exception e) {
  125. error+=e.getMessage();
  126. }
  127.  
  128.  
  129. return "<h1>"+error+" -- "+tmp+"</h1>";
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement