Comparison of java array

Asked

Viewed 78 times

-3

I was doing a project and I needed the following logic... https://www.devmedia.com.br/multiplos-resultsets-em-java/21152 and I came across the following situation... and if it were with two separate consultations how would be? in my case I have to compare the result of the first select and search in the second the items q correspond to the first... to be more specific I have a select with patients who have appointments and I need to seek the care of each patient

Connection

package geradorhtml3;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Conexao {

    public Connection conFirebird() {

        Connection con = null;
        String driver = "org.firebirdsql.jdbc.FBDriver";
        String user = "SYSDBA";
        String senha = "masterkey";
        String url = "jdbc:firebirdsql://localhost:3050/D:/firebird/meubanco.FDB";

        try {
            Class.forName(driver);
            con = (Connection) DriverManager.getConnection(url, user, senha);
            System.out.println("Conexão realizada com sucesso.");

        } catch (ClassNotFoundException | SQLException ex) {
            System.err.println(ex.getMessage());

        }
        return con;
    }

}

Patient package geradorhtml3;

import java.sql.Date;

public class Paciente {

       private static String nome;  
       private static String cpf;  
       private static String tel;  
       private static Date dataNasc; 

    public static String getNome() {
        return nome;
    }

    public static void setNome(String aNome) {
        nome = aNome;
    }

    public static String getCpf() {
        return cpf;
    }

    public static void setCpf(String aCpf) {
        cpf = aCpf;
    }

    public static String getTel() {
        return tel;
    }

    public static void setTel(String aTel) {
        tel = aTel;
    }

    public static Date getDataNasc() {
        return dataNasc;
    }

    public static void setDataNasc(Date aDataNasc) {
        dataNasc = aDataNasc;
    }


}

Cycles

package geradorhtml3;

import java.sql.Date;

public class Ciclo {

    private static String id;
    private static String nome;
    private static String cod_paciente;
    private static Date data;

    public static String getId() {
        return id;
    }

    public static void setId(String aId) {
        id = aId;
    }

    public static String getNome() {
        return nome;
    }

    public static void setNome(String aNome) {
        nome = aNome;
    }

    public static String getCod_paciente() {
        return cod_paciente;
    }

    public static void setCod_paciente(String aCod_paciente) {
        cod_paciente = aCod_paciente;
    }

    public static Date getData() {
        return data;
    }

    public static void setData(Date aData) {
        data = aData;
    }



}

Here lies my doubt

package geradorhtml3;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Buscar {

    private static ArrayList c;

    private static ArrayList f;

    public Buscar() {

        try {

            ResultSet[] rs = new ResultSet[2];
            Connection con = new Conexao().conFirebird();
            CallableStatement cs = null;
            //String sql = "SELECT * FROM PACIENTE";            
            //String sql = "SELECT * FROM CICLOS";
            /* 
            Estou tentando juntar esses dois selects os quais tem um relacionamento, 
            porem NÃO pode ser feito no banco, tem q ser feito em codigo a uniao deles
            */

            String sql = "{CALL RETORNA_CLI_FORN()}";

            cs = con.prepareCall(sql);
            rs[0] = cs.executeQuery();

            c = new ArrayList();
            Paciente cl = new Paciente();

            while (rs[0].next()) {

                cl.setNome(rs[0].getString("NOME"));
                cl.setCpf(rs[0].getString("CPF"));
                cl.setTel(rs[0].getString("TEL"));
                cl.setDataNasc(rs[0].getDate("DATA_NASCIMENTO"));
                c.add(cl);

            }

            if (cs.getMoreResults()) {
                rs[1] = cs.getResultSet();
                f = new ArrayList();
                Ciclo fo = new Ciclo();
                while (rs[1].next()) {
                    fo.setNome(rs[1].getString("NOME"));
                    fo.setCod_paciente(rs[1].getString("CNPJ"));
                    f.add(fo);
                }
            }

        } catch (SQLException ex) {
            Logger.getLogger(Paciente.class.getName()).log(Level.SEVERE, null, ex);

        }

    }

    public Iterator retornaPaciente() {
        Iterator i = c.iterator();
        return i;

    }

    public Iterator retornaCiclo() {
        Iterator i = f.iterator();
        return i;
    }

}

MAIN

package geradorhtml3;

import java.util.Iterator;

public class GeradorHtml3 {

    public static void main(String[] args) {

        Buscar c = new Buscar();
        Iterator _ic = null;
        Iterator _if = null;
        Paciente cl = new Paciente();
        Ciclo fo = new Ciclo();
        _ic = c.retornaPaciente();
        _if = c.retornaCiclo();

        while (_ic.hasNext()) {
            cl = (Paciente) _ic.next();
            System.out.println(
                cl.getNome() + " - " + 
                cl.getCpf() + " -" + 
                cl.getTel() + " - " + 
                cl.getDataNasc());
        }

        while (_if.hasNext()) {
            fo = (Ciclo) _if.next();
            System.out.println(fo.getNome() + " - " + fo.getCod_paciente());

        }

    }

}
  • 1

    There’s no way we can guess your code and give you tips or tell you what to do without seeing what you’ve got so far. The logic is one thing, the approach is another totally different. Edit the post and put what you already have. Don’t see it as a criticism, but it’s in the SOPT rules and, if you don’t, the chances of your topic being deleted are great.

  • 1

    Help us help you, mate. Enter your code, especially what you have tried to do to solve the problem.

  • 1

    Man, that article you linked up is hideous. It starts with totally messed-up formatting and totally useless code comments, but even ignoring those and just looking at the implemented logic, it’s an aberration, a festival of bad programming practices, one of the worst articles I’ve ever read. These articles should be written to inform and teach, but this one only makes teaching wrong and confuse. I would love to write an answer about that, but... Which patients and care are those you talk about? What are the related tables?

1 answer

0

I received some Criticism and no solution to my question, but I was able to solve and I will post to help someone who may have the same doubt...

To sweep 2 different contultas being compared and filled via code I used the following logic (very coarse, but it worked!)

public class GeradorHtml2 {

    public static void main(String[] args) throws ClassNotFoundException, FileNotFoundException, SQLException, IOException {

        try {
            Connection conn = new Conectar().conFirebird();
            Statement stmt = conn.createStatement();
            String Html = null;
            ResultSet resultSet1 = stmt.executeQuery(""
                    + "select COD_PACIENTE FROM PACIENTE"
                    + "");

            while (resultSet1.next()) {

                Integer COD_PACIENTE = resultSet1.getInt("COD_PACIENTE");

                System out Println(COD_PACIENTE);

                Statement statement2 = conn.createStatement();
                ResultSet resultSet2 = statement2.executeQuery(""
                        + "select * FROM ATENDIMENTO "
                        + "where COD_PACIENTE IN (" + COD_PACIENTE + ")\n"
                        + "");

                while (resultSet2.next()) {

                    String TRATAMENTO = resultSet2.getString("TRATAMENTO");
                    System out Println(TRATAMENTO);

                }
            }

        } catch (SQLException e) {
            //JOptionPane.showMessageDialog(null, e.toString());
            e.printStackTrace();
        }

    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.