Passing variable value obtained in one method to another method in Java

The project is about a school GED, where there is the main class that makes the connection with database and there is another class to generate PDF files.

The problem is that the system manages to extract the desired data from the database, but cannot insert it into the PDF file.

See:

Main

import java.sql.*;
import static javax.swing.UIManager.getString;

public class Conectora {

    public static String nome;

    public static void main(String[] args){

        try {
            Statement atestamento;
            ResultSet resultado;

            String usuario = "postgres";
            String senha = "123";
            String endereco = "jdbc:postgresql://localhost:5433/ueer5";

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

            Connection con = DriverManager.getConnection(endereco, usuario, senha);

            atestamento = con.createStatement();

            atestamento.executeQuery("SELECT * FROM alunos WHERE codigo = 1;");

            resultado = atestamento.getResultSet();

            while (resultado.next()) {

                nome = resultado.getString(2);
            }

            System.out.println(nome);//fora do while

            con.close();

            System.out.println("Conexão bem-sucedida.");

        } catch (Exception e){
            System.out.println("Conexão mal-sucedida.\n"+e);
        }

        System.out.println(nome);//fora do try-catch
    }        

}

Other class

import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.PageSize;
import Gertrudes.Conectora;
import static Gertrudes.Conectora.*;
import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Declaradora {

    public static void main(String[] args){

        String aluno = null;
        String serie = "1º";
        String nascimento = "20/01/2001";
        String mae = "Beltrana de Tal";
        String pai = "Cicrano de Tal";
        String diretor = "Dirceu E. Torquato";

        Document dec = new Document(PageSize.A4);

        try{
            PdfWriter.getInstance(dec, new FileOutputStream("dec.pdf"));

            dec.open();
            Paragraph titulo = new Paragraph("DECLARAÇÃO");
            titulo.setAlignment(1);
            titulo.setSpacingAfter(30);
            dec.add(titulo);

            Paragraph texto = new Paragraph("Declaro para os fins que se fizerem necessários que o (a) aluno (a) "+ aluno +" está matriculado (a) e cursando regularmente o  "+ serie +" ano do Ensino Fundamental neste estabelecimento de ensino no ano corrente.");
            texto.setAlignment(3);
            texto.setSpacingAfter(30);
            dec.add(texto);

            Paragraph dados = new Paragraph("Data de nascimento: "+ nascimento +"\nMãe: "+ mae +"\nPai:"+ pai );
            //dados.setAlignment(Element.ALIGN_LEFT);
            dados.setSpacingAfter(30);
            dec.add(dados);

            Paragraph assinatura = new Paragraph("____________________________\n" + diretor + "\nDiretor");
            assinatura.setAlignment(1);
            assinatura.setSpacingAfter(30);
            dec.add(assinatura);

            Paragraph local = new Paragraph("Agricolândia - PI, "+ new Date());
            local.setAlignment(2);
            //assinatura.setSpacingAfter(30);
            dec.add(local);

            dec.close();

        } catch (Exception e){
            e.printStackTrace();
        }

        try {
            Desktop.getDesktop().open(new File("dec.pdf"));
        } catch (IOException ex) {
            System.out.println("Errooouu!"+ ex);
        }
        System.out.println("nome: "+nome+"\naluno: "+aluno);
    }
}

In the case, I was testing first pass only the student's name.

Author: Dherik, 2018-04-09

1 answers

Good Morning, create a student class that will be your DTO and a function in the class that connects to the database that returns that student. Class example:

public class Aluno {

private String nomeAluno;
private int matriculaAluno;

public String getNomeAluno() {
    return nomeAluno;
}

public void setNomeAluno(String nomeAluno) {
    this.nomeAluno = nomeAluno;
}

public int getMatriculaAluno() {
    return matriculaAluno;
}

public void setMatriculaAluno(int matriculaAluno) {
    this.matriculaAluno = matriculaAluno;
}

And the method in the query class to return this:

private List<Aluno> buscaTodosOsAlunos() {
    // ...
}

I hope I helped. Good luck.

 1
Author: Jota Freitas Jr, 2018-04-10 17:35:17