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 com.airhacks;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.json.Json;
- import javax.json.JsonArray;
- import javax.json.JsonObject;
- import javax.ws.rs.GET;
- import javax.ws.rs.Path;
- /**
- *
- * @author mateus
- */
- @Path("dados")
- public class Test {
- private final String SERVER = "127.0.0.1";
- private final String DATABASE = "teste";
- private final String USER = "mateus";
- private final String PASS = "root";
- private final String DRIVER = "com.mysql.jdbc.Driver";
- private final String URL = "jdbc:mysql://" + SERVER + "/" + DATABASE + "?zeroDateTimeBehavior=convertToNull";
- public Connection con = null;
- public ResultSet rs = null;
- public Statement stmt = null;
- public SQLException e;
- public boolean getConnection(){
- System.setProperty("jdbc.Drivers", DRIVER);
- try {
- con = DriverManager.getConnection(URL, USER, PASS);
- return true;
- } catch (SQLException ex) {
- e = ex;
- Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
- return false;
- }
- }
- @GET
- public JsonArray celulares(){
- if (getConnection()) {
- return Json.createArrayBuilder().
- add(celular("OK", "Sucesso")).
- build();
- } else {
- return Json.createArrayBuilder().
- add(celular(e.getMessage(), "Erro")).
- build();
- }
- }
- public JsonObject celular(String marca, String modelo){
- return Json.createObjectBuilder().
- add("E: ", marca).
- add("Status: ", modelo).
- build();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement