Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package aula;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.InputStreamReader;
- import java.io.StringReader;
- import java.net.URL;
- import java.net.URLConnection;
- import java.net.URLEncoder;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.List;
- import javax.xml.bind.JAXBContext;
- import javax.xml.bind.Unmarshaller;
- public class BancoDados {
- public Connection conectar() throws SQLException, ClassNotFoundException {
- Connection conexao = null;
- Class.forName("org.sqlite.JDBC");
- File bd = new File("bdprevisao.db");
- /* verifica se o arquivo do BD existe na raiz do projeto */
- if( !bd.exists() ){
- /* cria o arquivo do BD na raiz do projeto e cria uma conexão para o BD */
- conexao = DriverManager.getConnection("jdbc:sqlite:bdprevisao.db");
- /* como o BD não existe então é necessário criar as tabelas */
- //createTableCidade();
- //createTablePrevisao();
- }
- else{
- /* cria uma conexão com o BD */
- conexao = DriverManager.getConnection("jdbc:sqlite:bdprevisao.db");
- }
- conexao.setAutoCommit(false);
- return conexao;
- }
- public boolean createTableCidade(Connection conexao) throws SQLException{
- Statement stmt = conexao.createStatement();
- String sql = "create table if not exists tbcidade( " +
- "id int not null," +
- "nome varchar(64) not null," +
- "uf char(2) not null," +
- "atualizacao varchar(10)" +
- ")";
- stmt.executeUpdate(sql);
- stmt.close();
- return true;
- };
- public boolean createTablePrevisao(Connection conexao) throws SQLException{
- Statement stmt = conexao.createStatement();
- String sql = "create table if not exists tbprevisao( " +
- "id int not null," +
- "dia date not null," +
- "tempo char(3) not null," +
- "minima float not null," +
- "maxima float not null," +
- "iuv float not null," +
- "primary key (id, dia)," +
- "foreign key (id) references tbcidade(id) " +
- ")";
- stmt.executeUpdate(sql);
- stmt.close();
- return true;
- }
- public boolean insertCidade(Cidade cidade, Connection conexao) throws SQLException{
- // o campo atualizacao irá receber o valor padrão, ou seja, null
- String sql = "insert or ignore into tbcidade(id,nome,uf) values(?,?,?)";
- PreparedStatement stmt = conexao.prepareStatement(sql);
- stmt.setInt(1, cidade.getId() );
- stmt.setString(2, cidade.getNome() );
- stmt.setString(3, cidade.getUf() );
- stmt.execute();
- stmt.close();
- conexao.commit();
- return true;
- }
- public List<Cidade> selectCidade(String sql, Connection conexao) throws SQLException{
- Statement stmt = conexao.createStatement();
- ResultSet rs = stmt.executeQuery(sql);
- List<Cidade> lista = new ArrayList<>();
- Cidade cidade;
- while ( rs.next() ) {
- cidade = new Cidade();
- cidade.setId(rs.getInt("id"));
- cidade.setNome(rs.getString("nome"));
- cidade.setUf(rs.getString("uf"));
- cidade.setAtualizacao(rs.getString("atualizacao"));
- }
- rs.close();
- stmt.close();
- conexao.commit();
- return lista;
- }
- public String getXMLCidade(String cidade) throws Exception {
- String charset = java.nio.charset.StandardCharsets.ISO_8859_1.name();
- String linha, resultado = "";
- String urlListaCidade = "http://servicos.cptec.inpe.br/XML/listaCidades?city=%s";
- //codifica os parâmetros
- String parametro = String.format(urlListaCidade, URLEncoder.encode(cidade, charset) );
- URL url = new URL(parametro);
- URLConnection conexao = url.openConnection();
- BufferedReader reader = new BufferedReader(new InputStreamReader(conexao.getInputStream()));
- while((linha = reader.readLine()) != null){
- resultado += linha;
- }
- return resultado;
- }
- public Cidade[] xmlToObjectCidade(String xml) throws Exception {
- StringReader sr = new StringReader(xml);
- /* a base do XML é uma marcação de nome cidades */
- JAXBContext context = JAXBContext.newInstance(Cidades.class);
- Unmarshaller un = context.createUnmarshaller();
- Cidades cidades = (Cidades) un.unmarshal(sr);
- return cidades.getCidade();
- }
- package aula;
- import javax.xml.bind.annotation.XmlElement;
- import javax.xml.bind.annotation.XmlRootElement;
- import javax.xml.bind.annotation.XmlType;
- @XmlRootElement(name = "cidade")
- @XmlType(propOrder = {"nome", "uf", "id"})
- public class Cidade {
- @XmlElement(name = "id")
- private Integer id;
- @XmlElement(name = "nome")
- private String nome;
- @XmlElement(name = "uf")
- private String uf;
- private String atualicao;
- public void setId(int int1) {
- // TODO Auto-generated method stub
- this.id = int1;
- }
- public void setAtualizacao(String string) {
- // TODO Auto-generated method stub
- this.atualicao = string;
- }
- public void setUf(String string) {
- // TODO Auto-generated method stub
- this.uf = string;
- }
- public void setNome(String string) {
- // TODO Auto-generated method stub
- this.nome = string;
- }
- public Integer getId() {
- return id;
- }
- public String getNome() {
- return nome;
- }
- public String getUf() {
- return uf;
- }
- public String getAtualicao() {
- return atualicao;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public void setAtualicao(String atualicao) {
- this.atualicao = atualicao;
- }
- }
- package aula;
- import javax.xml.bind.annotation.XmlElement;
- import javax.xml.bind.annotation.XmlRootElement;
- import javax.xml.bind.annotation.XmlType;
- @XmlRootElement(name = "cidades")
- @XmlType(propOrder = {"cidade"})
- public class Cidades {
- @XmlElement
- private Cidade[] cidade;
- public Cidade[] getCidade() {
- // TODO Auto-generated method stub
- return null;
- }
- }
- com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 7 counts of IllegalAnnotationExceptions
- A propriedade atualicao está presente, mas não foi especificada em @XmlType.propOrder
- this problem is related to the following location:
- at public java.lang.String aula.Cidade.getAtualicao()
- at aula.Cidade
- at private aula.Cidade[] aula.Cidades.cidade
- at aula.Cidades
- Há duas propriedades com o nome "id"
- this problem is related to the following location:
- at public java.lang.Integer aula.Cidade.getId()
- at aula.Cidade
- at private aula.Cidade[] aula.Cidades.cidade
- at aula.Cidades
- this problem is related to the following location:
- at private java.lang.Integer aula.Cidade.id
- at aula.Cidade
- at private aula.Cidade[] aula.Cidades.cidade
- at aula.Cidades
- Há duas propriedades com o nome "nome"
- this problem is related to the following location:
- at public java.lang.String aula.Cidade.getNome()
- at aula.Cidade
- at private aula.Cidade[] aula.Cidades.cidade
- at aula.Cidades
- this problem is related to the following location:
- at private java.lang.String aula.Cidade.nome
- at aula.Cidade
- at private aula.Cidade[] aula.Cidades.cidade
- at aula.Cidades
- Há duas propriedades com o nome "uf"
- this problem is related to the following location:
- at public java.lang.String aula.Cidade.getUf()
- at aula.Cidade
- at private aula.Cidade[] aula.Cidades.cidade
- at aula.Cidades
- this problem is related to the following location:
- at private java.lang.String aula.Cidade.uf
- at aula.Cidade
- at private aula.Cidade[] aula.Cidades.cidade
- at aula.Cidades
- A classe tem duas propriedades do mesmo nome "nome"
- this problem is related to the following location:
- at public java.lang.String aula.Cidade.getNome()
- at aula.Cidade
- at private aula.Cidade[] aula.Cidades.cidade
- at aula.Cidades
- this problem is related to the following location:
- at private java.lang.String aula.Cidade.nome
- at aula.Cidade
- at private aula.Cidade[] aula.Cidades.cidade
- at aula.Cidades
- A classe tem duas propriedades do mesmo nome "uf"
- this problem is related to the following location:
- at public java.lang.String aula.Cidade.getUf()
- at aula.Cidade
- at private aula.Cidade[] aula.Cidades.cidade
- at aula.Cidades
- this problem is related to the following location:
- at private java.lang.String aula.Cidade.uf
- at aula.Cidade
- at private aula.Cidade[] aula.Cidades.cidade
- at aula.Cidades
- A classe tem duas propriedades do mesmo nome "id"
- this problem is related to the following location:
- at public java.lang.Integer aula.Cidade.getId()
- at aula.Cidade
- at private aula.Cidade[] aula.Cidades.cidade
- at aula.Cidades
- this problem is related to the following location:
- at private java.lang.Integer aula.Cidade.id
- at aula.Cidade
- at private aula.Cidade[] aula.Cidades.cidade
- at aula.Cidades
- at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(Unknown Source)
- at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(Unknown Source)
- at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source)
- at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source)
- at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(Unknown Source)
- at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
- at java.lang.reflect.Method.invoke(Unknown Source)
- at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
- at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
- at javax.xml.bind.ContextFinder.find(Unknown Source)
- at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
- at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
- at aula.BancoDados.xmlToObjectCidade(BancoDados.java:128)
- at aula.Main.main(Main.java:26)
- BancoDados banco = new BancoDados();
- try {
- Connection conexao = banco.conectar();
- banco.createTableCidade(conexao);
- banco.createTablePrevisao(conexao);
- //List<Cidade> teste = banco.selectCidade(sql, conexao);
- try {
- String testando = banco.getXMLCidade("sao jose");
- System.out.println(testando);
- banco.xmlToObjectCidade(banco.getXMLCidade("sao jose"));
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
Add Comment
Please, Sign In to add comment