Create Array using SELECT output

Asked

Viewed 475 times

0

public class TelaGrafico extends javax.swing.JFrame {

    Connection conexao = null;
    PreparedStatement pst = null;
    ResultSet rs = null;


    /**
     * Creates new form TelaGrafico
     */
    public TelaGrafico() {
        initComponents();
        conexao = ModuloConexao.conectar();
        jRadioButtonNatal.setSelected(true);
        jLabelGerarGrafico.setVisible(false);
        jProgressBarGerarGrafico.setVisible(false);

        Calendar calen = Calendar.getInstance();
        int anos = calen.get(Calendar.YEAR);
        jTextFieldAno.setText(Integer.toString(anos));
    }


    //MÉTODO PARA GERAR GRAFICO PELA LOCALIDADE:
    private void GraficoLocalidade() {
        String localidade = jComboBoxLocalidade.getSelectedItem().toString();
        String ano = jTextFieldAno.getText();
        String semIni = jTextFieldSemInicial.getText();
        String semFin = jTextFieldSemFinal.getText();
        switch (jComboBoxDistritos.getSelectedIndex()) {
            case 0:
                //NORTE:
                break;
            case 1:
                //SUL:
                break;
            case 2:
                //LESTE:

                String dadosGrafico = "select sem_epi,ipo,ido from tb_indices_leste where localidade='" + localidade + "' and ano='" + ano + "' and sem_epi between '" + semIni + "' and '"+semFin+"'";
                try {
                    pst = conexao.prepareStatement(dadosGrafico);
                    rs = pst.executeQuery();
                    if (rs.next()) {}

The code ends here and this SELECT above returns me to the table:

+---------+-------+-------+
| sem_epi | ipo   | ido   |
+---------+-------+-------+
|       1 | 100.0 |  23.1 |
|       2 |  88.9 |  85.0 |
|       3 |  77.8 | 145.3 |
|       4 |  77.8 |   4.0 |
|       5 | 100.0 |   5.0 |
+---------+-------+-------+

How to create an Array[][] using the result of this table in JAVA?

  • could if how is returned this Array? ie the code to get to that point!

  • The code does not return any array to me. The code stops at this (SELECT) query and the question is this, how to create an array with these table values.

  • So share this code!

  • Ready. The complete code.

  • Need to make a class and create a list of this class will be what you need?

  • I believe I don’t need another class, I just need to take these values and put them in an array[][]. I think this solves.

  • If you are going to use this result where ?

  • @Virgilionovic It worked out too much! Thank you so much for the time and patience! Hug!

  • whether it was useful to accept as an answer to your question

Show 4 more comments

1 answer

2

By the comments the Array is the type float with 5 rows and 3 columns SQL, an example:

try {
    pst = conexao.prepareStatement(dadosGrafico);
    rs = pst.executeQuery();
    float[][] items = new float[5][3];
    int line = 0;
    while (rs.next()) 
    {
        items[line][0] = rs.getInt("sem_ip");
        items[line][1] = rs.getFloat("ipo");
        items[line][2] = rs.getFloat("ido");
        line++;
    }
}

Browser other questions tagged

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