Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 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 com.airhacks;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15. import javax.json.Json;
  16. import javax.json.JsonArray;
  17. import javax.json.JsonObject;
  18. import javax.ws.rs.GET;
  19. import javax.ws.rs.Path;
  20.  
  21. /**
  22. *
  23. * @author mateus
  24. */
  25. @Path("dados")
  26. public class Test {
  27.  
  28. private final String SERVER = "127.0.0.1";
  29. private final String DATABASE = "teste";
  30. private final String USER = "mateus";
  31. private final String PASS = "root";
  32. private final String DRIVER = "com.mysql.jdbc.Driver";
  33. private final String URL = "jdbc:mysql://" + SERVER + "/" + DATABASE + "?zeroDateTimeBehavior=convertToNull";
  34. public Connection con = null;
  35. public ResultSet rs = null;
  36. public Statement stmt = null;
  37. public SQLException e;
  38.  
  39.  
  40. public boolean getConnection(){
  41. System.setProperty("jdbc.Drivers", DRIVER);
  42. try {
  43. con = DriverManager.getConnection(URL, USER, PASS);
  44. return true;
  45. } catch (SQLException ex) {
  46. e = ex;
  47. Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
  48. return false;
  49. }
  50. }
  51.  
  52.  
  53. @GET
  54. public JsonArray celulares(){
  55. if (getConnection()) {
  56. return Json.createArrayBuilder().
  57. add(celular("OK", "Sucesso")).
  58. build();
  59. } else {
  60. return Json.createArrayBuilder().
  61. add(celular(e.getMessage(), "Erro")).
  62. build();
  63. }
  64. }
  65.  
  66. public JsonObject celular(String marca, String modelo){
  67. return Json.createObjectBuilder().
  68. add("E: ", marca).
  69. add("Status: ", modelo).
  70. build();
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement