caio/ws/projetocriandows.pdf · após adicionar a biblioteca jdbc do postgresql, pode-se testar o...

8

Upload: lamthu

Post on 29-Dec-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

import java.sql.Statement;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.logging.Level;

import java.util.logging.Logger;

/**

*

* @author Caio.Nakashima

*/

public class MunicipioEstado {

public static void main (String arg[]) {

MunicipioEstado a = new MunicipioEstado();

System.out.println(a.buscaMunicipio("12"));

}

public String buscaMunicipio (String UF) {

String retorno="";

try {

Class.forName("org.postgresql.Driver");

String contxt = "jdbc:postgresql://localhost:5432/postgres"

Connection connection =

DriverManager.getConnection(contxt,"postgres", "123456");

String textosql = "select ibge, nome from municipios where uf="+UF;

Statement statement = (Statement) connection.createStatement();

ResultSet rs = statement.executeQuery(textosql);

while (rs.next()) {

retorno += "<ibge>"+rs.getString("ibge")+"</ibge>";

retorno += "<municipio>"+rs.getString("nome")+"</municipio>";

}

connection.close();

} catch (ClassNotFoundException | SQLException ex) {

Logger.getLogger(MunicipioEstado.class.getName()).log(Level.SEVERE,

null, ex);

}

return retorno;

}

}

Adicionar a biblioteca POSTGRESQL http://jdbc.postgresql.org/download.html

Após adicionar a biblioteca jdbc do postgresql, pode-se testar o código java que faz a conexão com o banco de

dados e a consulta com o banco de dados.

Criando um WebService do código Java que consulta o banco de dados buscando os municípios de um determinado

estado.

Alterar o código que o ambiente gera automaticamente de:

@WebMethod(operationName = "hello")

public String hello(@WebParam(name = "name") String txt) {

return "Hello " + txt + " !";

}

para:

@WebMethod(operationName = "buscaMunicipio")

public String buscaMunicipio(@WebParam(name = "uf") String uf) {

MunicipioEstado a = new MunicipioEstado();

return a.buscaMunicipio(uf);

}

Próximo passo implantar o WebService no servidor Glassfish

Após implantar o WebService passa-se a testar o seu funcionamento.