· 8 years ago · Nov 23, 2017, 04:26 PM
1package aula;
2import java.io.BufferedReader;
3import java.io.File;
4import java.io.InputStreamReader;
5import java.io.StringReader;
6import java.net.URL;
7import java.net.URLConnection;
8import java.net.URLEncoder;
9import java.sql.Connection;
10import java.sql.DriverManager;
11import java.sql.PreparedStatement;
12import java.sql.ResultSet;
13import java.sql.SQLException;
14import java.sql.Statement;
15import java.util.ArrayList;
16import java.util.List;
17
18import javax.xml.bind.JAXBContext;
19import javax.xml.bind.Unmarshaller;
20
21
22
23public class BancoDados {
24
25 public Connection conectar() throws SQLException, ClassNotFoundException {
26 Connection conexao = null;
27 Class.forName("org.sqlite.JDBC");
28 File bd = new File("bdprevisao.db");
29 /* verifica se o arquivo do BD existe na raiz do projeto */
30 if( !bd.exists() ){
31 /* cria o arquivo do BD na raiz do projeto e cria uma conexão para o BD */
32 conexao = DriverManager.getConnection("jdbc:sqlite:bdprevisao.db");
33 /* como o BD não existe então é necessário criar as tabelas */
34 //createTableCidade();
35 //createTablePrevisao();
36 }
37 else{
38 /* cria uma conexão com o BD */
39 conexao = DriverManager.getConnection("jdbc:sqlite:bdprevisao.db");
40 }
41 conexao.setAutoCommit(false);
42 return conexao;
43 }
44
45 public boolean createTableCidade(Connection conexao) throws SQLException{
46 Statement stmt = conexao.createStatement();
47 String sql = "create table if not exists tbcidade( " +
48 "id int not null," +
49 "nome varchar(64) not null," +
50 "uf char(2) not null," +
51 "atualizacao varchar(10)" +
52 ")";
53 stmt.executeUpdate(sql);
54 stmt.close();
55 return true;
56 };
57
58 public boolean createTablePrevisao(Connection conexao) throws SQLException{
59
60 Statement stmt = conexao.createStatement();
61 String sql = "create table if not exists tbprevisao( " +
62 "id int not null," +
63 "dia date not null," +
64 "tempo char(3) not null," +
65 "minima float not null," +
66 "maxima float not null," +
67 "iuv float not null," +
68 "primary key (id, dia)," +
69 "foreign key (id) references tbcidade(id) " +
70 ")";
71 stmt.executeUpdate(sql);
72 stmt.close();
73 return true;
74 }
75
76
77
78 public boolean insertCidade(Cidade cidade, Connection conexao) throws SQLException{
79 // o campo atualizacao irá receber o valor padrão, ou seja, null
80 String sql = "insert or ignore into tbcidade(id,nome,uf) values(?,?,?)";
81 PreparedStatement stmt = conexao.prepareStatement(sql);
82 stmt.setInt(1, cidade.getId() );
83 stmt.setString(2, cidade.getNome() );
84 stmt.setString(3, cidade.getUf() );
85 stmt.execute();
86 stmt.close();
87 conexao.commit();
88 return true;
89 }
90
91
92 public List<Cidade> selectCidade(String sql, Connection conexao) throws SQLException{
93 Statement stmt = conexao.createStatement();
94 ResultSet rs = stmt.executeQuery(sql);
95 List<Cidade> lista = new ArrayList<>();
96 Cidade cidade;
97 while ( rs.next() ) {
98 cidade = new Cidade();
99 cidade.setId(rs.getInt("id"));
100 cidade.setNome(rs.getString("nome"));
101 cidade.setUf(rs.getString("uf"));
102 cidade.setAtualizacao(rs.getString("atualizacao"));
103 }
104 rs.close();
105 stmt.close();
106 conexao.commit();
107 return lista;
108 }
109
110 public String getXMLCidade(String cidade) throws Exception {
111 String charset = java.nio.charset.StandardCharsets.ISO_8859_1.name();
112 String linha, resultado = "";
113 String urlListaCidade = "http://servicos.cptec.inpe.br/XML/listaCidades?city=%s";
114
115
116//codifica os parâmetros
117 String parametro = String.format(urlListaCidade, URLEncoder.encode(cidade, charset) );
118 URL url = new URL(parametro);
119 URLConnection conexao = url.openConnection();
120 BufferedReader reader = new BufferedReader(new InputStreamReader(conexao.getInputStream()));
121 while((linha = reader.readLine()) != null){
122 resultado += linha;
123 }
124 return resultado;
125 }
126
127public Cidade[] xmlToObjectCidade(String xml) throws Exception {
128 StringReader sr = new StringReader(xml);
129 /* a base do XML é uma marcação de nome cidades */
130 JAXBContext context = JAXBContext.newInstance(Cidades.class);
131 Unmarshaller un = context.createUnmarshaller();
132 Cidades cidades = (Cidades) un.unmarshal(sr);
133 return cidades.getCidade();
134 }
135
136package aula;
137
138import javax.xml.bind.annotation.XmlElement;
139import javax.xml.bind.annotation.XmlRootElement;
140import javax.xml.bind.annotation.XmlType;
141@XmlRootElement(name = "cidade")
142@XmlType(propOrder = {"nome", "uf", "id"})
143public class Cidade {
144 @XmlElement(name = "id")
145 private Integer id;
146 @XmlElement(name = "nome")
147 private String nome;
148 @XmlElement(name = "uf")
149 private String uf;
150private String atualicao;
151
152public void setId(int int1) {
153 // TODO Auto-generated method stub
154 this.id = int1;
155}
156
157public void setAtualizacao(String string) {
158 // TODO Auto-generated method stub
159 this.atualicao = string;
160}
161
162public void setUf(String string) {
163 // TODO Auto-generated method stub
164 this.uf = string;
165}
166
167public void setNome(String string) {
168 // TODO Auto-generated method stub
169 this.nome = string;
170}
171
172public Integer getId() {
173 return id;
174}
175
176public String getNome() {
177 return nome;
178}
179
180public String getUf() {
181 return uf;
182}
183
184public String getAtualicao() {
185 return atualicao;
186}
187
188public void setId(Integer id) {
189 this.id = id;
190}
191
192public void setAtualicao(String atualicao) {
193 this.atualicao = atualicao;
194}
195
196
197}
198
199package aula;
200
201import javax.xml.bind.annotation.XmlElement;
202import javax.xml.bind.annotation.XmlRootElement;
203import javax.xml.bind.annotation.XmlType;
204
205@XmlRootElement(name = "cidades")
206@XmlType(propOrder = {"cidade"})
207
208public class Cidades {
209 @XmlElement
210 private Cidade[] cidade;
211
212public Cidade[] getCidade() {
213 // TODO Auto-generated method stub
214 return null;
215}
216}
217
218com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 7 counts of IllegalAnnotationExceptions
219A propriedade atualicao está presente, mas não foi especificada em @XmlType.propOrder
220 this problem is related to the following location:
221 at public java.lang.String aula.Cidade.getAtualicao()
222 at aula.Cidade
223 at private aula.Cidade[] aula.Cidades.cidade
224 at aula.Cidades
225Há duas propriedades com o nome "id"
226 this problem is related to the following location:
227 at public java.lang.Integer aula.Cidade.getId()
228 at aula.Cidade
229 at private aula.Cidade[] aula.Cidades.cidade
230 at aula.Cidades
231 this problem is related to the following location:
232 at private java.lang.Integer aula.Cidade.id
233 at aula.Cidade
234 at private aula.Cidade[] aula.Cidades.cidade
235 at aula.Cidades
236Há duas propriedades com o nome "nome"
237 this problem is related to the following location:
238 at public java.lang.String aula.Cidade.getNome()
239 at aula.Cidade
240 at private aula.Cidade[] aula.Cidades.cidade
241 at aula.Cidades
242 this problem is related to the following location:
243 at private java.lang.String aula.Cidade.nome
244 at aula.Cidade
245 at private aula.Cidade[] aula.Cidades.cidade
246 at aula.Cidades
247Há duas propriedades com o nome "uf"
248 this problem is related to the following location:
249 at public java.lang.String aula.Cidade.getUf()
250 at aula.Cidade
251 at private aula.Cidade[] aula.Cidades.cidade
252 at aula.Cidades
253 this problem is related to the following location:
254 at private java.lang.String aula.Cidade.uf
255 at aula.Cidade
256 at private aula.Cidade[] aula.Cidades.cidade
257 at aula.Cidades
258A classe tem duas propriedades do mesmo nome "nome"
259 this problem is related to the following location:
260 at public java.lang.String aula.Cidade.getNome()
261 at aula.Cidade
262 at private aula.Cidade[] aula.Cidades.cidade
263 at aula.Cidades
264 this problem is related to the following location:
265 at private java.lang.String aula.Cidade.nome
266 at aula.Cidade
267 at private aula.Cidade[] aula.Cidades.cidade
268 at aula.Cidades
269A classe tem duas propriedades do mesmo nome "uf"
270 this problem is related to the following location:
271 at public java.lang.String aula.Cidade.getUf()
272 at aula.Cidade
273 at private aula.Cidade[] aula.Cidades.cidade
274 at aula.Cidades
275 this problem is related to the following location:
276 at private java.lang.String aula.Cidade.uf
277 at aula.Cidade
278 at private aula.Cidade[] aula.Cidades.cidade
279 at aula.Cidades
280A classe tem duas propriedades do mesmo nome "id"
281 this problem is related to the following location:
282 at public java.lang.Integer aula.Cidade.getId()
283 at aula.Cidade
284 at private aula.Cidade[] aula.Cidades.cidade
285 at aula.Cidades
286 this problem is related to the following location:
287 at private java.lang.Integer aula.Cidade.id
288 at aula.Cidade
289 at private aula.Cidade[] aula.Cidades.cidade
290 at aula.Cidades
291
292 at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(Unknown Source)
293 at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(Unknown Source)
294 at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source)
295 at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source)
296 at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(Unknown Source)
297 at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source)
298 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
299 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
300 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
301 at java.lang.reflect.Method.invoke(Unknown Source)
302 at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
303 at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
304 at javax.xml.bind.ContextFinder.find(Unknown Source)
305 at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
306 at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
307 at aula.BancoDados.xmlToObjectCidade(BancoDados.java:128)
308 at aula.Main.main(Main.java:26)
309
310BancoDados banco = new BancoDados();
311
312 try {
313 Connection conexao = banco.conectar();
314 banco.createTableCidade(conexao);
315 banco.createTablePrevisao(conexao);
316
317 //List<Cidade> teste = banco.selectCidade(sql, conexao);
318
319 try {
320 String testando = banco.getXMLCidade("sao jose");
321
322 System.out.println(testando);
323
324 banco.xmlToObjectCidade(banco.getXMLCidade("sao jose"));
325
326
327 } catch (Exception e) {
328 // TODO Auto-generated catch block
329 e.printStackTrace();
330 }
331
332 } catch (ClassNotFoundException e) {
333 // TODO Auto-generated catch block
334 e.printStackTrace();
335 } catch (SQLException e) {
336 // TODO Auto-generated catch block
337 e.printStackTrace();
338 }