Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- /* PESQUISANDO BANCO DE DADOS */
- List<String> listaLojas = new ArrayList();
- List<String> listaStatus = new ArrayList();
- List<String> listaModelos = new ArrayList();
- List<String> listaCodigos = new ArrayList();
- int numeroDeLinhas = 0;
- String sql = "SELECT * FROM pos WHERE loja LIKE ? and status LIKE ? and modelo LIKE ?;";
- /*
- Pesquisar coluna LIKE "%" aceita qualquer valor
- Então, se não for selecionada uma opção em uma das combobox, pesquisa com "%"
- */
- String loja = new Integer(jComboBox1.getSelectedIndex()).equals(0) ? "%" : jComboBox1.getSelectedItem().toString();
- String status = new Integer(jComboBox2.getSelectedIndex()).equals(0) ? "%" : jComboBox2.getSelectedItem().toString();
- String modelo = new Integer(jComboBox3.getSelectedIndex()).equals(0) ? "%" : jComboBox3.getSelectedItem().toString();
- /*
- Isso ai em cima é operador ternário, é tipo um if/else inline
- Tá checando, em cada combobox, se a opção selecionada é a primeira (0), ou seja, nenhum valor definido
- Ai, se não selecionaram nada, puxa registros com qualquer valor nessa coluna, usando o LIKE "%"
- */
- try {
- pst = conecta.prepareStatement(sql);
- pst.setString(1, loja);
- pst.setString(2, status);
- pst.setString(3, modelo);
- rs = pst.executeQuery();
- // Puxando a lista dos valores que retornaram da consulta ao bd:
- while(rs.next())
- {
- listaLojas.add(rs.getString("loja"));
- listaStatus.add(rs.getString("status"));
- listaModelos.add(rs.getString("modelo"));
- listaCodigos.add(rs.getString("codigo"));
- numeroDeLinhas++;
- }
- } catch (SQLException error) {
- JOptionPane.showMessageDialog(null, "1 " + error);
- }
- /* PEGANDO DATA E HORA PARA INCLUIR NO RELATORIO */
- SimpleDateFormat dataForma = new SimpleDateFormat("dd/MM/yyy - HH:mm");
- String data = dataForma.format(new Date()) + "h";
- /* CRIANDO DOCUMENTO XHTML VIA DOM */
- try
- {
- Document xhtml = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
- Element root = xhtml.createElement("html");
- root.appendChild(xhtml.createElement("body"));
- Element table = xhtml.createElement("table");
- table.setAttribute("style", "border-collapse: collapse; margin-top: 1cm;");
- Element titulo = xhtml.createElement("tr");
- Element tituloLoja = xhtml.createElement("td");
- Element tituloStatus = xhtml.createElement("td");
- Element tituloModelo = xhtml.createElement("td");
- Element tituloCodigo = xhtml.createElement("td");
- tituloLoja.appendChild(xhtml.createTextNode("LOJA"));
- tituloStatus.appendChild(xhtml.createTextNode("STATUS"));
- tituloModelo.appendChild(xhtml.createTextNode("MODELO"));
- tituloCodigo.appendChild(xhtml.createTextNode("CODIGO"));
- titulo.appendChild(tituloLoja);
- titulo.appendChild(tituloStatus);
- titulo.appendChild(tituloModelo);
- titulo.appendChild(tituloCodigo);
- titulo.setAttribute("style", "font-size: .8cm; padding-bottom: .5mm; text-align: center");
- table.appendChild(titulo);
- for(int i = 0; i < numeroDeLinhas; i++)
- {
- Element tr = xhtml.createElement("tr");
- for(int j = 0; j < 4; j++)
- {
- Element td = xhtml.createElement("td");
- td.setAttribute("style", "padding-top: .2cm; padding-bottom: .2cm; width: 7cm; text-align: center; border-bottom: .2mm #000 solid;");
- String texto;
- if(j == 0)
- texto = listaLojas.get(i);
- else if(j == 1)
- texto = listaStatus.get(i);
- else if(j == 2)
- texto = listaModelos.get(i);
- else
- texto = listaCodigos.get(i);
- td.appendChild(xhtml.createTextNode(texto));
- tr.appendChild(td);
- }
- table.appendChild(tr);
- }
- root.getFirstChild().appendChild(xhtml.createTextNode(data));
- root.getFirstChild().appendChild(table);
- xhtml.appendChild(root);
- /* TRANSFORMANDO DOCUMENTO EM STRING */
- DOMImplementationLS domLS = (DOMImplementationLS) xhtml.getImplementation();
- LSSerializer serializer = domLS.createLSSerializer();
- String xhtmlString = serializer.writeToString(xhtml);
- InputStream stream = new ByteArrayInputStream(xhtmlString.getBytes(StandardCharsets.UTF_8));
- /* CONVERTENDO PARA PDF VIA ITEXT */
- File pdf = new File("relatorio.pdf");
- pdf.createNewFile();
- com.itextpdf.text.Document doc = new com.itextpdf.text.Document(PageSize.A4);
- FileOutputStream sPdf = new FileOutputStream(pdf);
- PdfWriter writer = PdfWriter.getInstance(doc, sPdf);
- doc.open();
- XMLWorkerHelper.getInstance().parseXHtml(writer, doc, stream);
- doc.close();
- sPdf.close();
- /* ABRINDO O ARQUIVO */
- Desktop.getDesktop().open(pdf);
- }
- catch(DocumentException|FileNotFoundException e)
- {
- JOptionPane.showMessageDialog(null, "2 " + e.getClass().toString());
- }
- catch(IOException e)
- {
- JOptionPane.showMessageDialog(null, "3 " + e.toString());
- }
- catch(Exception e){
- JOptionPane.showMessageDialog(null, "4 " + e.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement