Thread + Consulta Banco + Gerar HTML - Onde esta o erro?

0 respostas
JP1

Olhem este código, ele monta páginas HTML contendo informações dos computadores. É uma thread, eu gostaria de que ele fizesse uma consulta ao banco(ta ele faz isso), e gerasse 1 HTML para cada registro, só que quando eu rodo a aplicação, ele gera 1 pagina certo, e as outras fica tudo bagunçado, ele replica, ou até tricplica o ocnteudo da HTML.

O que pode estar errado? Seria melhor não usar Thread?

Eis o código…

package servidor.conexoes;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.StringTokenizer;


public class Relatorio extends Thread {
    
    public static String nome, html = "", nome_pc, software="";
    public static FileOutputStream file;
    public static PrintStream print;
    public static String dir = "C:\jusms\";
    
    public static String br = System.getProperty("line.separator");
    
    public static String sistema_pc, sisVer_pc, sisPatch_pc, padrao_pc, lista_soft;
    
    public void inicializaRelatorio(){
        html = new String();
        RelatorioBD();
    }
    
    public static void cabecalho(){
        
        html = html + "<html>" + br;
        html = html + "<body>" + br;
        
    }
    
    public static void montaPG(){
        consultaBD();
        montaListaSoftware();
        html = html + "<h1><center> jUSMS Resumo </h1>" + br;
        html = html + "<br><b> Computador</b><br> " + br + nome_pc + br;
        html = html + "<br><b> IP</b><br> " + br + "10.0.0.139" + br;
        html = html + "<br><b> Sistema Operacional</b><br> " + br + sistema_pc + br;
        html = html + "<br><b> Versão</b><br> " + br + sisVer_pc + br;
        html = html + "<br><b> Versão do Service Pack</b><br> " + br + sisPatch_pc + br;
        html = html + "<br><b> Arquitetura</b><br> " + br + padrao_pc + br;
        html = html + "<h2><b> Lista de softwares instalados</h2></b> " + br;
        html = html + software + br;
        html = html + "</center>" + br;        
    }
    
    public static void rodape(){
        
        html = html + "</body>" + br;
        html = html + "</html>" + br;
        
    }
    
    public static void geraHTML(){
        try {
            file = new FileOutputStream(dir+nome_pc + ".html");
            print = new PrintStream(file);
            print.println(html);
            file.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println(e);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(e);
        }
        nome_pc = "";
        sistema_pc = "";
        sisVer_pc = "";
        sisPatch_pc = "";
        padrao_pc = "";
        lista_soft = "";
    }
    
    public static void consultaBD(){
        String dbURL = "jdbc:mysql://localhost/jusms";
        Connection c = null;
        ResultSet r = null;

        try
        {
           Class.forName("com.mysql.jdbc.Driver").newInstance();
           c = DriverManager.getConnection(dbURL, "", "");
           Statement s = c.createStatement();
           r = s.executeQuery("SELECT * FROM rede WHERE pc_name = '" + nome_pc + "'");
           while (r.next()){
               sistema_pc = r.getString("pc_os");
               sisVer_pc = r.getString("pc_ver");
               sisPatch_pc = r.getString("pc_patch");
               padrao_pc = r.getString("pc_padrao");
               lista_soft = r.getString("pc_softlist");
           }
        } catch (InstantiationException e)
        {
           e.printStackTrace();
        } catch (IllegalAccessException e)
        {
           e.printStackTrace();
        } catch (ClassNotFoundException e)
        {
           e.printStackTrace();
        } catch (SQLException e)
        {
           e.printStackTrace();
        } finally
        {
           try
           {
              r.close();
              c.close();
           } catch (SQLException e1)
           {
              e1.printStackTrace();
           }
        }
    }
    
    public static void RelatorioBD(){
        String dbURL = "jdbc:mysql://localhost/jusms";
        Connection c = null;
        ResultSet r = null;

        try
        {
           Class.forName("com.mysql.jdbc.Driver").newInstance();
           c = DriverManager.getConnection(dbURL, "", "");
           Statement s = c.createStatement();
           r = s.executeQuery("SELECT * FROM rede");
           while (r.next()){
               nome_pc = r.getString("pc_name");
               cabecalho();
               montaPG();
               rodape();
               geraHTML();
           }
        } catch (InstantiationException e)
        {
           e.printStackTrace();
        } catch (IllegalAccessException e)
        {
           e.printStackTrace();
        } catch (ClassNotFoundException e)
        {
           e.printStackTrace();
        } catch (SQLException e)
        {
           e.printStackTrace();
        } finally
        {
           try
           {
              r.close();
              c.close();
           } catch (SQLException e1)
           {
              e1.printStackTrace();
           }
        }
    }
    
    public static void montaListaSoftware(){
        StringTokenizer st = new StringTokenizer(lista_soft, ";");
        while(st.hasMoreTokens())
            software += "* " + st.nextToken() + "<br>\n" + br;
    }
    
    public void run(){
        inicializaRelatorio();
    }
}

se eu passo na consulta um nome de um PC, ai sim, funciona perfeitamente.

O que pode estar ocorrendo?

:roll:

Criado 4 de agosto de 2005
Respostas 0
Participantes 1